1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
#[derive(Debug)] pub struct BitWriter { out: Vec<u8>, // output buffer cache: u8, // unwritten bits are stored here bits: u8, // number of unwritten bits in cache } impl BitWriter { #[inline] pub fn new() -> Self { BitWriter { out: Vec::new(), cache: 0, bits: 0, } } #[inline] pub fn with_capacity(n: usize) -> Self { BitWriter { out: Vec::with_capacity(n), cache: 0, bits: 0, } } #[inline] pub fn reserve(&mut self, n: usize) { self.out.reserve(n / 8); } #[inline] fn push_unaligned(&mut self, b: u8) { self.out.push(self.cache | (b >> self.bits)); self.cache = (b & ((1 << self.bits) - 1)) << (8 - self.bits); } #[inline] fn reset(&mut self) { self.cache = 0; self.bits = 0; } /// ```rust /// use treeid::bitter::*; /// /// let mut w = BitWriter::new(); /// w.push(0xef); /// w.push(0x4d); /// assert_eq!(&[0xef, 0x4d], w.align_ref()); /// ``` #[inline] pub fn push(&mut self, b: u8) { // self.bits will be the same after writing 8 bits, // so we don't need to update that. if self.bits == 0 { self.out.push(b); } else { self.push_unaligned(b); } } /// ```rust /// use treeid::bitter::*; /// /// let mut w = BitWriter::new(); /// w.push_bytes(&[0xef, 0x4d]); /// assert_eq!(&[0xef, 0x4d], w.align_ref()); /// ``` #[inline] pub fn push_bytes(&mut self, p: &[u8]) { // self.bits will be the same after writing 8 bits, // so we don't need to update that. if self.bits == 0 { self.out.extend_from_slice(p); return; } for &b in p { self.push_unaligned(b); } } /// ```rust /// use treeid::bitter::*; /// /// let mut w = BitWriter::new(); /// w.push_bit(true); /// w.push_bit(true); /// w.push_bit(true); /// w.push_bit(false); /// w.push_bit(true); /// w.push_bit(true); /// w.push_bit(true); /// w.push_bit(true); /// /// w.push_bit(false); /// w.push_bit(true); /// w.push_bit(false); /// w.push_bit(false); /// w.push_bit(true); /// w.push_bit(true); /// w.push_bit(false); /// w.push_bit(true); /// assert_eq!(&[0xef, 0x4d], w.align_ref()); /// ``` #[inline] pub fn push_bit(&mut self, b: bool) { if self.bits == 7 { if b { self.out.push(self.cache | 1); } else { self.out.push(self.cache); } self.reset(); return; } self.bits += 1; if b { self.cache |= 1 << (8 - self.bits); } } /// ```rust /// use treeid::bitter::*; /// /// let mut w = BitWriter::new(); /// w.push_bits(0x08, 4); /// w.push_bits(0x07, 3); /// w.push_bits(0x05, 3); /// w.push_bits(0x15, 6); /// assert_eq!(&[0x8f, 0x55], w.align_ref()); /// ``` /// /// ```rust /// use treeid::bitter::*; /// /// let mut w = BitWriter::new(); /// /// // 2 or 0b10 /// let x: u64 = 2; /// let nz = x.leading_zeros(); // 62 /// let nd = 63u32.saturating_sub(nz); // 1 /// w.push_bits(std::u64::MAX, nd as u8); /// //assert_eq!(&[0b10000000], &w.as_ref()); /// //w.push_bit(false); /// w.push_bit(false); /// //assert_eq!(&[0b10000000], &w.as_ref()); /// w.push_bits(x, nd as u8); /// // assert_eq!(&[0b10000000], &w.as_ref()); /// /// // 1 or 0b1 /// w.push_bit(true); /// // assert_eq!(&[0b10010000], &w.align_ref()); /// /// // 4 or 0b100 /// let x: u64 = 4; /// let nz = x.leading_zeros(); // 61 /// let nd = 63u32.saturating_sub(nz); // 2 /// w.push_bits(std::u64::MAX, nd as u8); /// // assert_eq!(&[0b10011100], &w.align_ref()); /// w.push_bit(false); /// // assert_eq!(&[0b10011100], &w.align_ref()); /// w.push_bits(x, nd as u8); /// println!("{:?}", w); /// assert_eq!(&[0b10011100, 0b00000000], &w.align_ref()); /// println!("{:?}", w); /// ``` #[inline] pub fn push_bits(&mut self, r: u64, n: u8) { if n == 0 { return; } let mut n = n; // the bits higher than n are read as well, // so they must be zeroed. let r = (r << 64 - n) >> 64 - n; let new_bits = self.bits + n; if new_bits < 8 { // r fits into cache, no write will occur to out self.cache |= (r as u8) << (8 - new_bits); self.bits = new_bits; return; } if new_bits > 8 { // cache will be filled, and there will be more bits to write // fill cache and write it out let free = 8 - self.bits; self.out.push(self.cache | ((r >> (n - free)) as u8)); n -= free; // write out whole bytes while n >= 8 { n -= 8; // no need to mask r, converting to u8 will mask // out higher bits self.out.push((r >> n) as u8); } // put remaining into cache if n > 0 { // note: n < 8 (in case of n=8, 1<<n would overflow u8) self.cache = ((r as u8) & ((1 << n) - 1)) << (8 - n); self.bits = n; } else { self.reset(); } return; } // cache will be filled exactly with the bits to be written self.out.push(self.cache | r as u8); self.reset(); } /// ```rust /// use treeid::bitter::*; /// /// let mut w = BitWriter::new(); /// w.push(0xc1); /// w.push_bit(false); /// w.push_bits(0x3f, 6); /// w.push_bit(true); /// w.push(0xac); /// w.push_bits(0x01, 1); /// w.push_bits(0x1248f, 20); /// assert_eq!(3, w.align()); /// w.push_bytes(&[0x01, 0x02]); /// w.push_bits(0x0f, 4); /// w.push_bytes(&[0x80, 0x8f]); /// assert_eq!(4, w.align()); /// assert_eq!(0, w.align()); /// w.push_bits(0x01, 1); /// w.push(0xff); /// assert_eq!( /// &[0xc1, 0x7f, 0xac, 0x89, 0x24, 0x78, 0x01, 0x02, 0xf8, 0x08, 0xf0, 0xff, 0x80], /// w.align_ref(), /// ); /// ``` #[inline] pub fn align(&mut self) -> u8 { let mut skipped: u8 = 0; if self.bits > 0 { self.out.push(self.cache); skipped = 8 - self.bits; self.reset(); } skipped } /// Not safe to call unless aligned (!) /// /// ```rust /// use treeid::bitter::*; /// /// let mut w = BitWriter::new(); /// w.push(1); /// assert_eq!(&[1], w.align_ref()); /// w.trailing_pad(4); /// assert_eq!(&[1, 0, 0, 0], w.align_ref()); /// ``` #[inline] pub fn trailing_pad(&mut self, boundary: usize) -> usize { let pad_bytes = self.out.len() % boundary; if pad_bytes > 0 { for _ in 0..boundary - pad_bytes { self.out.push(0x0); } } pad_bytes } #[inline] pub fn to_vec(self) -> Vec<u8> { self.out } #[inline] pub fn align_vec(mut self) -> Vec<u8> { self.align(); self.out } #[inline] pub fn align_ref(&mut self) -> &[u8] { self.align(); &self.out } } impl AsRef<[u8]> for BitWriter { #[inline] fn as_ref(&self) -> &[u8] { &self.out } }