Skip to main content

BitMap

Struct BitMap 

Source
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>

Source

pub const CHUNK_SIZE_BITS: u64

The size of a chunk in bits.

Source

pub const EMPTY_CHUNK: [u8; N]

A chunk of all 0s.

Source

pub const FULL_CHUNK: [u8; N]

A chunk of all 1s.

Source

pub const fn new() -> Self

Create a new empty bitmap.

Source

pub fn with_capacity(size: u64) -> Self

Source

pub fn zeroes(size: u64) -> Self

Create a new bitmap with size bits, with all bits set to 0.

Source

pub fn ones(size: u64) -> Self

Create a new bitmap with size bits, with all bits set to 1.

Source

pub const fn len(&self) -> u64

Return the number of bits currently stored in the bitmap.

Source

pub const fn is_empty(&self) -> bool

Returns true if the bitmap is empty.

Source

pub const fn is_chunk_aligned(&self) -> bool

Returns true if the bitmap length is aligned to a chunk boundary.

Source

pub fn max_encode_size(bits: u64) -> usize

Returns the largest encoded size of a bitmap of at most bits bits.

§Panics

Panics if the number of chunks does not fit in a usize.

Source

pub fn get(&self, bit: u64) -> bool

Get the value of the bit at the given index.

§Warning

Panics if the bit doesn’t exist.

Source

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.

Source

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.

Source

pub fn push(&mut self, bit: bool)

Add a single bit to the bitmap.

Source

pub fn pop(&mut self) -> bool

Remove and return the last bit from the bitmap.

§Warning

Panics if the bitmap is empty.

Source

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().

Source

pub fn flip(&mut self, bit: u64)

Flips the given bit.

§Panics

Panics if bit is out of bounds.

Source

pub fn flip_all(&mut self)

Flips all bits (1s become 0s and vice versa).

Source

pub fn set(&mut self, bit: u64, value: bool)

Set the value of the referenced bit.

§Warning

Panics if the bit doesn’t exist.

Source

pub fn set_all(&mut self, bit: bool)

Sets all bits to the specified value.

Source

pub fn push_chunk(&mut self, chunk: &[u8; N])

Add a chunk of bits to the bitmap.

§Warning

Panics if self.len is not chunk aligned.

Source

pub fn extend_from_bitmap<const M: usize>(&mut self, other: &BitMap<M>)

Append every bit of other (of any chunk size), in order.

Source

pub fn count_ones(&self) -> u64

Returns the number of bits set to 1.

Source

pub fn count_zeros(&self) -> u64

Returns the number of bits set to 0.

Source

pub const fn iter(&self) -> Iterator<'_, N> ⓘ

Creates an iterator over the bits.

Source

pub fn ones_iter(&self) -> OnesIter<'_, Self, N> ⓘ

Returns an iterator over the indices of set bits.

Source

pub fn and(&mut self, other: &Self)

Performs a bitwise AND with another BitMap.

§Panics

Panics if the lengths don’t match.

Source

pub fn or(&mut self, other: &Self)

Performs a bitwise OR with another BitMap.

§Panics

Panics if the lengths don’t match.

Source

pub fn xor(&mut self, other: &Self)

Performs a bitwise XOR with another BitMap.

§Panics

Panics if the lengths don’t match.

Source

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> BitAnd for &BitMap<N>

Source§

type Output = BitMap<N>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Self) -> Self::Output

Performs the & operation. Read more
Source§

impl<const N: usize> BitOr for &BitMap<N>

Source§

type Output = BitMap<N>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Self) -> Self::Output

Performs the | operation. Read more
Source§

impl<const N: usize> BitXor for &BitMap<N>

Source§

type Output = BitMap<N>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Self) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<const N: usize> Clone for BitMap<N>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const N: usize> Debug for BitMap<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const N: usize> Default for BitMap<N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<const N: usize> EncodeSize for BitMap<N>

Source§

fn encode_size(&self) -> usize

Returns the encoded size of this value (in bytes).
Source§

fn encode_inline_size(&self) -> usize

Returns the encoded size excluding bytes passed to BufsMut::push during Write::write_bufs. Used to size the fixed working buffer for inline writes, so it must not be less than the bytes written inline. Override alongside Write::write_bufs for types where large byte fields go via push, or the working buffer will over-allocate.
Source§

impl<const N: usize> Eq for BitMap<N>

Source§

impl<const N: usize> From<BitMap<N>> for Vec<bool>

