use super::BitChunk;
#[derive(Debug)]
pub struct BitChunksExactMut<'a, T: BitChunk> {
chunks: std::slice::ChunksExactMut<'a, u8>,
remainder: &'a mut [u8],
remainder_len: usize,
marker: std::marker::PhantomData<T>,
}
impl<'a, T: BitChunk> BitChunksExactMut<'a, T> {
#[inline]
pub fn new(bitmap: &'a mut [u8], length: usize) -> Self {
assert!(length <= bitmap.len() * 8);
let size_of = std::mem::size_of::<T>();
let bitmap = &mut bitmap[..length.saturating_add(7) / 8];
let split = (length / 8 / size_of) * size_of;
let (chunks, remainder) = bitmap.split_at_mut(split);
let remainder_len = length - chunks.len() * 8;
let chunks = chunks.chunks_exact_mut(size_of);
Self {
chunks,
remainder,
remainder_len,
marker: std::marker::PhantomData,
}
}
#[inline]
pub fn remainder(&mut self) -> &mut [u8] {
self.remainder
}
#[inline]
pub fn remainder_len(&mut self) -> usize {
self.remainder_len
}
}
impl<'a, T: BitChunk> Iterator for BitChunksExactMut<'a, T> {
type Item = &'a mut [u8];
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.chunks.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.chunks.size_hint()
}
}