#[derive(Debug, Default)]
pub struct BitBuffer {
pub data: Vec<u8>,
bit_count: usize,
}
impl BitBuffer {
pub fn new() -> Self {
Self {
data: Vec::new(),
bit_count: 0,
}
}
pub fn push_bits(&mut self, val: u32, num_bits: usize) {
for i in (0..num_bits).rev() {
let bit = ((val >> i) & 1) as u8;
if self.bit_count % 8 == 0 {
self.data.push(0);
}
let byte_idx = self.data.len() - 1;
let bit_idx = 7 - (self.bit_count % 8);
self.data[byte_idx] |= bit << bit_idx;
self.bit_count += 1;
}
}
pub fn len_bits(&self) -> usize {
self.bit_count
}
pub fn to_bytes(&self) -> Vec<u8> {
self.data.clone()
}
}