A fast contiguous growable array of bits allocated on the heap that allows storing and manipulating an arbitrary number of bits. This collection is backed by a Vector<u8>
which manages the underlying memory.
Getting Started
To get started add the deepmesa dependency to Cargo.toml and the use declaration in your source.
[dependencies]
deepmesa = "0.*.*"
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(128);
bv.push(true);
bv.push(false);
bv.push(true);
assert_eq!(bv.get(0), Some(true));
assert_eq!(bv.get(1), Some(false));
assert_eq!(bv.get(2), Some(true));
In addition to the new()
and with_capacity()
methods, the bitvector!
macro is also provided for convenient initialization.
use deepmesa::collections::BitVector;
use deepmesa::collections::bitvector;
let bv = bitvector![0,1,1,0];
assert_eq!(bv.len(), 4);
Memory Management
Memory is managed by an underlying Vec<u8>
and all methods operate on bytes whenever possible for efficiency. Internally the BitVector maintains a count of the number of bits currently held by the BitVector and the actual number of bytes stored maybe greater than eight times the number of bits. All bits stored past the number of active bits are zeroed out but this is not guaranteed to be true in future versions of the BitVector and should be relied up for correctness.
The BitVector can also return mutable and immutable pointers and slices to the underlying Vec<u8>
. Modifying the underlying Vector can cause undefined behavior in the BitVector.
Slices
Slices are implemented by the BitSlice
which is a view into a range within the BitVector
. A BitSlice is a wrapper around a slice of bytes with the 4 most significant bits of the slice length used to store the bit offset into the first byte of the slice. The rest of the bits of the length are used to store the length of the slice in bits.