Struct BitSet
pub struct BitSet { /* private fields */ }Expand description
A fixed-bound dense set of indices in 0..self.bit_len().
Implementations§
§impl BitSet
impl BitSet
pub fn new(bits: usize) -> BitSet
pub fn new(bits: usize) -> BitSet
Creates an empty set with bits addressable indices, allocating its storage.
pub fn bit_len(&self) -> usize
pub fn bit_len(&self) -> usize
Returns the exclusive index bound, not the number of set bits (Self::count).
use ferravex_engine::bitset::BitSet;
let set = BitSet::new(65);
assert_eq!(set.bit_len(), 65);
assert_eq!(set.count(), 0);
assert_eq!(BitSet::new(0).bit_len(), 0);pub fn set(&mut self, index: usize, value: bool) -> bool
pub fn set(&mut self, index: usize, value: bool) -> bool
Sets or clears an index. Returns false without changing the set if out of bounds.
A valid index returns true even when its value is unchanged.
pub fn contains(&self, index: usize) -> bool
pub fn contains(&self, index: usize) -> bool
Tests membership, returning false for an out-of-bounds index.
pub fn clear(&mut self)
pub fn clear(&mut self)
Clears every bit while retaining the bound and allocated storage.
pub fn union_with(&mut self, other: &BitSet) -> bool
pub fn union_with(&mut self, other: &BitSet) -> bool
Adds every bit in other without allocating.
Returns true for equal bit bounds, including an unchanged result, or
false for unequal bounds without modifying self.
use ferravex_engine::bitset::BitSet;
let mut a = BitSet::new(65);
let mut b = BitSet::new(65);
a.set(0, true);
b.set(64, true);
assert!(a.union_with(&b));
assert!(a.iter().eq([0, 64]));
assert!(!a.union_with(&BitSet::new(66)));
assert!(a.iter().eq([0, 64]));pub fn intersection_with(&mut self, other: &BitSet) -> bool
pub fn intersection_with(&mut self, other: &BitSet) -> bool
Keeps only bits also in other, without allocating.
Returns true for equal bit bounds, including an unchanged result, or
false for unequal bounds without modifying self.
use ferravex_engine::bitset::BitSet;
let mut a = BitSet::new(65);
let mut b = BitSet::new(65);
a.set(0, true);
a.set(64, true);
b.set(64, true);
assert!(a.intersection_with(&b));
assert!(a.iter().eq([64]));
assert!(!a.intersection_with(&BitSet::new(64)));
assert!(a.iter().eq([64]));pub fn difference_with(&mut self, other: &BitSet) -> bool
pub fn difference_with(&mut self, other: &BitSet) -> bool
Removes every bit in other from self, without allocating.
Returns true for equal bit bounds, including an unchanged result, or
false for unequal bounds without modifying self. Operand order matters.
use ferravex_engine::bitset::BitSet;
let mut a = BitSet::new(65);
let mut b = BitSet::new(65);
a.set(0, true);
a.set(64, true);
b.set(64, true);
assert!(a.difference_with(&b));
assert!(a.iter().eq([0]));
assert!(!a.difference_with(&BitSet::new(66)));
assert!(a.iter().eq([0]));pub fn union<'a>(
&'a self,
other: &'a BitSet,
) -> Option<impl Iterator<Item = usize> + 'a>
pub fn union<'a>( &'a self, other: &'a BitSet, ) -> Option<impl Iterator<Item = usize> + 'a>
Lazily yields indices present in either set, once each in ascending order.
Returns None if bit bounds differ. Borrows both sets without modifying
or allocating storage; partially consuming the iterator has no side effects.
use ferravex_engine::bitset::BitSet;
let mut a = BitSet::new(65);
let mut b = BitSet::new(65);
a.set(0, true);
a.set(64, true);
b.set(64, true);
assert!(a.union(&b).unwrap().eq([0, 64]));
assert!(a.union(&BitSet::new(66)).is_none());pub fn intersection<'a>(
&'a self,
other: &'a BitSet,
) -> Option<impl Iterator<Item = usize> + 'a>
pub fn intersection<'a>( &'a self, other: &'a BitSet, ) -> Option<impl Iterator<Item = usize> + 'a>
Lazily yields indices present in both sets in ascending order.
Returns None if bit bounds differ. Borrows both sets without modifying
or allocating storage; partially consuming the iterator has no side effects.
use ferravex_engine::bitset::BitSet;
let mut a = BitSet::new(65);
let mut b = BitSet::new(65);
a.set(0, true);
a.set(64, true);
b.set(64, true);
assert!(a.intersection(&b).unwrap().eq([64]));
assert!(a.intersection(&BitSet::new(66)).is_none());pub fn difference<'a>(
&'a self,
other: &'a BitSet,
) -> Option<impl Iterator<Item = usize> + 'a>
pub fn difference<'a>( &'a self, other: &'a BitSet, ) -> Option<impl Iterator<Item = usize> + 'a>
Lazily yields indices in self but absent from other in ascending order.
Returns None if bit bounds differ. Borrows both sets without modifying
or allocating storage; partially consuming the iterator has no side effects.
Operand order matters.
use ferravex_engine::bitset::BitSet;
let mut a = BitSet::new(65);
let mut b = BitSet::new(65);
a.set(0, true);
a.set(64, true);
b.set(64, true);
assert!(a.difference(&b).unwrap().eq([0]));
assert!(b.difference(&a).unwrap().next().is_none());
assert!(a.difference(&BitSet::new(66)).is_none());pub fn iter(&self) -> impl Iterator<Item = usize>
pub fn iter(&self) -> impl Iterator<Item = usize>
Yields set indices in ascending order without allocating or changing the set. Empty words are skipped, and unused trailing bits are never yielded.
use ferravex_engine::bitset::BitSet;
let mut set = BitSet::new(65);
set.set(64, true);
set.set(0, true);
assert!(set.iter().eq([0, 64]));
assert_eq!(set.count(), 2);