Struct diem_bitvec::BitVec
source · 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 u8::MAX. As a result, the size of the inner vector is limited to 32 (= 256 / 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 diem_bitvec::BitVec;
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 & bv2;
assert!(intersection.is_set(2));
assert_eq!(false, intersection.is_set(3));
Implementations§
source§impl BitVec
impl BitVec
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<u8>
pub fn last_set_bit(&self) -> Option<u8>
Returns the index of the last set bit.
Trait Implementations§
source§impl Arbitrary for BitVec
impl Arbitrary for BitVec
§type Parameters = <Vec<u8, Global> as Arbitrary>::Parameters
type Parameters = <Vec<u8, Global> as Arbitrary>::Parameters
The type of parameters that
arbitrary_with
accepts for configuration
of the generated Strategy
. Parameters must implement Default
.§type Strategy = Map<<Vec<u8, Global> as Arbitrary>::Strategy, fn(_: Vec<u8, Global>) -> BitVec>
type Strategy = Map<<Vec<u8, Global> as Arbitrary>::Strategy, fn(_: Vec<u8, Global>) -> BitVec>
The type of
Strategy
used to generate values of type Self
.source§fn arbitrary_with(_top: Self::Parameters) -> Self::Strategy
fn arbitrary_with(_top: Self::Parameters) -> Self::Strategy
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