pub struct BitVector { /* private fields */ }
Expand description
This is a space-efficient, resizeable bitvector class. In the common case it occupies one word, but if necessary, it will inflate this one word to point to a single chunk of out-of-line allocated storage to store an arbitrary number of bits.
-
The bitvector remembers the bound of how many bits can be stored, but this may be slightly greater (by as much as some platform-specific constant) than the last argument passed to ensureSize().
-
The bitvector can resize itself automatically (set, clear, get) or can be used in a manual mode, which is faster (quick_set, quick_clear, quick_get, ensure_size).
-
Accesses
assert!
that you are within bounds. -
Bits are automatically initialized to zero.
On the other hand, this BitVector class may not be the fastest around, since it does conditionals on every get/set/clear. But it is great if you need to juggle a lot of variable-length BitVectors and you’re worried about wasting space.
Implementations§
Source§impl BitVector
impl BitVector
pub fn new() -> Self
pub fn with_capacity(num_bits: usize) -> Self
Sourcepub fn filter(&mut self, other: &Self)
pub fn filter(&mut self, other: &Self)
Filter self
by other
, keeping only the bits that are set in both, equal to bit-and.
Sourcepub fn exclude(&mut self, other: &Self)
pub fn exclude(&mut self, other: &Self)
Exclude the bits in other
from self
, equal to bit-and-not.
pub fn is_empty(&self) -> bool
Sourcepub fn find_bit(&self, index: usize, value: bool) -> usize
pub fn find_bit(&self, index: usize, value: bool) -> usize
Search after index
for the next bit with value value
, returns index
if no such bit is found.
Sourcepub fn quick_clear(&mut self, bit: usize) -> bool
pub fn quick_clear(&mut self, bit: usize) -> bool
Sourcepub fn clear(&mut self, index: usize) -> bool
pub fn clear(&mut self, index: usize) -> bool
Clear bit at index, or return false if index is out of bounds.
Sourcepub fn set(&mut self, index: usize, value: bool) -> bool
pub fn set(&mut self, index: usize, value: bool) -> bool
Set bit at index. Resizes bitvector if necessary.
Sourcepub fn ensure_size(&mut self, num_bits: usize)
pub fn ensure_size(&mut self, num_bits: usize)
Ensure that the bitvector can hold at least num_bits
bits.
Sourcepub fn shift_right_by_multiple_of_64(&mut self, shift_in_bits: usize)
pub fn shift_right_by_multiple_of_64(&mut self, shift_in_bits: usize)
Shift right by shift_in_bits
bits. Resizes bitvector if necessary.
Sourcepub fn iter(&self) -> BitVectorIter<'_> ⓘ
pub fn iter(&self) -> BitVectorIter<'_> ⓘ
Creates a new iterator over the bitvector.