Source§

fn from(bv: BitMap<N>) -> Self

Converts to this type from the input type.
Source§

impl<T: AsRef<[bool]>, const N: usize> From<T> for BitMap<N>

Source§

fn from(t: T) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize> Hash for BitMap<N>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<const N: usize> Index<u64> for BitMap<N>

Source§

fn index(&self, bit: u64) -> &Self::Output

Allows accessing bits using the [] operator.

Panics if out of bounds.

Source§

type Output = bool

The returned type after indexing.
Source§

impl<const N: usize> PartialEq for BitMap<N>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<const N: usize> Read for BitMap<N>

Source§

type Cfg = RangeCfg<u64>

Accepted range for the decoded length in bits.

Source§

fn read_cfg(buf: &mut impl Buf, range: &Self::Cfg) -> Result<Self, CodecError>

Reads a value from the buffer using the provided configuration cfg. Read more
Source§

impl<const N: usize> Readable<N> for BitMap<N>

Source§

fn complete_chunks(&self) -> usize

Return the number of complete (fully filled) chunks.
Source§

fn get_chunk(&self, chunk: usize) -> [u8; N]

Return the chunk data at the given absolute chunk index.
Source§

fn last_chunk(&self) -> ([u8; N], u64)

Return the last chunk and its size in bits.
Source§

fn pruned_chunks(&self) -> usize

Return the number of pruned chunks.
Source§

fn len(&self) -> u64

Return the total number of bits.
Source§

fn is_empty(&self) -> bool

Returns true if the bitmap is empty.
Source§

fn pruned_bits(&self) -> u64

Return the number of pruned bits (i.e. pruned chunks * bits per chunk).
Source§

fn get_bit(&self, bit: u64) -> bool

Return the value of a single bit.
Source§

fn ones_iter_from(&self, pos: u64) -> OnesIter<'_, Self, N> ⓘ
where Self: Sized,

Returns an iterator over the indices of set bits starting from pos. Read more
Source§

fn ones_iter_range(&self, range: Range<u64>) -> OnesIter<'_, Self, N> ⓘ
where Self: Sized,

Returns an iterator over the indices of set bits in range. Read more
Source§

impl<const N: usize> StructuralPartialEq for BitMap<N>

Source§

impl<const N: usize> Write for BitMap<N>

Source§

fn write(&self, buf: &mut impl BufMut)

Writes the binary representation of self to the provided buffer buf. Read more
Source§

fn write_bufs(&self, buf: &mut impl BufsMut)

Writes to a BufsMut, allowing existing byte chunks to be appended via BufsMut::push instead of written inline. Must encode to the same format as Write::write. Defaults to Write::write.

Auto Trait Implementations§

§

impl<const N: usize> Freeze for BitMap<N>
where VecDeque<[u8; N]>: Freeze,

§

impl<const N: usize> RefUnwindSafe for BitMap<N>

§

impl<const N: usize> Send for BitMap<N>
where VecDeque<[u8; N]>: Send,

§

impl<const N: usize> Sync for BitMap<N>
where VecDeque<[u8; N]>: Sync,

§

impl<const N: usize> Unpin for BitMap<N>
where VecDeque<[u8; N]>: Unpin,

§

impl<const N: usize> UnsafeUnpin for BitMap<N>
where VecDeque<[u8; N]>: UnsafeUnpin,

§

impl<const N: usize> UnwindSafe for BitMap<N>
where VecDeque<[u8; N]>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Codec for T
where T: Encode + Decode,

Source§

impl<T> CodecShared for T
where T: Codec + Send + Sync,

Source§

impl<T> Decode for T
where T: Read,

Source§

fn decode_cfg(buf: impl Input, cfg: &Self::Cfg) -> Result<Self, Error>

Decodes a value from buf using cfg, ensuring the entire buffer is consumed. Read more
Source§

impl<T> Encode for T
where T: Write + EncodeSize,

Source§

fn encode(&self) -> Bytes

Encodes self into a new Bytes buffer. Read more
Source§

fn encode_mut(&self) -> BytesMut

Encodes self into a new BytesMut buffer. Read more
Source§

impl<T> EncodeShared for T
where T: Encode + Send + Sync,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Threaded<T> for T

Source§

type Rest = ()

The outputs beyond the threaded value.
Source§

fn split(self) -> (T, ())

Splits into the threaded value and the extra outputs.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more