pub struct BitVec { /* private fields */ }Expand description
A growable list of bits implemented using a Vec<u8>
§Examples
use probabilistic_collections::bit_vec::BitVec;
let mut bv = BitVec::from_elem(5, false);
bv.set(0, true);
bv.set(1, true);
bv.set(2, true);
assert_eq!(
bv.iter().collect::<Vec<bool>>(),
vec![true, true, true, false, false],
);
bv.set_all(true);
assert_eq!(
bv.iter().collect::<Vec<bool>>(),
vec![true, true, true, true, true],
);
bv.flip(0);
bv.flip_all();
assert_eq!(
bv.iter().collect::<Vec<bool>>(),
vec![true, false, false, false, false],
);
bv.push(true);
assert_eq!(
bv.iter().collect::<Vec<bool>>(),
vec![true, false, false, false, false, true],
);
assert_eq!(bv.pop(), Some(true));
let clone = bv.clone();
bv.flip_all();
bv.union(&clone);
assert_eq!(
bv.iter().collect::<Vec<bool>>(),
vec![true, true, true, true, true],
);Implementations§
Source§impl BitVec
impl BitVec
Sourcepub fn new(len: usize) -> Self
pub fn new(len: usize) -> Self
Constructs a new BitVec with a certain number of bits. All bits are initialized to false.
§Examples
use probabilistic_collections::bit_vec::BitVec;
let bv = BitVec::new(5);
assert_eq!(
bv.iter().collect::<Vec<bool>>(),
vec![false, false, false, false, false],
);Sourcepub fn from_elem(len: usize, bit: bool) -> Self
pub fn from_elem(len: usize, bit: bool) -> Self
Constructs a new BitVec with a certain number of bits. All bits are initialized to bit.
§Examples
use probabilistic_collections::bit_vec::BitVec;
let bv = BitVec::from_elem(5, true);
assert_eq!(
bv.iter().collect::<Vec<bool>>(),
vec![true, true, true, true, true],
);Sourcepub fn from_bytes(bytes: &[u8]) -> Self
pub fn from_bytes(bytes: &[u8]) -> Self
Constructs a BitVec from a byte slice. Each byte becomes eight bits, with the most
signficant bits of each byte coming first.
§Examples
use probabilistic_collections::bit_vec::BitVec;
let bv = BitVec::from_bytes(&[0b11010000]);
assert_eq!(
bv.iter().collect::<Vec<bool>>(),
vec![true, true, false, true, false, false, false, false],
);Sourcepub fn to_bytes(&self) -> Vec<u8> ⓘ
pub fn to_bytes(&self) -> Vec<u8> ⓘ
Returns the byte-vec representation of the BitVec with the first bit in the BitVec
becoming the high-order bit of the first byte.
§Examples
use probabilistic_collections::bit_vec::BitVec;
let mut bv = BitVec::new(8);
bv.set(0, true);
bv.set(1, true);
bv.set(3, true);
assert_eq!(bv.to_bytes(), vec![0b11010000]);Sourcepub fn with_capacity(len: usize) -> Self
pub fn with_capacity(len: usize) -> Self
Constructs a new, empty BitVec with a certain capacity.
§Examples
use probabilistic_collections::bit_vec::BitVec;
let bv = BitVec::with_capacity(5);Sourcepub fn get(&self, index: usize) -> Option<bool>
pub fn get(&self, index: usize) -> Option<bool>
Returns the value at index index, or None if index is out of bounds.
§Examples
use probabilistic_collections::bit_vec::BitVec;
let mut bv = BitVec::new(5);
bv.set(1, true);
assert_eq!(bv.get(0), Some(false));
assert_eq!(bv.get(1), Some(true));Sourcepub fn flip_all(&mut self)
pub fn flip_all(&mut self)
Flips all values in the BitVec.
§Examples
use probabilistic_collections::bit_vec::BitVec;
let mut bv = BitVec::from_elem(5, false);
bv.flip_all();
assert_eq!(
bv.iter().collect::<Vec<bool>>(),
vec![true, true, true, true, true],
);
bv.flip_all();
assert_eq!(
bv.iter().collect::<Vec<bool>>(),
vec![false, false, false, false, false],
);Sourcepub fn union(&mut self, other: &Self)
pub fn union(&mut self, other: &Self)
Sets self to the union of self and other.
§Panics
Panics if the two BitVec are of different lengths.
§Examples
use probabilistic_collections::bit_vec::BitVec;
let mut bv1 = BitVec::new(4);
bv1.set(0, true);
bv1.set(1, true);
let mut bv2 = BitVec::new(4);
bv2.set(0, true);
bv2.set(2, true);
bv1.union(&bv2);
assert_eq!(
bv1.iter().collect::<Vec<bool>>(),
vec![true, true, true, false],
);Sourcepub fn intersection(&mut self, other: &Self)
pub fn intersection(&mut self, other: &Self)
Sets self to the intersection of self and other.
§Panics
Panics if the two BitVec are of different lengths.
§Examples
use probabilistic_collections::bit_vec::BitVec;
let mut bv1 = BitVec::new(4);
bv1.set(0, true);
bv1.set(1, true);
let mut bv2 = BitVec::new(4);
bv2.set(0, true);
bv2.set(2, true);
bv1.intersection(&bv2);
assert_eq!(
bv1.iter().collect::<Vec<bool>>(),
vec![true, false, false, false],
);Sourcepub fn difference(&mut self, other: &Self)
pub fn difference(&mut self, other: &Self)
Sets self to the difference of self and other.
§Panics
Panics if the two BitVec are of different lengths.
§Examples
use probabilistic_collections::bit_vec::BitVec;
let mut bv1 = BitVec::new(4);
bv1.set(0, true);
bv1.set(1, true);
let mut bv2 = BitVec::new(4);
bv2.set(0, true);
bv2.set(2, true);
bv1.difference(&bv2);
assert_eq!(
bv1.iter().collect::<Vec<bool>>(),
vec![false, true, false, false],
);Sourcepub fn symmetric_difference(&mut self, other: &Self)
pub fn symmetric_difference(&mut self, other: &Self)
Sets self to the symmetric difference of self and other.
§Panics
Panics if the two BitVec are of different lengths.
§Examples
use probabilistic_collections::bit_vec::BitVec;
let mut bv1 = BitVec::new(4);
bv1.set(0, true);
bv1.set(1, true);
let mut bv2 = BitVec::new(4);
bv2.set(0, true);
bv2.set(2, true);
bv1.symmetric_difference(&bv2);
assert_eq!(
bv1.iter().collect::<Vec<bool>>(),
vec![false, true, true, false],
);Sourcepub fn truncate(&mut self, len: usize)
pub fn truncate(&mut self, len: usize)
Truncates a BitVec, dropping excess elements.
§Examples
use probabilistic_collections::bit_vec::BitVec;
let mut bv = BitVec::from_elem(5, false);
bv.truncate(2);
assert_eq!(bv.iter().collect::<Vec<bool>>(), vec![false, false]);Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more bits to be inserted in the given
BitVec.
§Examples
use probabilistic_collections::bit_vec::BitVec;
let mut bv = BitVec::from_elem(5, false);
bv.reserve(10);
assert_eq!(bv.len(), 5);
assert!(bv.capacity() >= 16);Sourcepub fn reserve_exact(&mut self, additional: usize)
pub fn reserve_exact(&mut self, additional: usize)
Reserves capacity for exactly additional more bits to be inserted in the given
BitVec.
§Examples
use probabilistic_collections::bit_vec::BitVec;
let mut bv = BitVec::from_elem(5, false);
bv.reserve_exact(10);
assert_eq!(bv.len(), 5);
assert_eq!(bv.capacity(), 16);Sourcepub fn pop(&mut self) -> Option<bool>
pub fn pop(&mut self) -> Option<bool>
Returns and removes the last element of the BitVec. Returns None if the BitVec is
empty.
§Examples
use probabilistic_collections::bit_vec::BitVec;
let mut bv = BitVec::from_elem(1, false);
assert_eq!(bv.pop(), Some(false));
assert_eq!(bv.pop(), None);Sourcepub fn push(&mut self, bit: bool)
pub fn push(&mut self, bit: bool)
Pushes an element into the BitVec.
§Examples
use probabilistic_collections::bit_vec::BitVec;
let mut bv = BitVec::from_elem(1, false);
bv.push(true);
assert_eq!(bv.get(1), Some(true));Sourcepub fn iter(&self) -> BitVecIter<'_> ⓘ
pub fn iter(&self) -> BitVecIter<'_> ⓘ
Returns an iterator over the elements of the vector in order.
§Examples
use probabilistic_collections::bit_vec::BitVec;
let mut bv = BitVec::from_elem(1, false);
bv.push(true);
assert_eq!(bv.iter().collect::<Vec<bool>>(), vec![false, true]);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the BitVec is empty.
§Examples
use probabilistic_collections::bit_vec::BitVec;
let mut bv = BitVec::from_elem(1, false);
assert!(!bv.is_empty());
bv.pop();
assert!(bv.is_empty());Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the BitVec.
§Examples
use probabilistic_collections::bit_vec::BitVec;
let mut bv = BitVec::from_elem(1, false);
assert_eq!(bv.len(), 1);
bv.pop();
assert_eq!(bv.len(), 0);Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the capacity of the BitVec.
§Examples
use probabilistic_collections::bit_vec::BitVec;
let mut bv = BitVec::new(0);
bv.reserve_exact(10);
assert_eq!(bv.capacity(), 16);Sourcepub fn count_ones(&self) -> usize
pub fn count_ones(&self) -> usize
Returns the number of set bits in the BitVec.
§Examples
use probabilistic_collections::bit_vec::BitVec;
let bv = BitVec::from_bytes(&[0b11010000]);
assert_eq!(bv.count_ones(), 3);Sourcepub fn count_zeros(&self) -> usize
pub fn count_zeros(&self) -> usize
Returns the number of unset bits in the BitVec.
§Examples
use probabilistic_collections::bit_vec::BitVec;
let bv = BitVec::from_bytes(&[0b11010000]);
assert_eq!(bv.count_zeros(), 5);