Debian Codegraph @

src/compact.rs

raw symbols SCIP
1 //! Struct containing an u8-array of C size to store bitwise boolean values
2
3 #![deny(unsafe_code)]
4 #![warn(missing_docs)]
5
6 use core::fmt::{Display, Formatter};
7
8 use crate::Version;
9
10 /// Values to keep last X bits of a u8
11 /// `KEEP_LAST[i]` equates `(1 << i) - 1`
12 ///
13 /// # Example
14 /// ```rust
15 /// # pub const KEEP_LAST: [usize; 65] = [
16 /// # 0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383,
17 /// # 32767, 65535, 131071, 262143, 524287, 1048575, 2097151, 4194303, 8388607,
18 /// # 16777215, 33554431, 67108863, 134217727, 268435455, 536870911, 1073741823,
19 /// # 2147483647, 4294967295, 8589934591, 17179869183, 34359738367, 68719476735,
20 /// # 137438953471, 274877906943, 549755813887, 1099511627775, 2199023255551,
21 /// # 4398046511103, 8796093022207, 17592186044415, 35184372088831,
22 /// # 70368744177663, 140737488355327, 281474976710655, 562949953421311,
23 /// # 1125899906842623, 2251799813685247, 4503599627370495, 9007199254740991,
24 /// # 18014398509481983, 36028797018963967, 72057594037927935,
25 /// # 144115188075855871, 288230376151711743, 576460752303423487,
26 /// # 1152921504606846975, 2305843009213693951, 4611686018427387903,
27 /// # 9223372036854775807, 18446744073709551615,
28 /// # ];
29 /// let mut b = 0b1010_1010;
30 /// assert_eq!(b & KEEP_LAST[3], 0b010)
31 /// ```
32 #[rustfmt::skip]
33 #[cfg(not(target_arch = "wasm32"))]
34 pub const KEEP_LAST: [usize; 65] = [
35 0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383,
36 32767, 65535, 131_071, 262_143, 524_287, 1_048_575, 2_097_151, 4_194_303, 8_388_607,
37 16_777_215, 33_554_431, 67_108_863, 134_217_727, 268_435_455, 536_870_911, 1_073_741_823,
38 2_147_483_647, 4_294_967_295, 8_589_934_591, 17_179_869_183, 34_359_738_367, 68_719_476_735,
39 137_438_953_471, 274_877_906_943, 549_755_813_887, 1_099_511_627_775, 2_199_023_255_551,
40 4_398_046_511_103, 8_796_093_022_207, 17_592_186_044_415, 35_184_372_088_831,
41 70_368_744_177_663, 140_737_488_355_327, 281_474_976_710_655, 562_949_953_421_311,
42 1_125_899_906_842_623, 2_251_799_813_685_247, 4_503_599_627_370_495, 9_007_199_254_740_991,
43 18_014_398_509_481_983, 36_028_797_018_963_967, 72_057_594_037_927_935,
44 144_115_188_075_855_871, 288_230_376_151_711_743, 576_460_752_303_423_487,
45 1_152_921_504_606_846_975, 2_305_843_009_213_693_951, 4_611_686_018_427_387_903,
46 9_223_372_036_854_775_807, 18_446_744_073_709_551_615,
47 ];
48
49 /// Values to keep last X bits of a u8
50 /// `KEEP_LAST[i]` equates `(1 << i) - 1`
51 #[rustfmt::skip]
52 #[cfg(target_arch = "wasm32")]
53 pub const KEEP_LAST: [usize; 33] = [
54 0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1_023, 2_047, 4_095, 8_191, 16_383,
55 32_767, 65_535, 131_071, 262_143, 524_287, 1_048_575, 2_097_151, 4_194_303, 8_388_607,
56 16_777_215, 33_554_431, 67_108_863, 134_217_727, 268_435_455, 536_870_911, 1_073_741_823,
57 2_147_483_647, 4_294_967_295,
58 ];
59
60 /// `CompactQR` is a struct that contains a `Vec<u8>` to store boolean values as bits.
61 pub struct CompactQR {
62 pub len: usize,
63 pub data: Vec<u8>,
64 }
65
66 /// Returns a string visualization of the `CompactQR`. \
67 /// `CompactQR { len: 4, data: [0b1111_1010] }.to_string()` => `"1010"`
68 impl Display for CompactQR {
69 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
70 let mut res = String::with_capacity(self.len);
71
72 for i in 0..(self.data.capacity() / 8) {
73 let nb = self.data[i];
74 for j in 0..8 {
75 if i * 8 + j >= self.len {
76 return f.write_str(&res);
77 }
78
79 let j = 7 - j;
80 let c = if nb & (1 << j) == 0 { '0' } else { '1' };
81 res.push(c);
82 }
83 }
84
85 f.write_str(&res)
86 }
87 }
88
89 #[allow(clippy::cast_possible_truncation)]
90 impl CompactQR {
91 /// Instantiates a new `CompactQR`, should not be used, reduces performance.
92 #[allow(dead_code)]
93 pub const fn new() -> Self {
94 CompactQR {
95 len: 0,
96 data: Vec::new(),
97 }
98 }
99
100 pub fn from_version(version: Version) -> Self {
101 let len = version.max_bytes();
102 let data = vec![0; len * 8];
103
104 CompactQR { len: 0, data }
105 }
106
107 /// Instantiates a new `CompactQR`, with a given length, expects the length to be a multiple of 8.
108 #[allow(dead_code)]
109 #[cfg(test)]
110 pub fn with_len(data_length: usize) -> Self {
111 let length = data_length / 8 + usize::from(data_length % 8 != 0);
112 CompactQR {
113 len: 0,
114 data: vec![0; length],
115 }
116 }
117
118 /// Increase the length of data to specified length.
119 pub fn increase_len(&mut self, data_length: usize) {
120 if data_length / 8 >= self.data.len() {
121 self.data.resize(data_length / 8 + 1, 0);
122 }
123 }
124
125 /// Instantiates a new `CompactQR` from an already created array
126 pub fn from_array(data: &[u8], len: usize) -> Self {
127 CompactQR {
128 len,
129 data: data.to_vec(),
130 }
131 }
132
133 /// Returns `len`, length is the current number of bits / boolean values stored in the array.
134 pub const fn len(&self) -> usize {
135 self.len
136 }
137
138 /// Returns `data`, the array of bits.
139 pub const fn get_data(&self) -> &Vec<u8> {
140 &self.data
141 }
142
143 /// Pushes eight values in the `CompactQR`, if the array is not big enough, it will be resized.
144 #[inline(always)]
145 #[allow(dead_code)]
146 pub fn push_u8(&mut self, bits: u8) {
147 self.increase_len(self.len + 8);
148
149 let right = self.len % 8;
150 let first_idx = self.len / 8;
151
152 if right == 0 {
153 self.data[first_idx] = bits;
154 } else {
155 let left = 8 - right;
156 self.data[first_idx] |= (bits >> right) & (KEEP_LAST[left] as u8);
157 self.data[first_idx + 1] |= (bits & KEEP_LAST[right] as u8) << left;
158 }
159
160 self.len += 8;
161 }
162
163 /// Pushes the u8 array in the `CompactQR`, using the `push_u8` function. \
164 /// If the array is not big enough, it will be resized.
165 #[inline(always)]
166 pub fn push_u8_slice(&mut self, slice: &[u8]) {
167 self.increase_len(self.len + 8 * slice.len());
168
169 for &u in slice {
170 self.push_u8(u);
171 }
172 }
173
174 /// Pushes `len` values to the `CompactQR`. \
175 /// If the array is not big enough, it will be resized.
176 #[inline(always)]
177 pub fn push_bits(&mut self, bits: usize, len: usize) {
178 self.increase_len(self.len + len);
179
180 // Caps to max usize bits
181 let bits = bits & KEEP_LAST[len];
182
183 let rem_space = (8 - self.len % 8) % 8;
184 let first = self.len / 8;
185
186 if rem_space > len {
187 self.data[first] |= (bits << (rem_space - len)) as u8;
188 self.len += len;
189 return;
190 }
191
192 if rem_space != 0 {
193 self.data[first] |= ((bits >> (len - rem_space)) & KEEP_LAST[rem_space]) as u8;
194 self.len += rem_space;
195 }
196
197 for i in (8..=len - rem_space).rev().step_by(8) {
198 self.push_u8((bits >> (i - 8)) as u8);
199 }
200
201 let remaining = (len - rem_space) % 8;
202 if remaining == 0 {
203 return;
204 }
205
206 self.data[self.len / 8] += ((bits & KEEP_LAST[remaining]) as u8) << (8 - remaining);
207 self.len += remaining;
208 }
209
210 /// Fills the `CompactQR`'s remaining space with `[236, 17]`.
211 /// Expects the `CompactQR` `len` to be a multiple of 8.
212 #[inline(always)]
213 pub fn fill(&mut self) {
214 const PAD_BYTES: [u8; 2] = [0b1110_1100, 0b0001_0001]; //[236, 17]
215
216 #[cfg(debug_assertions)]
217 assert_eq!(self.len % 8, 0);
218
219 for (i, _) in (self.len..self.data.len()).step_by(8).enumerate() {
220 let bits = PAD_BYTES[i % 2];
221 self.push_u8(bits);
222 }
223 }
224 }