use alloc::vec::Vec;
#[derive(Debug, Clone)]
pub(crate) struct BitVec {
bits: Vec<u8>,
}
const BITS_PER_BYTE: usize = 8;
impl BitVec {
#[inline]
pub fn new(bytes: usize) -> Self {
Self {
bits: vec![0u8; bytes],
}
}
#[inline]
pub fn len(&self) -> usize {
self.bits.len()
}
#[inline]
pub fn contain(&self, bit_offset: usize) -> bool {
let byte_offset = bit_offset / BITS_PER_BYTE;
let bit_shift = bit_offset % BITS_PER_BYTE;
debug_assert!(byte_offset < self.bits.len(), "bit_offset out of bounds");
(self.bits()[byte_offset] & (1 << bit_shift)) != 0
}
#[inline]
pub fn set(&mut self, bit_offset: usize) {
let byte_offset = bit_offset / BITS_PER_BYTE;
let bit_shift = bit_offset % BITS_PER_BYTE;
debug_assert!(byte_offset < self.bits.len(), "bit_offset out of bounds");
self.bits_mut()[byte_offset] |= 1 << bit_shift;
}
#[inline]
fn bits(&self) -> &[u8] {
&self.bits
}
#[inline]
fn bits_mut(&mut self) -> &mut [u8] {
&mut self.bits
}
#[inline]
pub fn capacity_in_bits(&self) -> usize {
self.len() * BITS_PER_BYTE
}
#[inline]
pub fn clear(&mut self) {
for byte in &mut self.bits {
*byte = 0;
}
}
}