pub struct BitVec { /* private fields */ }Expand description
BitVec represents a bit vector that supports 4 operations:
- Marking a position as set.
- Checking if a position is set.
- Count set bits.
- Get the index of the last set bit.
Internally, it stores a vector of u8’s (as Vec
- The first 8 positions of the bit vector are encoded in the first element of the vector, the next 8 are encoded in the second element, and so on.
- Bits are read from left to right. For instance, in the following bitvec [0b0001_0000, 0b0000_0000, 0b0000_0000, 0b0000_0001], the 3rd and 31st positions are set.
- Each bit of a u8 is set to 1 if the position is set and to 0 if it’s not.
- We only allow setting positions upto u16::MAX. As a result, the size of the inner vector is limited to 8192 (= 65536 / 8).
- Once a bit has been set, it cannot be unset. As a result, the inner vector cannot shrink.
- The positions can be set in any order.
- A position can set more than once – it remains set after the first time.
§Examples:
use aptos_bitvec::BitVec;
use std::ops::BitAnd;
let mut bv = BitVec::default();
bv.set(2);
bv.set(5);
assert!(bv.is_set(2));
assert!(bv.is_set(5));
assert_eq!(false, bv.is_set(0));
assert_eq!(bv.count_ones(), 2);
assert_eq!(bv.last_set_bit(), Some(5));
// A bitwise AND of BitVec can be performed by using the `&` operator.
let mut bv1 = BitVec::default();
bv1.set(2);
bv1.set(3);
let mut bv2 = BitVec::default();
bv2.set(2);
let intersection = bv1.bitand(&bv2);
assert!(intersection.is_set(2));
assert_eq!(false, intersection.is_set(3));Implementations§
Source§impl BitVec
impl BitVec
Sourcepub fn with_num_bits(num_bits: u16) -> Self
pub fn with_num_bits(num_bits: u16) -> Self
Initialize with buckets that can fit in num_bits.
Sourcepub fn count_ones(&self) -> u32
pub fn count_ones(&self) -> u32
Returns the number of set bits.
Sourcepub fn last_set_bit(&self) -> Option<u16>
pub fn last_set_bit(&self) -> Option<u16>
Returns the index of the last set bit.
Sourcepub fn iter_ones(&self) -> impl Iterator<Item = usize> + '_
pub fn iter_ones(&self) -> impl Iterator<Item = usize> + '_
Return an Iterator over all ‘1’ bit indexes.
Sourcepub fn num_buckets(&self) -> usize
pub fn num_buckets(&self) -> usize
Return the number of buckets.
Sourcepub fn required_buckets(num_bits: u16) -> usize
pub fn required_buckets(num_bits: u16) -> usize
Number of buckets require for num_bits.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for BitVec
impl<'de> Deserialize<'de> for BitVec
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl FromIterator<u8> for BitVec
impl FromIterator<u8> for BitVec
impl Eq for BitVec
impl StructuralPartialEq for BitVec
Auto Trait Implementations§
impl Freeze for BitVec
impl RefUnwindSafe for BitVec
impl Send for BitVec
impl Sync for BitVec
impl Unpin for BitVec
impl UnwindSafe for BitVec
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more