Struct BitSet

pub struct BitSet { /* private fields */ }
Expand description

A fixed-bound dense set of indices in 0..self.bit_len().

Implementations§

§

impl BitSet

pub fn new(bits: usize) -> BitSet

Creates an empty set with bits addressable indices, allocating its storage.

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

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

Tests membership, returning false for an out-of-bounds index.

pub fn count(&self) -> usize

Counts set bits, excluding unused trailing bits.

pub fn clear(&mut self)

Clears every bit while retaining the bound and allocated storage.

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

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

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>

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>

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>

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>

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);

Trait Implementations§

§

impl Clone for BitSet

§

fn clone(&self) -> BitSet

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for BitSet

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl PartialEq for BitSet

§

fn eq(&self, other: &BitSet) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl Eq for BitSet

§

impl StructuralPartialEq for BitSet

Auto Trait Implementations§

§

impl Freeze for BitSet

§

impl RefUnwindSafe for BitSet

§

impl Send for BitSet

§

impl Sync for BitSet

§

impl Unpin for BitSet

§

impl UnwindSafe for BitSet

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.