pub struct BitSet<S: BitStore = usize> { /* private fields */ }Expand description
A compact data structure for storing bits
§Examples
let mut bs = BitSet::from(0u8);
bs.set(3);
bs.set(7);
assert_eq!(bs.get(3), Some(true));
assert_eq!(bs.get(7), Some(true));
assert_eq!(bs.get(2), Some(false));Implementations§
Source§impl<S: BitStoreConst> BitSet<S>
impl<S: BitStoreConst> BitSet<S>
Source§impl<S: BitStore> BitSet<S>
impl<S: BitStore> BitSet<S>
Sourcepub fn get(&self, index: u32) -> Option<bool>
pub fn get(&self, index: u32) -> Option<bool>
Gets the value of the bit at the specified index.
§Examples
let mut bs = BitSet::from(0u8);
bs.set(3);
assert_eq!(bs.get(3), Some(true));
assert_eq!(bs[3], true);Sourcepub unsafe fn get_unchecked(&self, index: u32) -> bool
pub unsafe fn get_unchecked(&self, index: u32) -> bool
Sourcepub const fn len(&self) -> u32
pub const fn len(&self) -> u32
Returns the number of bits in the BitSet.
§Examples
let bs = BitSet::from(0u8);
assert_eq!(bs.len(), 8);
let bs = BitSet::from(0u16);
assert_eq!(bs.len(), 16);
let bs = BitSet::from([0u16; 2]);
assert_eq!(bs.len(), 32);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the BitSet is empty, i.e., all bits are unset.
§Examples
let bs = BitSet::from(0u16);
assert!(bs.is_empty());Sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Returns true if the BitSet is full, i.e., all bits are set.
§Examples
let bs = BitSet::from(!0u16);
assert!(bs.is_full());Sourcepub fn any(&self) -> bool
pub fn any(&self) -> bool
Returns true if the BitSet contains any set bits.
§Examples
let mut bs = BitSet::from(0u16);
assert!(!bs.any());
bs.set(3);
assert!(bs.any());Sourcepub const fn iter(&self) -> Bits<&S> ⓘ
pub const fn iter(&self) -> Bits<&S> ⓘ
Returns a borrowed iterator over the bits in the BitSet.
Sourcepub fn ones(&self) -> impl Iterator<Item = u32> + DoubleEndedIterator + '_
pub fn ones(&self) -> impl Iterator<Item = u32> + DoubleEndedIterator + '_
Returns an iterator over the indices of the set bits in the BitSet.
Sourcepub fn into_ones(self) -> impl Iterator<Item = u32> + DoubleEndedIterator
pub fn into_ones(self) -> impl Iterator<Item = u32> + DoubleEndedIterator
Returns an iterator over the indices of the set bits in the BitSet.
Source§impl<S: BitStoreMut> BitSet<S>
impl<S: BitStoreMut> BitSet<S>
Sourcepub fn set(&mut self, index: u32) -> Option<bool>
pub fn set(&mut self, index: u32) -> Option<bool>
Sets the bit at the specified index, and returns original value.
§Examples
let mut bs = BitSet::from(0u8);
assert_eq!(bs.set(3), Some(false));
assert_eq!(bs.get(3), Some(true));Sourcepub unsafe fn set_unchecked(&mut self, index: u32) -> bool
pub unsafe fn set_unchecked(&mut self, index: u32) -> bool
Sets the bit at the specified index without checking that the index is in bounds, and returns original value.
§Safety
Using an index that is out of bounds may lead to undefined behavior.
§Examples
let mut bs = BitSet::from(0u8);
assert_eq!(unsafe { bs.set_unchecked(3) }, false);
assert_eq!(bs.get(3), Some(true));Sourcepub fn unset(&mut self, index: u32) -> Option<bool>
pub fn unset(&mut self, index: u32) -> Option<bool>
Unsets the bit at the specified index, and returns original value.
§Examples
let mut bs = BitSet::from(0u8);
bs.negate();
assert_eq!(bs.unset(3), Some(true));
assert_eq!(bs.get(3), Some(false));Sourcepub unsafe fn unset_unchecked(&mut self, index: u32) -> bool
pub unsafe fn unset_unchecked(&mut self, index: u32) -> bool
Unsets the bit at the specified index without checking that the index is in bounds, and returns original value.
§Safety
Using an index that is out of bounds may lead to undefined behavior.
§Examples
let mut bs = BitSet::from(0u8);
bs.negate();
assert_eq!(unsafe { bs.unset_unchecked(3) }, true);
assert_eq!(bs.get(3), Some(false));Sourcepub fn change(&mut self, index: u32, value: bool) -> Option<bool>
pub fn change(&mut self, index: u32, value: bool) -> Option<bool>
Changes the bit at the specified index to value, and returns
original value.
§Examples
let mut bs = BitSet::from(0u8);
assert_eq!(bs.change(3, true), Some(false));
assert_eq!(bs.get(3), Some(true));
assert_eq!(bs.change(3, false), Some(true));
assert_eq!(bs.get(3), Some(false));Sourcepub unsafe fn change_unchecked(&mut self, index: u32, value: bool) -> bool
pub unsafe fn change_unchecked(&mut self, index: u32, value: bool) -> bool
Changes the bit at the specified index to value without checking
that the index is in bounds, and returns original value.
§Safety
Using an index that is out of bounds may lead to undefined behavior.
§Examples
let mut bs = BitSet::from(0u8);
assert_eq!(unsafe { bs.change_unchecked(3, true) }, false);
assert_eq!(bs.get(3), Some(true));
assert_eq!(unsafe { bs.change_unchecked(3, false) }, true);
assert_eq!(bs.get(3), Some(false));Sourcepub fn union_with(&mut self, other: &Self)
pub fn union_with(&mut self, other: &Self)
Performs the union of two BitSets, modifying self in place.
§Examples
let mut bs1 = BitSet::from(0u16);
let mut bs2 = BitSet::from(0o16);
bs1.set(3);
bs1.set(7);
bs2.set(7);
bs2.set(9);
bs1.union_with(&bs2);
assert_eq!(bs1.get(3), Some(true));
assert_eq!(bs1.get(7), Some(true));
assert_eq!(bs1.get(9), Some(true));Sourcepub fn intersect_with(&mut self, other: &Self)
pub fn intersect_with(&mut self, other: &Self)
Performs the intersection of two BitSets, modifying self in place.
§Examples
let mut bs1 = BitSet::from(0u16);
let mut bs2 = BitSet::from(0u16);
bs1.set(3);
bs1.set(7);
bs2.set(7);
bs2.set(9);
bs1.intersect_with(&bs2);
assert_eq!(bs1.get(3), Some(false));
assert_eq!(bs1.get(7), Some(true));
assert_eq!(bs1.get(9), Some(false));Sourcepub fn difference_with(&mut self, other: &Self)
pub fn difference_with(&mut self, other: &Self)
Performs the difference of two BitSets, modifying self in place.
§Examples
let mut bs1 = BitSet::from(0u16);
let mut bs2 = BitSet::from(0u16);
bs1.set(3);
bs1.set(7);
bs2.set(7);
bs2.set(9);
bs1.difference_with(&bs2);
assert_eq!(bs1.get(3), Some(true));
assert_eq!(bs1.get(7), Some(false));
assert_eq!(bs1.get(9), Some(false));Sourcepub fn symmetric_difference_with(&mut self, other: &Self)
pub fn symmetric_difference_with(&mut self, other: &Self)
Performs the symmetric difference of two BitSets, modifying self in place.
§Examples
let mut bs1 = BitSet::from(0u16);
let mut bs2 = BitSet::from(0u16);
bs1.set(3);
bs1.set(7);
bs2.set(7);
bs2.set(9);
bs1.symmetric_difference_with(&bs2);
assert_eq!(bs1.get(3), Some(true));
assert_eq!(bs1.get(7), Some(false));
assert_eq!(bs1.get(9), Some(true));Source§impl<S: BitStoreMut + Clone> BitSet<S>
impl<S: BitStoreMut + Clone> BitSet<S>
Sourcepub fn union(&self, other: &Self) -> Self
pub fn union(&self, other: &Self) -> Self
Performs the union of two BitSets.
§Examples
let mut bs1 = BitSet::from(0u16);
let mut bs2 = BitSet::from(0o16);
bs1.set(3);
bs1.set(7);
bs2.set(7);
bs2.set(9);
let bs3 = bs1.union(&bs2);
assert_eq!(bs3.get(3), Some(true));
assert_eq!(bs3.get(7), Some(true));
assert_eq!(bs3.get(9), Some(true));Sourcepub fn intersection(&self, other: &Self) -> Self
pub fn intersection(&self, other: &Self) -> Self
Performs the intersection of two BitSets.
§Examples
let mut bs1 = BitSet::from(0u16);
let mut bs2 = BitSet::from(0u16);
bs1.set(3);
bs1.set(7);
bs2.set(7);
bs2.set(9);
let bs3 = bs1.intersection(&bs2);
assert_eq!(bs3.get(3), Some(false));
assert_eq!(bs3.get(7), Some(true));
assert_eq!(bs3.get(9), Some(false));Sourcepub fn difference(&self, other: &Self) -> Self
pub fn difference(&self, other: &Self) -> Self
Performs the difference of two BitSets.
§Examples
let mut bs1 = BitSet::from(0u16);
let mut bs2 = BitSet::from(0u16);
bs1.set(3);
bs1.set(7);
bs2.set(7);
bs2.set(9);
let bs3 = bs1.difference(&bs2);
assert_eq!(bs3.get(3), Some(true));
assert_eq!(bs3.get(7), Some(false));
assert_eq!(bs3.get(9), Some(false));Sourcepub fn symmetric_difference(&self, other: &Self) -> Self
pub fn symmetric_difference(&self, other: &Self) -> Self
Performs the symmetric difference of two BitSets.
§Examples
let mut bs1 = BitSet::from(0u16);
let mut bs2 = BitSet::from(0u16);
bs1.set(3);
bs1.set(7);
bs2.set(7);
bs2.set(9);
let bs3 = bs1.symmetric_difference(&bs2);
assert_eq!(bs3.get(3), Some(true));
assert_eq!(bs3.get(7), Some(false));
assert_eq!(bs3.get(9), Some(true));Sourcepub fn negation(&self) -> Self
pub fn negation(&self) -> Self
Performs the negation of all the bits.
§Examples
let mut bs1 = BitSet::from(0u16);
bs1.set(3);
bs1.set(7);
let bs2 = bs1.negation();
assert_eq!(bs2.get(3), Some(false));
assert_eq!(bs2.get(7), Some(false));
assert_eq!(bs2.get(9), Some(true));Sourcepub fn is_subset(&self, other: &Self) -> bool
pub fn is_subset(&self, other: &Self) -> bool
Returns true if self is a subset of other,
i.e., every bit in self is set in other.
§Examples
let mut bs1 = BitSet::from(0u8);
let mut bs2 = BitSet::from(0u8);
bs1.set(7);
bs2.set(3);
bs2.set(7);
assert!(bs1.is_subset(&bs2));Sourcepub fn is_superset(&self, other: &Self) -> bool
pub fn is_superset(&self, other: &Self) -> bool
Returns true if self is a superset of other,
i.e., every bit in other is set in self.
§Examples
let mut bs1 = BitSet::from(0u8);
let mut bs2 = BitSet::from(0u8);
bs1.set(3);
bs1.set(7);
bs2.set(7);
assert!(bs1.is_superset(&bs2));Sourcepub fn is_disjoint(&self, other: &Self) -> bool
pub fn is_disjoint(&self, other: &Self) -> bool
Returns true if self and other have no common bits set,
i.e., their intersection is empty.
§Examples
let mut bs1 = BitSet::from(0u8);
let mut bs2 = BitSet::from(0u8);
bs1.set(3);
bs2.set(7);
assert!(bs1.is_disjoint(&bs2));Trait Implementations§
Source§impl<S: BitStoreMut> BitAndAssign for BitSet<S>
impl<S: BitStoreMut> BitAndAssign for BitSet<S>
Source§fn bitand_assign(&mut self, rhs: Self)
fn bitand_assign(&mut self, rhs: Self)
&= operation. Read moreSource§impl<S: BitStoreMut> BitOrAssign for BitSet<S>
impl<S: BitStoreMut> BitOrAssign for BitSet<S>
Source§fn bitor_assign(&mut self, rhs: Self)
fn bitor_assign(&mut self, rhs: Self)
|= operation. Read moreSource§impl<S: BitStoreMut> BitXorAssign for BitSet<S>
impl<S: BitStoreMut> BitXorAssign for BitSet<S>
Source§fn bitxor_assign(&mut self, rhs: Self)
fn bitxor_assign(&mut self, rhs: Self)
^= operation. Read moreSource§impl<S: BitStoreMut + BitStoreConst> FromIterator<u16> for BitSet<S>
impl<S: BitStoreMut + BitStoreConst> FromIterator<u16> for BitSet<S>
Source§impl<S: BitStoreMut + BitStoreConst> FromIterator<u32> for BitSet<S>
impl<S: BitStoreMut + BitStoreConst> FromIterator<u32> for BitSet<S>
Source§impl<S: BitStoreMut + BitStoreConst> FromIterator<u64> for BitSet<S>
impl<S: BitStoreMut + BitStoreConst> FromIterator<u64> for BitSet<S>
Source§impl<S: BitStoreMut + BitStoreConst> FromIterator<u8> for BitSet<S>
impl<S: BitStoreMut + BitStoreConst> FromIterator<u8> for BitSet<S>
Source§impl<S: BitStoreMut + BitStoreConst> FromIterator<usize> for BitSet<S>
impl<S: BitStoreMut + BitStoreConst> FromIterator<usize> for BitSet<S>
Source§impl<S: BitStore> IntoIterator for BitSet<S>
impl<S: BitStore> IntoIterator for BitSet<S>
Source§impl<S: Ord + BitStore> Ord for BitSet<S>
impl<S: Ord + BitStore> Ord for BitSet<S>
Source§impl<S: PartialOrd + BitStore> PartialOrd for BitSet<S>
impl<S: PartialOrd + BitStore> PartialOrd for BitSet<S>
Source§impl<S: BitStoreMut> SubAssign for BitSet<S>
impl<S: BitStoreMut> SubAssign for BitSet<S>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read more