pub struct BitMap<const N: usize = DEFAULT_CHUNK_SIZE> { /* private fields */ }Expand description
A bitmap that stores data in chunks of N bytes.
§Panics
Operations panic if bit / CHUNK_SIZE_BITS > usize::MAX. On 32-bit systems
with N=32, this occurs at bit >= 1,099,511,627,776.
Implementations§
Source§impl<const N: usize> BitMap<N>
impl<const N: usize> BitMap<N>
Sourcepub const CHUNK_SIZE_BITS: u64
pub const CHUNK_SIZE_BITS: u64
The size of a chunk in bits.
pub fn with_capacity(size: u64) -> Self
Sourcepub const fn is_chunk_aligned(&self) -> bool
pub const fn is_chunk_aligned(&self) -> bool
Returns true if the bitmap length is aligned to a chunk boundary.
Sourcepub const fn get_bit_from_chunk(chunk: &[u8; N], bit: u64) -> bool
pub const fn get_bit_from_chunk(chunk: &[u8; N], bit: u64) -> bool
Get the value at the given bit from the chunk.
bit is an index into the entire bitmap, not just the chunk.
Sourcepub fn extend_to(&mut self, new_len: u64)
pub fn extend_to(&mut self, new_len: u64)
Extend the bitmap to new_len bits, filling new positions with zero.
No-op if new_len <= self.len.
Sourcepub fn truncate(&mut self, new_len: u64)
pub fn truncate(&mut self, new_len: u64)
Shrink the bitmap to new_len bits, discarding trailing bits.
§Panics
Panics if new_len > self.len().
Sourcepub fn push_chunk(&mut self, chunk: &[u8; N])
pub fn push_chunk(&mut self, chunk: &[u8; N])
Sourcepub fn count_ones(&self) -> u64
pub fn count_ones(&self) -> u64
Returns the number of bits set to 1.
Sourcepub fn count_zeros(&self) -> u64
pub fn count_zeros(&self) -> u64
Returns the number of bits set to 0.
Sourcepub fn ones_iter(&self) -> OnesIter<'_, Self, N> ⓘ
pub fn ones_iter(&self) -> OnesIter<'_, Self, N> ⓘ
Returns an iterator over the indices of set bits.
Sourcepub fn is_unset(&self, range: Range<u64>) -> bool
pub fn is_unset(&self, range: Range<u64>) -> bool
Check if all the bits in a given range are 0.
Returns true if every index in the range is unset (i.e.
Self::get returns false). Returns true if the range
is empty.
§Panics
Panics if range.end exceeds the length of the bitmap.
§Examples
use commonware_utils::bitmap::BitMap;
let mut bitmap = BitMap::<8>::zeroes(128);
assert!(bitmap.is_unset(0..128));
bitmap.set(64, true);
assert!(bitmap.is_unset(0..64));
assert!(!bitmap.is_unset(0..65));Trait Implementations§
Source§impl<const N: usize> EncodeSize for BitMap<N>
impl<const N: usize> EncodeSize for BitMap<N>
Source§fn encode_size(&self) -> usize
fn encode_size(&self) -> usize
Source§fn encode_inline_size(&self) -> usize
fn encode_inline_size(&self) -> usize
BufsMut::push
during Write::write_bufs. Used to size the working buffer for inline
writes. Override alongside Write::write_bufs for types where large
Bytes fields go via push; failing to do so will over-allocate.Source§impl<const N: usize> Read for BitMap<N>
impl<const N: usize> Read for BitMap<N>
Source§impl<const N: usize> Readable<N> for BitMap<N>
impl<const N: usize> Readable<N> for BitMap<N>
Source§fn complete_chunks(&self) -> usize
fn complete_chunks(&self) -> usize
Source§fn get_chunk(&self, chunk: usize) -> [u8; N]
fn get_chunk(&self, chunk: usize) -> [u8; N]
Source§fn pruned_chunks(&self) -> usize
fn pruned_chunks(&self) -> usize
Source§fn pruned_bits(&self) -> u64
fn pruned_bits(&self) -> u64
Source§impl<const N: usize> Write for BitMap<N>
impl<const N: usize> Write for BitMap<N>
Source§fn write_bufs(&self, buf: &mut impl BufsMut)
fn write_bufs(&self, buf: &mut impl BufsMut)
BufsMut, allowing existing Bytes chunks to be
appended via BufsMut::push instead of written inline. Must encode
to the same format as Write::write. Defaults to Write::write.