Struct diem_bitvec::BitVec [−][src]
pub struct BitVec { /* fields omitted */ }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;
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
Returns the number of set bits.
Returns the index of the last set bit.
Trait Implementations
type Parameters = ()
type Parameters = ()
The type of parameters that arbitrary_with accepts for configuration
of the generated Strategy. Parameters must implement Default. Read more
Deserialize this value from the given Serde deserializer. Read more
Creates a value from an iterator. Read more
Auto Trait Implementations
impl RefUnwindSafe for BitVec
impl UnwindSafe for BitVec
Blanket Implementations
Mutably borrows from an owned value. Read more