#[repr(transparent)]pub struct BitSet<T, const N: usize> { /* private fields */ }Expand description
The bit set itself.
This wrapper is #![repr(transparent)] and guaranteed to have the same memory
representation as the inner bit array
Panics
All non-try functions taking a bit parameter panics if the bit is bigger
than the capacity of the set. For non-panicking versions, use try_.
Implementations
sourceimpl<T, const N: usize> BitSet<T, N>
impl<T, const N: usize> BitSet<T, N>
sourcepub fn into_inner(self) -> [T; N]
pub fn into_inner(self) -> [T; N]
Return the inner integer array.
Examples
use rbitset::BitSet8;
let set = BitSet8::from_iter([1u8, 2, 3]);
assert_eq!(set.into_inner(), [0b00001110]);sourcepub const fn capacity() -> usize
pub const fn capacity() -> usize
Returns the capacity of the set, in other words how many bits it can hold.
This function may very well overflow if the size or length is too big, but if you’re making that big allocations you probably got bigger things to worry about.
Examples
use rbitset::BitSet;
let capacity = BitSet::<u32, 3>::capacity();
assert_eq!(capacity, 32 * 3);sourceimpl<T: PrimInt, const N: usize> BitSet<T, N>
impl<T: PrimInt, const N: usize> BitSet<T, N>
sourcepub fn from_ref(inner: &mut [T; N]) -> &mut Self
pub fn from_ref(inner: &mut [T; N]) -> &mut Self
Transmutes a reference to a borrowed bit array to a borrowed BitSet with the same lifetime.
Examples
use rbitset::BitSet;
let mut raw = [0b00001110, 0u8];
let set = BitSet::from_ref(&mut raw);
assert!(set.contains(1));
assert!(set.contains(2));
assert!(set.contains(3));sourcepub fn try_append<U, const M: usize>(
&mut self,
other: &mut BitSet<U, M>
) -> Result<(), BitSetError> where
U: PrimInt,
pub fn try_append<U, const M: usize>(
&mut self,
other: &mut BitSet<U, M>
) -> Result<(), BitSetError> where
U: PrimInt,
Tries to move all elements from other into self, leaving other empty.
Examples
use rbitset::BitSet16;
let mut a = BitSet16::new();
a.insert(1);
a.insert(2);
a.insert(3);
let mut b = BitSet16::new();
b.insert(3);
b.insert(4);
b.insert(5);
a.try_append(&mut b).expect("An error ocurred");
assert_eq!(a.len(), 5);
assert_eq!(b.len(), 0);
assert!(a.contains(1));
assert!(a.contains(2));
assert!(a.contains(3));
assert!(a.contains(4));
assert!(a.contains(5));sourcepub fn try_insert(&mut self, bit: usize) -> Result<bool, BitSetError>
pub fn try_insert(&mut self, bit: usize) -> Result<bool, BitSetError>
Tries to add a value to the set.
If the set did not have this value present, true is returned.
If the set did have this value present, false is returned.
Examples
use rbitset::{BitSet16, BitSetError};
let mut set = BitSet16::new();
assert_eq!(set.try_insert(2), Ok(true));
assert_eq!(set.try_insert(2), Ok(false));
assert_eq!(set.try_insert(16), Err(BitSetError::BiggerThanCapacity));sourcepub fn try_remove(&mut self, bit: usize) -> Result<bool, BitSetError>
pub fn try_remove(&mut self, bit: usize) -> Result<bool, BitSetError>
Removes a value from the set. Returns whether the value was present in the set.
If the bit is already disabled this is a no-op.
Examples
use rbitset::BitSet8;
let mut set = BitSet8::new();
set.insert(2);
assert_eq!(set.remove(2), true);
assert_eq!(set.remove(2), false);sourcepub fn append<U, const M: usize>(&mut self, other: &mut BitSet<U, M>) where
U: PrimInt,
pub fn append<U, const M: usize>(&mut self, other: &mut BitSet<U, M>) where
U: PrimInt,
Move all elements from other into self, leaving other empty.
Panics
This function may panic if other contains activated bits bigger than what self capacity.
Examples
use rbitset::BitSet16;
let mut a = BitSet16::new();
a.insert(1);
a.insert(2);
a.insert(3);
let mut b = BitSet16::new();
b.insert(3);
b.insert(4);
b.insert(5);
a.append(&mut b);
assert_eq!(a.len(), 5);
assert_eq!(b.len(), 0);
assert!(a.contains(1));
assert!(a.contains(2));
assert!(a.contains(3));
assert!(a.contains(4));
assert!(a.contains(5));sourcepub fn insert(&mut self, bit: usize) -> bool
pub fn insert(&mut self, bit: usize) -> bool
Adds a value to the set.
If the set did not have this value present, true is returned.
If the set did have this value present, false is returned.
Panics
This function may panic if bit value trying to be inserted is bigger than the
capacity of the BitSet. Check try_insert
for a non-panicking version
Examples
use rbitset::BitSet16;
let mut set = BitSet16::new();
assert_eq!(set.insert(2), true);
assert_eq!(set.insert(2), false);
assert_eq!(set.len(), 1);sourcepub fn remove(&mut self, bit: usize) -> bool
pub fn remove(&mut self, bit: usize) -> bool
Removes a value from the set. Returns whether the value was present in the set.
If the bit is already disabled this is a no-op.
Panics
This function may panic if bit value trying to be removed is bigger than the
capacity of the BitSet. Check try_remove
for a non-panicking version
Examples
use rbitset::BitSet8;
let mut set = BitSet8::new();
set.insert(2);
assert_eq!(set.remove(2), true);
assert_eq!(set.remove(2), false);sourcepub fn retain<F>(&mut self, f: F) where
F: FnMut(usize) -> bool,
pub fn retain<F>(&mut self, f: F) where
F: FnMut(usize) -> bool,
Retains only the elements specified by the predicate.
In other words, remove all elements e for which f(&e) returns false.
The elements are visited in ascending order.
Examples
use rbitset::BitSet16;
let mut set = BitSet16::from_iter([1u8, 2, 3, 4, 5, 6]);
// Keep only the even numbers.
set.retain(|k| k % 2 == 0);
let res = BitSet16::from_iter([2u8, 4, 6]);
assert_eq!(set, res);sourcepub fn contains(&self, bit: usize) -> bool
pub fn contains(&self, bit: usize) -> bool
Returns true if the specified bit is enabled, in other words, if the set contains a
value.
Examples
use rbitset::BitSet8;
let set = BitSet8::from_iter([1u8, 2, 3]);
assert_eq!(set.contains(1), true);
assert_eq!(set.contains(4), false);sourcepub fn try_contains(&self, bit: usize) -> Result<bool, BitSetError>
pub fn try_contains(&self, bit: usize) -> Result<bool, BitSetError>
Returns true if the specified bit is enabled, in other words, if the set contains a
value.
Examples
use rbitset::BitSet8;
let set = BitSet8::from_iter([1u8, 2, 3]);
assert_eq!(set.try_contains(1), Ok(true));
assert_eq!(set.try_contains(4), Ok(false));sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the set.
Examples
use rbitset::BitSet16;
let mut set = BitSet16::new();
assert_eq!(set.len(), 0);
set.insert(1);
assert_eq!(set.len(), 1);sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the set contains no elements.
Examples
use rbitset::BitSet16;
let mut set = BitSet16::new();
assert!(set.is_empty());
set.insert(1);
assert!(!set.is_empty());sourcepub fn is_disjoint<U: PrimInt, const M: usize>(
&self,
other: &BitSet<U, M>
) -> bool
pub fn is_disjoint<U: PrimInt, const M: usize>(
&self,
other: &BitSet<U, M>
) -> bool
Returns true if self has no elements in common with other. This is equivalent to
checking for an empty intersection.
Examples
use rbitset::BitSet128;
let a = BitSet128::from_iter([1u8, 2, 3]);
let mut b = BitSet128::new();
assert!(a.is_disjoint(&b));
b.insert(4);
assert!(a.is_disjoint(&b));
b.insert(1);
assert!(!a.is_disjoint(&b));sourcepub fn is_subset<U: PrimInt, const M: usize>(
&self,
other: &BitSet<U, M>
) -> bool
pub fn is_subset<U: PrimInt, const M: usize>(
&self,
other: &BitSet<U, M>
) -> bool
Returns true if the set is a subset of another, i.e., other contains at least all the
values in self.
Examples
use rbitset::BitSet8;
let sup = BitSet8::from_iter([1u8, 2, 3]);
let mut set = BitSet8::new();
assert!(set.is_subset(&sup));
set.insert(2);
assert!(set.is_subset(&sup));
set.insert(4);
assert!(!set.is_subset(&sup));sourcepub fn is_superset<U: PrimInt, const M: usize>(
&self,
other: &BitSet<U, M>
) -> bool
pub fn is_superset<U: PrimInt, const M: usize>(
&self,
other: &BitSet<U, M>
) -> bool
Returns true if the set is a superset of another, i.e., self contains at least all the
values in other.
Examples
use rbitset::BitSet8;
let sub = BitSet8::from_iter([1u8, 2]);
let mut set = BitSet8::new();
assert!(!set.is_superset(&sub));
set.insert(0);
set.insert(1);
assert!(!set.is_superset(&sub));
set.insert(2);
assert!(set.is_superset(&sub));sourcepub fn count_ones(&self) -> u32
pub fn count_ones(&self) -> u32
Returns the total number of enabled bits.
Examples
use rbitset::BitSet8;
let set = BitSet8::from_iter([1u8, 2, 3]);
assert_eq!(set.count_ones(), 3);sourcepub fn count_zeros(&self) -> u32
pub fn count_zeros(&self) -> u32
Returns the total number of disabled bits.
Examples
use rbitset::BitSet8;
let set = BitSet8::from_iter([1u8, 2, 3]);
assert_eq!(set.count_zeros(), 5);sourcepub fn drain(&mut self) -> Drain<'_, T, N>ⓘNotable traits for Drain<'a, T, N>impl<'a, T: PrimInt, const N: usize> Iterator for Drain<'a, T, N> type Item = usize;
pub fn drain(&mut self) -> Drain<'_, T, N>ⓘNotable traits for Drain<'a, T, N>impl<'a, T: PrimInt, const N: usize> Iterator for Drain<'a, T, N> type Item = usize;
Clears the set, returning all elements as an iterator. Keeps the allocated memory for reuse.
If the returned iterator is dropped before being fully consumed, it drops the remaining elements. The returned iterator keeps a mutable borrow on the vector to optimize its implementation.
Examples
use rbitset::BitSet8;
let mut set = BitSet8::from_iter([1u8, 2, 3]);
assert!(!set.is_empty());
for i in set.drain() {
println!("{i}");
}
assert!(set.is_empty());sourcepub fn difference<'a, U: PrimInt, const M: usize>(
&'a self,
other: &'a BitSet<U, M>
) -> Difference<'a, T, U, N, M>ⓘNotable traits for Difference<'a, T, U, N, M>impl<'a, T, U, const N: usize, const M: usize> Iterator for Difference<'a, T, U, N, M> where
T: PrimInt + 'a,
U: PrimInt + 'a, type Item = usize;
pub fn difference<'a, U: PrimInt, const M: usize>(
&'a self,
other: &'a BitSet<U, M>
) -> Difference<'a, T, U, N, M>ⓘNotable traits for Difference<'a, T, U, N, M>impl<'a, T, U, const N: usize, const M: usize> Iterator for Difference<'a, T, U, N, M> where
T: PrimInt + 'a,
U: PrimInt + 'a, type Item = usize;
T: PrimInt + 'a,
U: PrimInt + 'a, type Item = usize;
Visits the values representing the difference, i.e., the values that are in self but not
in other.
Examples
use rbitset::BitSet8;
let a = BitSet8::from_iter([1u8, 2, 3]);
let b = BitSet8::from_iter([4u8, 2, 3, 4]);
// Can be seen as `a - b`.
for x in a.difference(&b) {
println!("{x}"); // Print 1
}
let diff: BitSet8 = a.difference(&b).collect();
let res = BitSet8::from_iter([1u8]);
assert_eq!(diff, res);
// Note that difference is not symmetric,
// and `b - a` means something else:
let diff: BitSet8 = b.difference(&a).collect();
let res = BitSet8::from_iter([4u8]);
assert_eq!(diff, res);sourcepub fn intersection<'a, U: PrimInt, const M: usize>(
&'a self,
other: &'a BitSet<U, M>
) -> Intersection<'a, T, U, N, M>ⓘNotable traits for Intersection<'a, T, U, N, M>impl<'a, T, U, const N: usize, const M: usize> Iterator for Intersection<'a, T, U, N, M> where
T: PrimInt + 'a,
U: PrimInt + 'a, type Item = usize;
pub fn intersection<'a, U: PrimInt, const M: usize>(
&'a self,
other: &'a BitSet<U, M>
) -> Intersection<'a, T, U, N, M>ⓘNotable traits for Intersection<'a, T, U, N, M>impl<'a, T, U, const N: usize, const M: usize> Iterator for Intersection<'a, T, U, N, M> where
T: PrimInt + 'a,
U: PrimInt + 'a, type Item = usize;
T: PrimInt + 'a,
U: PrimInt + 'a, type Item = usize;
Visits the values representing the intersection, i.e., the values that are both in self
and other.
Examples
use rbitset::BitSet8;
let a = BitSet8::from_iter([1u8, 2, 3]);
let b = BitSet8::from_iter([4u8, 2, 3, 4]);
for x in a.intersection(&b) {
println!("{x}");
}
let intersection: BitSet8 = a.intersection(&b).collect();
let test = BitSet8::from_iter([2u8, 3]);
assert_eq!(intersection, test);sourcepub fn symmetric_difference<'a, U: PrimInt, const M: usize>(
&'a self,
other: &'a BitSet<U, M>
) -> SymmetricDifference<'a, T, U, N, M>ⓘNotable traits for SymmetricDifference<'a, T, U, N, M>impl<'a, T, U, const N: usize, const M: usize> Iterator for SymmetricDifference<'a, T, U, N, M> where
T: PrimInt + 'a,
U: PrimInt + 'a, type Item = usize;
pub fn symmetric_difference<'a, U: PrimInt, const M: usize>(
&'a self,
other: &'a BitSet<U, M>
) -> SymmetricDifference<'a, T, U, N, M>ⓘNotable traits for SymmetricDifference<'a, T, U, N, M>impl<'a, T, U, const N: usize, const M: usize> Iterator for SymmetricDifference<'a, T, U, N, M> where
T: PrimInt + 'a,
U: PrimInt + 'a, type Item = usize;
T: PrimInt + 'a,
U: PrimInt + 'a, type Item = usize;
Visits the values representing the symmetric difference, i.e., the values that are in self
or in other but not in both.
Examples
use rbitset::BitSet8;
let a = BitSet8::from_iter([1u8, 2, 3]);
let b = BitSet8::from_iter([4u8, 2, 3, 4]);
for x in a.symmetric_difference(&b) {
println!("{x}");
}
let diff1: BitSet8 = a.symmetric_difference(&b).collect();
let diff2: BitSet8 = b.symmetric_difference(&a).collect();
assert_eq!(diff1, diff2);
let res = BitSet8::from_iter([1u8, 4]);
assert_eq!(diff1, res);sourcepub fn union<'a, U: PrimInt, const M: usize>(
&'a self,
other: &'a BitSet<U, M>
) -> Union<'a, T, U, N, M>ⓘNotable traits for Union<'a, T, U, N, M>impl<'a, T, U, const N: usize, const M: usize> Iterator for Union<'a, T, U, N, M> where
T: PrimInt + 'a,
U: PrimInt + 'a, type Item = usize;
pub fn union<'a, U: PrimInt, const M: usize>(
&'a self,
other: &'a BitSet<U, M>
) -> Union<'a, T, U, N, M>ⓘNotable traits for Union<'a, T, U, N, M>impl<'a, T, U, const N: usize, const M: usize> Iterator for Union<'a, T, U, N, M> where
T: PrimInt + 'a,
U: PrimInt + 'a, type Item = usize;
T: PrimInt + 'a,
U: PrimInt + 'a, type Item = usize;
Visits the values representing the union, i.e., all the values in self or other, without
duplicates.
Examples
use rbitset::BitSet8;
let a = BitSet8::from_iter([1u8, 2, 3]);
let b = BitSet8::from_iter([4u8, 2, 3, 4]);
for x in a.union(&b) {
println!("{x}");
}
let union: BitSet8 = a.union(&b).collect();
let res = BitSet8::from_iter([1u8, 2, 3, 4]);
assert_eq!(union, res);sourcepub fn iter(&self) -> Iter<'_, T, N>ⓘNotable traits for Iter<'a, T, N>impl<'a, T: PrimInt, const N: usize> Iterator for Iter<'a, T, N> type Item = usize;
pub fn iter(&self) -> Iter<'_, T, N>ⓘNotable traits for Iter<'a, T, N>impl<'a, T: PrimInt, const N: usize> Iterator for Iter<'a, T, N> type Item = usize;
An iterator visiting all elements in the set.
Examples
use rbitset::BitSet8;
let mut set = BitSet8::new();
set.insert(1);
set.insert(2);
for x in set.iter() {
println!("{x}");
}Trait Implementations
sourceimpl<'de, T: PrimInt + Default, const N: usize> Deserialize<'de> for BitSet<T, N>
Available on crate feature serde only.
impl<'de, T: PrimInt + Default, const N: usize> Deserialize<'de> for BitSet<T, N>
serde only.sourcefn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl<T: PrimInt, U: Into<usize>, const N: usize> Extend<U> for BitSet<T, N>
impl<T: PrimInt, U: Into<usize>, const N: usize> Extend<U> for BitSet<T, N>
sourcefn extend<I: IntoIterator<Item = U>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = U>>(&mut self, iter: I)
Extends a collection with the contents of an iterator. Read more
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Extends a collection with exactly one element.
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Reserves capacity in a collection for the given number of additional elements. Read more
sourceimpl<T: PrimInt + Default, U: Into<usize>, const N: usize> FromIterator<U> for BitSet<T, N>
impl<T: PrimInt + Default, U: Into<usize>, const N: usize> FromIterator<U> for BitSet<T, N>
sourcefn from_iter<I>(iter: I) -> Self where
I: IntoIterator<Item = U>,
fn from_iter<I>(iter: I) -> Self where
I: IntoIterator<Item = U>,
Creates a value from an iterator. Read more
sourceimpl<T: PrimInt, const N: usize> IntoIterator for BitSet<T, N>
impl<T: PrimInt, const N: usize> IntoIterator for BitSet<T, N>
sourceimpl<'a, T: PrimInt, const N: usize> IntoIterator for &'a BitSet<T, N>
impl<'a, T: PrimInt, const N: usize> IntoIterator for &'a BitSet<T, N>
sourceimpl<T: PrimInt, const N: usize> Serialize for BitSet<T, N>
Available on crate feature serde only.
impl<T: PrimInt, const N: usize> Serialize for BitSet<T, N>
serde only.impl<T: Copy, const N: usize> Copy for BitSet<T, N>
impl<T: Eq, const N: usize> Eq for BitSet<T, N>
impl<T, const N: usize> StructuralEq for BitSet<T, N>
impl<T, const N: usize> StructuralPartialEq for BitSet<T, N>
Auto Trait Implementations
impl<T, const N: usize> RefUnwindSafe for BitSet<T, N> where
T: RefUnwindSafe,
impl<T, const N: usize> Send for BitSet<T, N> where
T: Send,
impl<T, const N: usize> Sync for BitSet<T, N> where
T: Sync,
impl<T, const N: usize> Unpin for BitSet<T, N> where
T: Unpin,
impl<T, const N: usize> UnwindSafe for BitSet<T, N> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more