1use std::ops::{AddAssign, Neg, Shl, Shr};
2use std::convert::{TryFrom, From, TryInto};
3use std::fmt::Display;
4
5pub trait IntegerBaseFunctions {
7 const IS_SIGNED: bool;
8 fn get_absolute_uint(self: &Self) -> u64;
9 fn is_val_negative(self: &Self) -> bool;
10 fn switch_sign_if_possible(self: &Self) -> Self;
11 fn get_zero() -> Self;
12}
13macro_rules! impl_integer_signed {
14 ($type:ty) => {
15 impl IntegerBaseFunctions for $type {
16 const IS_SIGNED: bool = true;
17 fn get_absolute_uint(self: &Self) -> u64 {
18 if *self < 0 { self.clone().neg() as u64 }
19 else { self.clone() as u64 }
20 }
21 fn is_val_negative(self: &Self) -> bool { *self < 0 }
22 fn switch_sign_if_possible(self: &Self) -> Self { return self.clone().neg() }
23 fn get_zero() -> Self { 0 as $type }
24 }
25} }
26macro_rules! impl_integer_unsigned {
27 ($type:ty) => {
28 impl IntegerBaseFunctions for $type {
29 const IS_SIGNED: bool = false;
30 fn get_absolute_uint(self: &Self) -> u64 { self.clone() as u64 }
31 fn is_val_negative(self: &Self) -> bool { false }
32 fn switch_sign_if_possible(self: &Self) -> Self { return self.clone() }
33 fn get_zero() -> Self { 0 as $type }
34 }
35} }
36impl_integer_signed!(i8);
37impl_integer_signed!(i16);
38impl_integer_signed!(i32);
39impl_integer_signed!(i64);
40impl_integer_unsigned!(u8);
41impl_integer_unsigned!(u16);
42impl_integer_unsigned!(u32);
43impl_integer_unsigned!(u64);
44
45#[derive(Debug, Clone)]
47pub struct Biseri {
48 cur_cache_u8: u16,
49 sub_byte_counter: u8,
50 data_cache: Vec<u8>,
51 final_total_bits: u64,
52}
53
54fn bits_for_and(x: u8) -> u16 {
55 u16::MAX >> (u16::BITS as u8 - x)
56}
57
58#[allow(dead_code)]
59pub trait BiserdiTraitVarBitSize : Sized {
60 fn bit_serialize(self: &Self, total_bits: u64, biseri: &mut Biseri) -> Option<u64>;
61 fn bit_deserialize(version_id: u16, total_bits: u64, bides: &mut Bides) -> Option<(Self, u64)>;
62}
63#[allow(dead_code)]
64pub trait BiserdiTrait: Sized {
65 fn bit_serialize(self: &Self, biseri: &mut Biseri) -> Option<u64>;
66 fn bit_deserialize(version_id: u16, bides: &mut Bides) -> Option<(Self, u64)>;
67}
68
69pub struct BiserSizes {
70 pub total_bits: u64,
71 pub total_bytes: u64
72}
73#[allow(dead_code)]
74impl Biseri {
75 pub fn new() -> Biseri {
76 Biseri { cur_cache_u8: 0, data_cache: Vec::new(), sub_byte_counter: 0, final_total_bits: 0 }
77 }
78
79 pub fn data_size_bytes(&self) -> u64 {
80 self.data_cache.len() as u64
81 }
82 pub fn get_data_ref(&self) -> &Vec<u8> {
83 &self.data_cache
84 }
85 pub fn get_data(&self) -> Vec<u8> {
86 self.data_cache.clone()
87 }
88
89 fn add_data_base_u8(&mut self, cur_u8: &u8, total_bits: u64) -> u64 {
90 if total_bits > 0 {
91 let cur_u16 = cur_u8.clone() as u16;
92 let shift_by = self.sub_byte_counter & 7;
93
94 let cur_bit_size = std::cmp::min(8, total_bits as u8);
95 let cur_u16 = (cur_u16 & (bits_for_and(cur_bit_size))) << shift_by;
97 self.cur_cache_u8 += cur_u16;
98
99 self.sub_byte_counter += cur_bit_size;
100 let total_bits = total_bits - cur_bit_size as u64;
101
102 if self.sub_byte_counter >= 8 {
103 self.sub_byte_counter -= 8;
104 let u8_to_add = (self.cur_cache_u8 & 0xFF) as u8;
105 self.data_cache.push(u8_to_add);
106 self.cur_cache_u8 >>= 8;
107 }
108 total_bits
109 }
110 else { 0 }
111 }
112
113 pub fn add_data(&mut self, cur_data: &Vec<u8>, total_bits: u64) -> Option<u64> {
114 let mut cur_total_bits = total_bits;
122 for cu8 in cur_data.iter() {
123 cur_total_bits = self.add_data_base_u8(cu8, cur_total_bits);
124 }
125 Some(total_bits)
126 }
127
128 pub fn add_biseri_data(&mut self, data: &Biseri) -> Option<u64> {
129 self.add_data(&data.data_cache, data.final_total_bits)
130 }
133 pub fn finish_add_data(&mut self) -> Option<BiserSizes> {
134 if self.final_total_bits > 0 {
135 None
136 }
137 else {
138 let total_bits = ((self.data_cache.len() as u64) << 3) + self.sub_byte_counter as u64;
139 if self.sub_byte_counter > 0 {
140 let u8_to_add = (self.cur_cache_u8 & 0xFF) as u8;
141 self.data_cache.push(u8_to_add);
142 }
143
144 self.final_total_bits = total_bits;
145 Some(BiserSizes{total_bits, total_bytes: self.data_cache.len() as u64})
146 }
147 }
148}
149
150#[derive(Debug, Clone)]
151pub struct Bides {
152 cur_read_pos: u64,
153 sub_byte_counter: u8,
154 pub data_cache: Vec<u8>,
155}
156#[allow(dead_code)]
157impl Bides {
158 pub fn new() -> Bides { Bides { cur_read_pos: 0, sub_byte_counter: 0, data_cache: Vec::new() } }
159 pub fn from_vec(data: &Vec<u8>) -> Bides {
160 Bides{sub_byte_counter: 0, data_cache: data.clone(), cur_read_pos: 0}
161 }
162 pub fn from_biseri(biseri: &Biseri) -> Bides {
163 Bides{sub_byte_counter: 0, data_cache: biseri.get_data().clone(), cur_read_pos: 0}
164 }
165
166 pub fn append_data(&mut self, data: &Vec<u8>) {
167 self.data_cache.extend(data);
168 }
169
170 pub fn reset_position(&mut self) {
171 self.sub_byte_counter = 0;
172 self.cur_read_pos = 0;
173 }
174 pub fn decode_data_base_u8(&mut self, total_bits: u64) -> Option<(u8, u64)> {
175 if self.cur_read_pos as usize >= self.data_cache.len() { return None }
176 let mut cur_u16: u16 = self.data_cache[self.cur_read_pos as usize] as u16;
177 if (self.cur_read_pos + 1 < self.data_cache.len() as u64) && (self.sub_byte_counter > 0) {
178 cur_u16 += (self.data_cache[(self.cur_read_pos + 1) as usize] as u16) << 8;
179 }
180
181 let cur_used_bits = std::cmp::min(total_bits as u8, 8);
182
183 let d = ((cur_u16 >> self.sub_byte_counter) & bits_for_and(cur_used_bits)) as u8;
186
187 self.sub_byte_counter += cur_used_bits;
188 if self.sub_byte_counter >= 8 {
189 self.sub_byte_counter -= 8;
190 self.cur_read_pos += 1;
191 }
192
193 Some((d, total_bits - cur_used_bits as u64))
194 }
195
196 pub fn decode_data(&mut self, total_bits: u64, expected_bytes: u32) -> Option<Vec<u8>> {
197 if self.cur_read_pos >= self.data_cache.len() as u64 {
198 return None;
199 }
200 let mut cur_total_bits = total_bits;
201 let mut dv = Vec::new();
202 while cur_total_bits > 0 {
203 let d;
204 (d, cur_total_bits) = match self.decode_data_base_u8(cur_total_bits) {
205 Some(d) => d, None => { return None; }
206 };
207 dv.push(d);
208 }
209 let num_add = expected_bytes - dv.len() as u32;
210 for _ in 0..num_add { dv.push(0); }
211 Some(dv)
212 }
213
214 pub fn skip_bits(&mut self, bits: u64) {
215 let bits_total_pos = bits + self.sub_byte_counter as u64;
216 self.sub_byte_counter = (bits_total_pos.clone() & 7) as u8;
217 self.cur_read_pos += bits_total_pos >> 3;
218 }
219}
220
221macro_rules! impl_biserdi_var_bitsize_trait {
222 ($type:ty, $num_bytes: expr) => {
223 impl BiserdiTraitVarBitSize for $type {
224 fn bit_serialize(self: &Self, total_bits: u64, biseri: &mut Biseri) -> Option<u64> {
225 if Self::IS_SIGNED { self.is_val_negative().bit_serialize(biseri)?; }
226 let v = self.get_absolute_uint();
227 let vv = &v.to_le_bytes().to_vec();
228 let bits = biseri.add_data(vv, total_bits)?;
229
230 let bits_with_sign = if Self::IS_SIGNED { bits+1 } else { bits };
231 Some(bits_with_sign)
232 }
233 fn bit_deserialize(version_id: u16, total_bits: u64, bides: &mut Bides) -> Option<(Self, u64)> {
234 let is_neg = if Self::IS_SIGNED {
235 bool::bit_deserialize(version_id, bides)?.0 }
236 else { false };
237
238 let mut v = Self::from_le_bytes(
239 bides.decode_data(total_bits, $num_bytes)?.try_into().ok()?);
240 if is_neg { v = v.switch_sign_if_possible() }
241
242 let bits_with_sign = if Self::IS_SIGNED { total_bits + 1 } else { total_bits };
243 Some((v, bits_with_sign))
244 }
245 }
246 };
247}
248macro_rules! impl_biserdi {
249 ($type:ty, $num_bits: expr) => {
250 impl BiserdiTrait for $type {
251 fn bit_serialize(self: &Self, biseri: &mut Biseri) -> Option<u64> {
252 Some(biseri.add_data(&self.clone().to_le_bytes().to_vec(), ($num_bits))?)
253 }
254 fn bit_deserialize(_version_id: u16, bides: &mut Bides) -> Option<(Self, u64)> {
255 Some((Self::from_le_bytes(bides.decode_data(
256 ($num_bits), std::cmp::max((($num_bits)>>3),1))?.try_into().ok()?),
257 $num_bits))
258 }
259 }
260 };
261}
262
263impl BiserdiTrait for bool {
264 fn bit_serialize(self: &Self, biseri: &mut Biseri) -> Option<u64> {
265 let val = if *self { 1_u8 } else { 0_u8 };
266 biseri.add_data_base_u8(&val, 1);
267 Some(1)
268 }
269 fn bit_deserialize(_version_id: u16, bides: &mut Bides) -> Option<(Self, u64)> {
270 let vec = bides.decode_data(1, 1)?;
271 Some((if vec[0] == 0 { false } else { true }, 1))
272 }
273}
274impl_biserdi_var_bitsize_trait!(u8, u8::BITS>>3);
275impl_biserdi_var_bitsize_trait!(u16, u16::BITS>>3);
276impl_biserdi_var_bitsize_trait!(u32, u32::BITS>>3);
277impl_biserdi_var_bitsize_trait!(u64, u64::BITS>>3);
278impl_biserdi_var_bitsize_trait!(i8, i8::BITS>>3);
279impl_biserdi_var_bitsize_trait!(i16, i16::BITS>>3);
280impl_biserdi_var_bitsize_trait!(i32, i32::BITS>>3);
281impl_biserdi_var_bitsize_trait!(i64, i64::BITS>>3);
282impl_biserdi!(f32, 32);
283impl_biserdi!(f64, 64);
284
285impl<T> BiserdiTrait for Option<T> where T: BiserdiTrait + Default {
286 fn bit_serialize(self: &Self, biseri: &mut Biseri) -> Option<u64> {
287 let mut size = 1;
288 match self {
289 None => { false.bit_serialize(biseri)?; },
290 Some(v) => {
291 true.bit_serialize(biseri)?;
292 size += v.bit_serialize(biseri)?;
293 }
294 }
295 Some(size)
296 }
297 fn bit_deserialize(version_id: u16, bides: &mut Bides) -> Option<(Self, u64)> {
298 let mut size = 1;
299
300 let (is_set, _) = bool::bit_deserialize(version_id, bides)?;
301 let v = if is_set {
302 let vv = T::bit_deserialize(version_id, bides)?;
303 size += vv.1.clone();
304 Some(vv.0)
305 }
306 else { None };
307
308 Some((v, size))
309 }
310}
311
312#[derive(Debug, Clone, PartialEq, Eq, Copy)]
313pub struct FixedArray<T: Default, const N: usize> { pub val: [T; N] }
314impl<T, const N: usize> BiserdiTrait for FixedArray<T, N> where T: BiserdiTrait + Default + Copy {
315 fn bit_serialize(self: &Self, biseri: &mut Biseri) -> Option<u64> {
316 let mut s = 0;
317 for i in 0..N { s += self.val[i].bit_serialize(biseri)?; }
318 Some(s)
319 }
320 fn bit_deserialize(version_id: u16, bides: &mut Bides) -> Option<(Self, u64)> {
321 let mut v: [T; N] = [T::default(); N];
322 let mut bits = 0;
323 let mut cur_bits;
324 for i in 0..N { (v[i], cur_bits) = T::bit_deserialize(version_id, bides)?; bits += cur_bits; }
325 Some((Self{ val: v}, bits))
326 }
327}
328impl<T, const N: usize> BiserdiTraitVarBitSize for FixedArray<T, N> where T: BiserdiTraitVarBitSize + Default + Copy {
329 fn bit_serialize(self: &Self, total_bits_per_unit: u64, biseri: &mut Biseri) -> Option<u64> {
330 let mut s = 0;
331 for i in 0..N {
332 s += self.val[i].bit_serialize(total_bits_per_unit, biseri)?;
333 }
334 Some(s)
335 }
336 fn bit_deserialize(version_id: u16, total_bits_per_unit: u64, bides: &mut Bides) -> Option<(Self, u64)> {
337 let mut v = [T::default(); N];
338 let mut bits = 0;
339 let mut cur_bits;
340 for i in 0..N {
341 (v[i], cur_bits) = T::bit_deserialize(version_id, total_bits_per_unit, bides)?; bits += cur_bits; }
342 Some((Self{ val: v}, bits))
343 }
344}
345impl<T: Sized + Copy + BiserdiTrait + Default, const N: usize> From<[T; N]> for FixedArray<T, N> {
346 fn from(val: [T;N]) -> Self { Self{ val: val.clone()} } }
347impl<T: Sized + Copy + Display + Default, const N: usize> std::fmt::Display for FixedArray<T, N> {
348 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
349 let das: Vec<String> = self.val.iter().map(|v| format!("{}", v)).collect();
350 write!(f, "[{}]", das.join(", "))
351 } }
352impl<T: Sized + Copy + BiserdiTrait + Default, const N: usize> Default for FixedArray<T, N> {
353 fn default() -> Self {
354 Self{val: [T::default(); N]}
355 }
356}
357
358pub struct DynArray<T, const DYNSIZEBITS: u8> { pub val: Vec<T> }
359impl<T, const DYNSIZEBITS: u8> BiserdiTrait for DynArray<T, DYNSIZEBITS> where T: BiserdiTrait + Default + Copy {
360 fn bit_serialize(self: &Self, biseri: &mut Biseri) -> Option<u64> {
361 let mut s = 0;
362 s += DynInteger::<u32, DYNSIZEBITS>::new(self.val.len() as u32).bit_serialize(biseri)?;
363 for d in self.val.iter() { s += d.bit_serialize(biseri)?; };
364 Some(s)
365 }
366 fn bit_deserialize(version_id: u16, bides: &mut Bides) -> Option<(Self, u64)> {
367 let mut s = 0;
368 let (v, cs) =
369 DynInteger::<u32, DYNSIZEBITS>::bit_deserialize(version_id, bides)?;
370 let mut data = Vec::with_capacity(cs as usize);
371 for _ci in 0..v.val {
372 let (vi, si) = T::bit_deserialize(version_id, bides)?;
373 s += si;
374 data.push(vi);
375 }
376 Some((Self{ val: data }, s+cs))
377 }
378}
379impl<T: Sized + Copy + BiserdiTraitVarBitSize, const DYNSIZEBITS: u8, const N: usize> From<[T; N]> for DynArray<T, DYNSIZEBITS> {
380 fn from(val: [T;N]) -> Self {
381 DynArray{ val:Vec::from(val)}
382 } }
383impl<T: Sized + Copy + BiserdiTraitVarBitSize, const DYNSIZEBITS: u8> From<Vec<T>> for DynArray<T, DYNSIZEBITS> {
384 fn from(val: Vec<T>) -> Self {
385 DynArray{ val: val.clone()}
386 } }
387impl<T: Sized + Copy + BiserdiTraitVarBitSize + Display, const DYNSIZEBITS: u8> std::fmt::Display for DynArray<T, DYNSIZEBITS> {
388 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
389 let das: Vec<String> = self.val.iter().map(|v| v.to_string()).collect();
390 write!(f, "[{} |dynbits:{}]", das.join(", "), DYNSIZEBITS)
391 } }
392impl<T: Sized + Copy + BiserdiTraitVarBitSize, const DYNSIZEBITS: u8> Default for DynArray<T, DYNSIZEBITS> {
393 fn default() -> Self {
394 Self{val: Vec::new()}
395 }
396}
397
398
399#[derive(Debug, Clone, PartialEq, Eq, Default, Copy)]
400pub struct VarWithGivenBitSize<T: Sized + Copy + BiserdiTraitVarBitSize + Default, const NUM_BITS: u64> {
401 pub val: T
402}
403impl<T: Sized + Copy + BiserdiTraitVarBitSize + Default, const NUM_BITS: u64> VarWithGivenBitSize<T, NUM_BITS> {
404 pub fn new(v: T) -> Self { VarWithGivenBitSize {val: v} }
405}
406impl<T: Sized + Copy + BiserdiTraitVarBitSize + Default, const NUM_BITS: u64> From<T> for VarWithGivenBitSize<T, NUM_BITS> {
407 fn from(val: T) -> Self {
408 VarWithGivenBitSize::<T, NUM_BITS>::new(val)
409 }
410}
411impl<T: Sized + Copy + BiserdiTraitVarBitSize + Display + Default, const NUM_BITS: u64> std::fmt::Display for VarWithGivenBitSize<T, NUM_BITS> {
417 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
418 write!(f, "{} |bits:{}", self.val, NUM_BITS)
419} }
420impl<T: Sized + Copy + BiserdiTraitVarBitSize + Default, const NUM_BITS: u64> BiserdiTrait for VarWithGivenBitSize<T, NUM_BITS> {
421 fn bit_serialize(self: &Self, biseri: &mut Biseri) -> Option<u64> {
422 self.val.bit_serialize(NUM_BITS, biseri)
423 }
424 fn bit_deserialize(version_id: u16, bides: &mut Bides) -> Option<(Self, u64)> {
425 let v = T::bit_deserialize(version_id, NUM_BITS, bides)?;
426 Some((Self{val: v.0}, v.1))
427 }
428}
429
430#[derive(Debug, Clone, PartialEq, Eq, Default, Copy)]
431pub struct DynInteger<
432 T: Sized + Copy + BiserdiTraitVarBitSize + AddAssign + Shl<Output = T> + Shr + Ord + PartialEq + IntegerBaseFunctions + Default, const N: u8> {
434 pub val: T
435}
436impl<T: Display + Sized + Copy + BiserdiTraitVarBitSize + AddAssign + Shl<Output = T> + Shr + Ord + PartialEq + TryFrom<u64>
437 + IntegerBaseFunctions + Default, const N: u8> DynInteger<T, N> {
438 const DYN_SIZE: u8 = N;
439 pub fn new(v: T) -> Self{
440 DynInteger{val: v}
441 }
442}
443impl<T: Display + Sized + Copy + BiserdiTraitVarBitSize + AddAssign + Shl<Output = T> + Shr + Ord + PartialEq + TryFrom<u64>
444 + IntegerBaseFunctions + Default, const N: u8> BiserdiTrait for DynInteger<T, N> {
445 fn bit_serialize(self: &Self, biseri: &mut Biseri) -> Option<u64> {
446 let mut bit_size: u64 = 1;
449 let mut val_work = self.val.get_absolute_uint();
450
451 if T::IS_SIGNED { self.val.is_val_negative().bit_serialize(biseri)?; bit_size +=1; }
452
453 (val_work != 0).bit_serialize(biseri);
454 while val_work > 0 {
455 val_work.bit_serialize(u64::from(Self::DYN_SIZE), biseri)?;
457 val_work >>= Self::DYN_SIZE;
458 bit_size += (Self::DYN_SIZE + 1) as u64;
459 let further_data = val_work > 0;
460 further_data.bit_serialize(biseri);
461 }
462 Some(bit_size)
463 }
464 fn bit_deserialize(version_id: u16, bides: &mut Bides) -> Option<(Self, u64)> {
465 let mut cur_shift: u64 = 0;
466 let mut v: u64 = 0;
467 let mut negative_sign = false;
468
469 let mut bit_size = 1;
470 if T::IS_SIGNED {
471 negative_sign = bool::bit_deserialize(version_id, bides)?.0; bit_size += 1; }
472 let mut further_data = bool::bit_deserialize(version_id, bides)?.0;
473 while further_data {
474 let vt = u64::bit_deserialize(version_id, Self::DYN_SIZE as u64, bides)?;
475 bit_size += vt.1 + 1;
476 v += vt.0 << cur_shift;
477 cur_shift += u64::from(Self::DYN_SIZE);
478 further_data = bool::bit_deserialize(version_id, bides)?.0;
479 }
480 let mut vv= T::try_from(v).ok()?;
481 if negative_sign {
482 vv = vv.switch_sign_if_possible();
483 }
484 Some((Self{val: vv}, bit_size))
485 }
486}
487impl<T: Display + Sized + Copy + BiserdiTraitVarBitSize + AddAssign + Shl<Output = T> + Shr + Ord + PartialEq + TryFrom<u64>
488 + IntegerBaseFunctions + Default, const N: u8> Display for DynInteger<T, N> {
489 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
490 write!(f, "{} |dynbits:{}]", self.val, N)
491 } }
492
493
494#[derive(Debug, Clone, PartialEq, Default)]
496pub enum FixPrecisionVal {
497 #[default]
498 Overflow,
499 Value(f64),
500 Underflow
501}
502#[derive(Debug, Clone, PartialEq, Default)]
503pub struct FixPrecisionMinMax<const NUM_BITS: u8, const MIN_IVALUE: i64, const MAX_IVALUE: i64> {
504 pub val: FixPrecisionVal
505}
506impl<const NUM_BITS: u8, const MIN_IVALUE: i64, const MAX_IVALUE: i64> FixPrecisionMinMax<NUM_BITS, MIN_IVALUE, MAX_IVALUE> {
507 const MIN_VALUE: f64 = MIN_IVALUE as f64;
508 const MAX_VALUE: f64 = MAX_IVALUE as f64;
509 const RANGE_VALUE: f64 = Self::MAX_VALUE - Self::MIN_VALUE;
510 const MAX_INT_VALUE_FOR_BITS: u64 = (1_u64<<NUM_BITS) - 1_u64;
511 const MAX_VALUE_FOR_BITS: f64 = (Self::MAX_INT_VALUE_FOR_BITS - 1_u64) as f64;
512
513 pub fn new(val: f64) -> Self {
514 if val > Self::MAX_VALUE { FixPrecisionMinMax { val: FixPrecisionVal::Overflow } }
515 else if val < Self::MIN_VALUE { FixPrecisionMinMax { val: FixPrecisionVal::Underflow } }
516 else { FixPrecisionMinMax { val: FixPrecisionVal::Value(val) } }
517 }
518}
519impl<const NUM_BITS: u8, const MIN_IVALUE: i64, const MAX_IVALUE: i64> BiserdiTrait for FixPrecisionMinMax<NUM_BITS, MIN_IVALUE, MAX_IVALUE> {
520 fn bit_serialize(self: &Self, biseri: &mut Biseri) -> Option<u64> {
521 let v = match self.val {
522 FixPrecisionVal::Value(v) =>
524 ((v - Self::MIN_VALUE) / Self::RANGE_VALUE * Self::MAX_VALUE_FOR_BITS + 1.0) as u64,
525 FixPrecisionVal::Underflow => 0,
526 FixPrecisionVal::Overflow => Self::MAX_INT_VALUE_FOR_BITS
527 };
528 v.bit_serialize(NUM_BITS as u64, biseri)
529 }
530 fn bit_deserialize(version_id: u16, bides: &mut Bides) -> Option<(Self, u64)> {
531 let (v, bits) = u64::bit_deserialize(version_id, NUM_BITS as u64, bides)?;
533 let vv = if v == 0 { FixPrecisionVal::Underflow }
534 else if v == Self::MAX_INT_VALUE_FOR_BITS { FixPrecisionVal::Overflow }
535 else {
536 FixPrecisionVal::Value(((v-1) as f64) / Self::MAX_VALUE_FOR_BITS
537 * Self::RANGE_VALUE + Self::MIN_VALUE)
538 };
539 Some((Self{val: vv}, bits))
540 }
541}
542impl<const NUM_BITS: u8, const MIN_IVALUE: i64, const MAX_IVALUE: i64> Display for FixPrecisionMinMax<NUM_BITS, MIN_IVALUE, MAX_IVALUE> {
543 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
544 match self.val {
545 FixPrecisionVal::Overflow => write!(f, "Overflow |dynbits:{}", NUM_BITS),
546 FixPrecisionVal::Underflow => write!(f, "Underflow |dynbits:{}", NUM_BITS),
547 FixPrecisionVal::Value(v) => write!(f, "{} |dynbits:{}", v, NUM_BITS)
548 }
549 } }
550
551#[derive(Debug, Clone, PartialEq)]
553pub struct Binary<const DYNSIZEBITS: u8> {
554 pub val: Vec<u8>
555}
556impl<const DYNSIZEBITS: u8> Binary<DYNSIZEBITS> {
557 pub fn new(data: Vec<u8>) -> Self {
558 Self{ val: data }
559 }
560 pub fn empty() -> Self {
561 Self{val: Vec::new()}
562 }
563}
564impl<const DYNSIZEBITS: u8> BiserdiTrait for Binary<DYNSIZEBITS> {
565 fn bit_serialize(self: &Self, biseri: &mut Biseri) -> Option<u64> {
566 let mut s = 0;
567 s += DynInteger::<u32, DYNSIZEBITS>::new(self.val.len() as u32).bit_serialize(biseri)?;
568 for d in self.val.iter() { s += d.bit_serialize(8, biseri)?; };
569 Some(s)
570 }
571 fn bit_deserialize(version_id: u16, bides: &mut Bides) -> Option<(Self, u64)> {
572 let mut s = 0;
573 let (v, cs) =
574 DynInteger::<u32, DYNSIZEBITS>::bit_deserialize(version_id, bides)?;
575 let mut data = Vec::with_capacity(cs as usize);
576 for _ci in 0..v.val {
577 let (vi, si) = u8::bit_deserialize(version_id, 8, bides)?;
578 s += si;
579 data.push(vi);
580 }
581 Some((Self{ val: data }, s+cs))
582 }
583}
584impl<const N: u8> std::fmt::Display for Binary< N> {
585 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
586 let hex = self.val.iter().map(|b| format!("{:02x}", b).to_string()).collect::<Vec<String>>().join(" ");
587 write!(f, "{:x?} |dynbits:{}", hex, N)
588 }
589}
590
591#[cfg(test)]
592mod bitis_base_serialization_deserialization {
593 use rstest::rstest;
594 use crate::lib_impl::berde::{Bides, Biseri};
595 use super::*;
596
597 #[rstest]
598 fn add_one_u8() {
599 let mut b = Biseri::new();
600 let d = 0b10101010_u8;
601
602 b.add_data_base_u8(&d, 8);
603
604 assert_eq!(b.clone().data_cache.len(), 1);
605 assert_eq!(b.sub_byte_counter, 0);
606
607 let r = b.finish_add_data().unwrap();
608 assert_eq!((r.total_bits, r.total_bytes), (8, 1));
609 }
610
611 #[rstest]
612 #[case::ot_uint4(4, 1, 4, 1)]
613 #[case::tt_uint4(4, 2, 8, 1)]
614 #[case::ot_uint6(6, 1, 6, 1)]
615 #[case::tt_uint6(6, 2, 12, 2)]
616 #[case::trt_uint6(6, 3, 18, 3)]
617 #[case::ft_uint6(6, 4, 24, 3)]
618 fn add_one_var(#[case] bitsize: u64, #[case] repeated: u8, #[case] final_bitsize: u64, #[case] num_bytes: u64) {
619 let mut b = Biseri::new();
620 let d = 0b10101010_u8;
621
622 for _i in 0..repeated {
623 b.add_data_base_u8(&d, bitsize);
624 }
625
626 let r = b.finish_add_data().unwrap();
627 assert_eq!((r.total_bits, r.total_bytes), (final_bitsize, num_bytes));
628 }
629
630 #[rstest]
631 #[case::ok(5, 4, 5)]
632 #[case::expected_overflow(0b10011, 4, 3)]
633 fn serialize_and_deserialize_base(#[case] val_in: u8, #[case] bitsize: u64, #[case] val_out: u8) {
634 let mut ser = Biseri::new();
635
636 ser.add_data_base_u8(&val_in, bitsize);
637 ser.finish_add_data();
638
639 let mut des = Bides::from_biseri(&ser);
640
641 assert_eq!(des.data_cache, ser.data_cache);
642
643 let r = des.decode_data_base_u8(bitsize);
644 assert!(r.is_some());
645 let (dd, bs) = r.unwrap();
646 assert_eq!(bs, 0);
647
648 println!("val_in: {val_in} vs. dd: {dd} (expected: {val_out}");
649 assert_eq!(val_out, dd);
650 }
651
652 #[rstest]
653 #[case::ok(3*256+5, 16, 3*256+5)]
654 #[case::ok(3*256+5, 12, 3*256+5)]
655 #[case::ok(3*256+5, 9, 1*256+5)]
656 fn serialize_and_deserialize_u16_single(#[case] val_in: u16, #[case] bitsize: u64, #[case] val_out: u16) {
657 let val_in_vec = val_in.to_le_bytes().clone();
658
659 let mut ser = Biseri::new();
660
661 let mut total_size = bitsize;
662 val_in_vec.clone().iter().for_each(|x| {
663 total_size = ser.add_data_base_u8(&x, bitsize);
664 });
665 ser.finish_add_data();
666
667 println!("ser.cache: {:?}", ser.data_cache);
668
669 assert_eq!(ser.data_cache.len(), 2);
670
671 let mut des = Bides::from_biseri(&ser);
672
673 assert_eq!(des.data_cache, ser.data_cache);
674
675 let mut dd = Vec::new();
676 let mut total_size = bitsize;
677 while total_size > 0 {
678 let ddd;
679 let r = des.decode_data_base_u8(total_size);
680 assert!(r.is_some());
681 (ddd, total_size) = r.unwrap();
682 dd.push(ddd);
683 };
684
685 let ddv = u16::from_le_bytes(dd.clone().try_into().unwrap());
686 println!("val_in: {val_in} ({val_in_vec:?}) vs. ddv: {ddv:?} ({dd:?}) (expected: {val_out})");
687 assert_eq!(val_out, ddv);
688 }
689
690 fn add_two_u16_fixed(ser: &mut Biseri, bits: u64) -> BiserSizes {
691 let d: u16 = 3;
692
693 ser.add_data(&d.to_le_bytes().to_vec(), bits);
694 ser.add_data(&d.to_le_bytes().to_vec(), bits);
695 ser.finish_add_data().unwrap()
696 }
697 #[rstest]
698 fn serialize_u16_fixed_full() {
699 let mut ser = Biseri::new();
700
701 let r = add_two_u16_fixed(&mut ser, 16);
702 let (lbits, lbytes) = (r.total_bits, r.total_bytes);
703
704 assert_eq!(ser.data_cache.len(), 4);
705 assert_eq!(lbytes, 4);
706 assert_eq!(lbits, 2 * 16);
707
708 assert_eq!(ser.data_cache[0], 3);
709 assert_eq!(ser.data_cache[1], 0);
710 assert_eq!(ser.data_cache[2], 3);
711 assert_eq!(ser.data_cache[3], 0);
712 }
713
714 #[rstest]
715 fn serialize_u16_fixed_12b() {
716 let mut ser = Biseri::new();
717
718 let r = add_two_u16_fixed(&mut ser, 12);
719 let (lbits, lbytes) = (r.total_bits, r.total_bytes);
720
721 assert_eq!(ser.data_cache.len(), 3);
722 assert_eq!(lbytes, 3);
723 assert_eq!(lbits, 2 * 12);
724
725 assert_eq!(ser.data_cache[0], 3);
726 assert_eq!(ser.data_cache[1], 3 << 4);
727 assert_eq!(ser.data_cache[2], 0);
728 }
729
730 #[rstest]
731 #[case::bitsize_16(16)]
732 #[case::bitsize_14(14)]
733 #[case::bitsize_12(12)]
734 fn ser_and_deserialize_u16_fixed(#[case] bits: u64) {
735 let mut ser = Biseri::new();
736
737 let _ = add_two_u16_fixed(&mut ser, bits);
738
739 let mut des = Bides::from_biseri(&ser);
740
741 assert_eq!(des.data_cache, ser.data_cache);
742
743 let d1 = des.decode_data(bits, 2);
744 assert!(d1.is_some());
745 let d2 = des.decode_data(bits, 2);
746 assert!(d2.is_some());
747
748 let d1 = d1.unwrap();
749 let d2 = d2.unwrap();
750
751 assert_eq!(d1[0], 3);
752 assert_eq!(d1[1], 0);
753 assert_eq!(d2[0], 3);
754 assert_eq!(d2[1], 0);
755 }
756
757 #[rstest]
758 fn ser_and_deserialize_i16_fixed() {
759 let mut ser = Biseri::new();
760
761 let v: i8 = -11;
762 v.bit_serialize(5, &mut ser);
763
764 let r = ser.finish_add_data().unwrap();
765 let (bits, bytes) = (r.total_bits, r.total_bytes);
766
767 println!("bits: {}, bytes: {}", bits, bytes);
768
769 let mut des = Bides::from_biseri(&ser);
770
771 let vv = i8::bit_deserialize(1,5, &mut des);
772 println!("v: {}, vv: {:?}", v, vv);
773
774 assert!(vv.is_some());
775
776 let vv = vv.unwrap();
777 println!("bits_des: {}", vv.1);
778 assert_eq!(v, vv.0);
779 }
780
781 #[rstest]
782 fn de_and_serialize_various_unsigned() {
783 let mut ser = Biseri::new();
785 let v1: u8 = 5;
786 v1.bit_serialize(6, &mut ser);
787 let v2: u16 = 15;
789 v2.bit_serialize(14, &mut ser);
790 let v3: u32 = 55;
792 v3.bit_serialize(22, &mut ser);
793 let r = ser.finish_add_data().unwrap();
795 let (bits, bytes) = (r.total_bits, r.total_bytes);
796
797 println!("bits: {}, bytes: {}", bits, bytes);
798
799 let mut des = Bides::from_biseri(&ser);
801 let vo1 = u8::bit_deserialize(1,6, &mut des);
802 let vo2 = u16::bit_deserialize(1,14, &mut des);
804 let vo3 = u32::bit_deserialize(1,22, &mut des);
806 println!("v1: {}, v2: {}, v3: {} vs vo1: {:?}, vo2: {:?}, vo3: {:?}", v1, v2, v3, vo1, vo2, vo3);
809
810 assert!(vo1.is_some());
812 assert_eq!(v1, vo1.unwrap().0);
813
814 assert!(vo2.is_some());
815 assert_eq!(v2, vo2.unwrap().0);
816
817 assert!(vo3.is_some());
818 assert_eq!(v3, vo3.unwrap().0);
819 }
820
821 #[rstest]
822 fn de_and_serialize_various_float() {
823 let mut ser = Biseri::new();
825 let v1: f32 = 56.78;
826 v1.bit_serialize(&mut ser);
827 let v2: u8 = 5;
829 v2.bit_serialize(5, &mut ser);
830 let v3: bool = true;
832 v3.bit_serialize(&mut ser);
833 let v4: bool = false;
835 v4.bit_serialize(&mut ser);
836 v1.bit_serialize(&mut ser);
838 let r = ser.finish_add_data().unwrap();
840 let (bits, bytes) = (r.total_bits, r.total_bytes);
841
842 println!("bits: {}, bytes: {}", bits, bytes);
843
844 let mut des = Bides::from_biseri(&ser);
846 let vo1 = f32::bit_deserialize(1, &mut des);
847 let vo2 = u8::bit_deserialize(1,5, &mut des);
848 let vo3 = bool::bit_deserialize(1,&mut des);
849 let vo4 = bool::bit_deserialize(1,&mut des);
850 let vo5 = f32::bit_deserialize(1,&mut des);
851 println!("vo1: {:?}, vo2: {:?}, vo3: {:?}, vo4: {:?}, vo4: {:?}", vo1, vo2, vo3, vo4, vo5);
858
859 assert!(vo1.is_some());
861 assert_eq!(v1, vo1.unwrap().0);
862
863 assert!(vo2.is_some());
864 assert_eq!(v2, vo2.unwrap().0);
865
866 assert!(vo3.is_some());
867 assert_eq!(v3, vo3.unwrap().0);
868
869 assert!(vo4.is_some());
870 assert_eq!(v4, vo4.unwrap().0);
871
872 assert!(vo5.is_some());
873 assert_eq!(v1, vo5.unwrap().0);
874 }
875
876 #[rstest]
877 fn serialize_and_deserialize_array_uint() {
878 let mut ser = Biseri::new();
879
880 let v: FixedArray<VarWithGivenBitSize<u16, 5>, 4> = [11.into(), 12.into(), 22.into(), 23.into()].into();
881 v.bit_serialize(&mut ser);
882 let r = ser.finish_add_data().unwrap();
883 let (bits, bytes) = (r.total_bits, r.total_bytes);
884
885 println!("bits: {}, bytes: {}", bits, bytes);
886
887 let mut des = Bides::from_biseri(&ser);
888 let vv = FixedArray::<VarWithGivenBitSize<u16, 5>, 4>::bit_deserialize(1,&mut des);
889
890 assert!(vv.is_some());
891 let vv = vv.unwrap().0;
892
893 assert_eq!(v.val[0], vv.val[0]);
894 assert_eq!(v.val[1], vv.val[1]);
895 assert_eq!(v.val[2], vv.val[2]);
896 assert_eq!(v.val[3], vv.val[3]);
897 }
898
899 #[rstest]
900 fn serialize_and_deserialize_array_bool() {
901 let mut ser = Biseri::new();
902
903 let v= FixedArray::from([true, true, false, true]);
904 v.bit_serialize(&mut ser);
905 let r = ser.finish_add_data().unwrap();
906 let (bits, bytes) = (r.total_bits, r.total_bytes);
907 println!("bits: {}, bytes: {}", bits, bytes);
908
909 let mut des = Bides::from_biseri(&ser);
910 let vv = FixedArray::<bool, 4>::bit_deserialize(1,&mut des);
911
912 assert!(vv.is_some());
913 let vv = vv.unwrap().0;
914
915 assert_eq!(v.val[0], vv.val[0]);
916 assert_eq!(v.val[1], vv.val[1]);
917 assert_eq!(v.val[2], vv.val[2]);
918 assert_eq!(v.val[3], vv.val[3]);
919 }
920 #[rstest]
921 fn serialize_and_deserialize_array_f64() {
922 let mut ser = Biseri::new();
923
924 let v = FixedArray::from([1.1, 1.2, 22.34, 123456.78]);
925 v.bit_serialize(&mut ser);
926 let r = ser.finish_add_data().unwrap();
927 let (bits, bytes) = (r.total_bits, r.total_bytes);
928 println!("bits: {}, bytes: {}", bits, bytes);
929
930 let mut des = Bides::from_biseri(&ser);
931 let vv = FixedArray::<f64, 4>::bit_deserialize(1,&mut des);
932
933 assert!(vv.is_some());
934 let vv = vv.unwrap().0;
935
936 assert_eq!(v.val[0], vv.val[0]);
937 assert_eq!(v.val[1], vv.val[1]);
938 assert_eq!(v.val[2], vv.val[2]);
939 assert_eq!(v.val[3], vv.val[3]);
940 }
941
942 #[rstest]
943 #[case::val_0(0, 1, 1)]
944 #[case::val_1(1, 5, 1)]
945 #[case::val_10(10, 9, 2)]
946 fn serialize_dyn_int_u32_3(#[case] val: u32, #[case] ex_bits: u64, #[case] ex_bytes: u64) {
947 let mut ser = Biseri::new();
954
955 let v = DynInteger::<u32, 3>::new(val);
956 v.bit_serialize(&mut ser);
957 let r = ser.finish_add_data().unwrap();
958 let (bits, bytes) = (r.total_bits, r.total_bytes);
959 println!("bits: {}, bytes: {}", bits, bytes);
960
961 assert_eq!(bits, ex_bits);
962 assert_eq!(bytes, ex_bytes);
963 }
964
965 #[rstest]
966 #[case::val_0(0, 2, 1)]
967 #[case::val_1(1, 6, 1)]
968 #[case::val_10(10, 10, 2)]
969 fn serialize_dyn_int_i32_3(#[case] val: i32, #[case] ex_bits: u64, #[case] ex_bytes: u64) {
970 let mut ser = Biseri::new();
977
978 let v = DynInteger::<i32, 3>::new(val);
979 v.bit_serialize(&mut ser);
980 let r = ser.finish_add_data().unwrap();
981 let (bits, bytes) = (r.total_bits, r.total_bytes);
982 println!("bits: {}, bytes: {}", bits, bytes);
983
984 assert_eq!(bits, ex_bits);
985 assert_eq!(bytes, ex_bytes);
986 }
987
988 #[rstest]
989 #[case::val_0(0)]
990 #[case::val_1(1)]
991 #[case::val_10(10)]
992 #[case::val_m1(-1)]
993 #[case::val_m1111(-1111)]
994 fn ser_and_deserialize_dyn_int_i32_3(#[case] val: i32) {
995 let mut ser = Biseri::new();
1000
1001 let v = DynInteger::<i32, 3>::new(val);
1002 v.bit_serialize(&mut ser);
1003 let r = ser.finish_add_data().unwrap();
1004 let (bits, bytes) = (r.total_bits, r.total_bytes);
1005 println!("bits: {}, bytes: {}", bits, bytes);
1006
1007 let mut der = Bides::from_biseri(&ser);
1008
1009 let dv = DynInteger::<i32, 3>::bit_deserialize(1, &mut der);
1010 assert!(dv.is_some());
1011
1012 let dv = dv.unwrap();
1013 assert_eq!(val, dv.0.val);
1014 }
1015
1016 #[rstest]
1017 fn ser_and_deserialize_fixed_int() {
1018 let mut ser = Biseri::new();
1019
1020 let v = VarWithGivenBitSize::<u32, 20>::new(1111);
1021 v.bit_serialize(&mut ser);
1022
1023 let r = ser.finish_add_data().unwrap();
1024 let (bits, bytes) = (r.total_bits, r.total_bytes);
1025 println!("bits: {}, bytes: {}", bits, bytes);
1026
1027 let mut der = Bides::from_biseri(&ser);
1028 let vv = VarWithGivenBitSize::<u32, 20>::bit_deserialize(1, &mut der);
1029
1030 println!("v: {:?}, vv: {:?}", v, vv);
1031 assert!(vv.is_some());
1032 let vv = vv.unwrap().0;
1033
1034 assert_eq!(v.val, vv.val);
1035 }
1036
1037 #[rstest]
1038 fn ser_and_deserialize_fixed_int_not_enough_data() {
1039 let mut ser = Biseri::new();
1040
1041 let v = VarWithGivenBitSize::<u32, 20>::new(1111);
1042 v.bit_serialize(&mut ser);
1043
1044 let r = ser.finish_add_data().unwrap();
1045 let (bits, bytes) = (r.total_bits, r.total_bytes);
1046 println!("bits: {}, bytes: {}", bits, bytes);
1047
1048 let mut der = Bides::from_biseri(&ser);
1049 der.data_cache.truncate(1);
1050 let vv = VarWithGivenBitSize::<u32, 20>::bit_deserialize(1, &mut der);
1051
1052 println!("v: {:?}, vv: {:?}", v, vv);
1053 assert!(vv.is_none());
1054 }
1055
1056 #[rstest]
1057 fn ser_and_deserialize_fixed_precision_1() {
1058 let mut ser = Biseri::new();
1059
1060 let v = FixPrecisionMinMax::<20, -50, 50>::new(12.3456);
1061 v.bit_serialize(&mut ser);
1062
1063 let r = ser.finish_add_data().unwrap();
1064 let (bits, bytes) = (r.total_bits, r.total_bytes);
1065 println!("bits: {}, bytes: {}", bits, bytes);
1066
1067 let mut der = Bides::from_biseri(&ser);
1068 let vv = FixPrecisionMinMax::<20, -50, 50>::bit_deserialize(1, &mut der);
1069
1070 println!("v: {:?}, vv: {:?}", v, vv);
1071 assert!(vv.is_some());
1072 let vv = vv.unwrap().0;
1073
1074 let eps = 1e-1;
1075 match v.val {
1076 FixPrecisionVal::Value(fpv) => {
1077 match vv.val {
1078 FixPrecisionVal::Value(fpvv) => assert!((fpv - fpvv).abs() < eps),
1079 _ => assert!(false)
1080 }
1081 },
1082 _ => assert!(false)
1083 }
1084 }
1085 #[rstest]
1086 fn ser_and_deserialize_fixed_precision_2() {
1087 let mut ser = Biseri::new();
1088
1089 let v = FixPrecisionMinMax::<20, -50, 50>::new(-12.3456);
1090 v.bit_serialize(&mut ser);
1091
1092 let r = ser.finish_add_data().unwrap();
1093 let (bits, bytes) = (r.total_bits, r.total_bytes);
1094 println!("bits: {}, bytes: {}", bits, bytes);
1095
1096 let mut der = Bides::from_biseri(&ser);
1097 let vv = FixPrecisionMinMax::<20, -50, 50>::bit_deserialize(1, &mut der);
1098
1099 println!("v: {:?}, vv: {:?}", v, vv);
1100 assert!(vv.is_some());
1101 let vv = vv.unwrap().0;
1102
1103 let eps = 1e-1;
1104 match v.val {
1105 FixPrecisionVal::Value(fpv) => {
1106 match vv.val {
1107 FixPrecisionVal::Value(fpvv) => assert!((fpv - fpvv).abs() < eps),
1108 _ => assert!(false)
1109 }
1110 },
1111 _ => assert!(false)
1112 }
1113 }
1114 #[rstest]
1115 fn ser_and_deserialize_fixed_precision_under() {
1116 let mut ser = Biseri::new();
1117
1118 let v = FixPrecisionMinMax::<10, -50, 50>::new(-60.0);
1119 v.bit_serialize(&mut ser);
1120
1121 let r = ser.finish_add_data().unwrap();
1122 let (bits, bytes) = (r.total_bits, r.total_bytes);
1123 println!("bits: {}, bytes: {}", bits, bytes);
1124
1125 let mut der = Bides::from_biseri(&ser);
1126 let vv = FixPrecisionMinMax::<10, -50, 50>::bit_deserialize(1, &mut der);
1127
1128 println!("v: {:?}, vv: {:?}", v, vv);
1129 assert!(vv.is_some());
1130 let vv = vv.unwrap().0;
1131
1132 match vv.val {
1133 FixPrecisionVal::Underflow => assert!(true),
1134 _ => assert!(false),
1135 }
1136 }
1137 #[rstest]
1138 fn ser_and_deserialize_fixed_precision_over() {
1139 let mut ser = Biseri::new();
1140
1141 let v = FixPrecisionMinMax::<10, -50, 50>::new(60.0);
1142 v.bit_serialize(&mut ser);
1143
1144 let r = ser.finish_add_data().unwrap();
1145 let (bits, bytes) = (r.total_bits, r.total_bytes);
1146 println!("bits: {}, bytes: {}", bits, bytes);
1147
1148 let mut der = Bides::from_biseri(&ser);
1149 let vv = FixPrecisionMinMax::<10, -50, 50>::bit_deserialize(1, &mut der);
1150
1151 println!("v: {:?}, vv: {:?}", v, vv);
1152 assert!(vv.is_some());
1153 let vv = vv.unwrap().0;
1154
1155 match vv.val {
1156 FixPrecisionVal::Overflow => assert!(true),
1157 _ => assert!(false)
1158 }
1159 }
1160}
1161