Struct BitSet
pub struct BitSet<B: BitBlock = u32> { /* private fields */ }
Implementations
impl BitSet<u32>
fn new() -> SelfCreates a new empty
BitSet.Examples
use bit_set::BitSet; let mut s = BitSet::new();fn with_capacity(nbits: usize) -> SelfCreates a new
BitSetwith initially no contents, able to holdnbitselements without resizing.Examples
use bit_set::BitSet; let mut s = BitSet::with_capacity(100); assert!(s.capacity() >= 100);fn from_bit_vec(bit_vec: BitVec) -> SelfCreates a new
BitSetfrom the given bit vector.Examples
use bit_vec::BitVec; use bit_set::BitSet; let bv = BitVec::from_bytes(&[0b01100000]); let s = BitSet::from_bit_vec(bv); // Print 1, 2 in arbitrary order for x in s.iter() { println!("{}", x); }fn from_bytes(bytes: &[u8]) -> Self
impl<B: BitBlock> BitSet<B>
fn new_general() -> SelfCreates a new empty
BitSet.Examples
use bit_set::BitSet; let mut s = <BitSet>::new_general();fn with_capacity_general(nbits: usize) -> SelfCreates a new
BitSetwith initially no contents, able to holdnbitselements without resizing.Examples
use bit_set::BitSet; let mut s = <BitSet>::with_capacity_general(100); assert!(s.capacity() >= 100);fn from_bit_vec_general(bit_vec: BitVec<B>) -> SelfCreates a new
BitSetfrom the given bit vector.Examples
use bit_vec::BitVec; use bit_set::BitSet; let bv: BitVec<u64> = BitVec::from_bytes_general(&[0b01100000]); let s = BitSet::from_bit_vec_general(bv); // Print 1, 2 in arbitrary order for x in s.iter() { println!("{}", x); }fn from_bytes_general(bytes: &[u8]) -> Selffn capacity(&self) -> usizeReturns the capacity in bits for this bit vector. Inserting any element less than this amount will not trigger a resizing.
Examples
use bit_set::BitSet; let mut s = BitSet::with_capacity(100); assert!(s.capacity() >= 100);fn reserve_len(&mut self, len: usize)Reserves capacity for the given
BitSetto containlendistinct elements. In the case ofBitSetthis means reallocations will not occur as long as all inserted elements are less thanlen.The collection may reserve more space to avoid frequent reallocations.
Examples
use bit_set::BitSet; let mut s = BitSet::new(); s.reserve_len(10); assert!(s.capacity() >= 10);fn reserve_len_exact(&mut self, len: usize)Reserves the minimum capacity for the given
BitSetto containlendistinct elements. In the case ofBitSetthis means reallocations will not occur as long as all inserted elements are less thanlen.Note that the allocator may give the collection more space than it requests. Therefore capacity can not be relied upon to be precisely minimal. Prefer
reserve_lenif future insertions are expected.Examples
use bit_set::BitSet; let mut s = BitSet::new(); s.reserve_len_exact(10); assert!(s.capacity() >= 10);fn into_bit_vec(self) -> BitVec<B>Consumes this set to return the underlying bit vector.
Examples
use bit_set::BitSet; let mut s = BitSet::new(); s.insert(0); s.insert(3); let bv = s.into_bit_vec(); assert!(bv[0]); assert!(bv[3]);fn get_ref(&self) -> &BitVec<B>Returns a reference to the underlying bit vector.
Examples
use bit_set::BitSet; let mut set = BitSet::new(); set.insert(0); let bv = set.get_ref(); assert_eq!(bv[0], true);fn get_mut(&mut self) -> &mut BitVec<B>Returns a mutable reference to the underlying bit vector.
Examples
use bit_set::BitSet; let mut set = BitSet::new(); set.insert(0); set.insert(3); { let bv = set.get_mut(); bv.set(1, true); } assert!(set.contains(0)); assert!(set.contains(1)); assert!(set.contains(3));fn shrink_to_fit(&mut self)Truncates the underlying vector to the least length required.
Examples
use bit_set::BitSet; let mut s = BitSet::new(); s.insert(3231); s.remove(3231); // Internal storage will probably be bigger than necessary println!("old capacity: {}", s.capacity()); assert!(s.capacity() >= 3231); // Now should be smaller s.shrink_to_fit(); println!("new capacity: {}", s.capacity());fn union_with(&mut self, other: &Self)Unions in-place with the specified other bit vector.
Examples
use bit_set::BitSet; let a = 0b01101000; let b = 0b10100000; let res = 0b11101000; let mut a = BitSet::from_bytes(&[a]); let b = BitSet::from_bytes(&[b]); let res = BitSet::from_bytes(&[res]); a.union_with(&b); assert_eq!(a, res);fn intersect_with(&mut self, other: &Self)Intersects in-place with the specified other bit vector.
Examples
use bit_set::BitSet; let a = 0b01101000; let b = 0b10100000; let res = 0b00100000; let mut a = BitSet::from_bytes(&[a]); let b = BitSet::from_bytes(&[b]); let res = BitSet::from_bytes(&[res]); a.intersect_with(&b); assert_eq!(a, res);fn difference_with(&mut self, other: &Self)Makes this bit vector the difference with the specified other bit vector in-place.
Examples
use bit_set::BitSet; let a = 0b01101000; let b = 0b10100000; let a_b = 0b01001000; // a - b let b_a = 0b10000000; // b - a let mut bva = BitSet::from_bytes(&[a]); let bvb = BitSet::from_bytes(&[b]); let bva_b = BitSet::from_bytes(&[a_b]); let bvb_a = BitSet::from_bytes(&[b_a]); bva.difference_with(&bvb); assert_eq!(bva, bva_b); let bva = BitSet::from_bytes(&[a]); let mut bvb = BitSet::from_bytes(&[b]); bvb.difference_with(&bva); assert_eq!(bvb, bvb_a);fn symmetric_difference_with(&mut self, other: &Self)Makes this bit vector the symmetric difference with the specified other bit vector in-place.
Examples
use bit_set::BitSet; let a = 0b01101000; let b = 0b10100000; let res = 0b11001000; let mut a = BitSet::from_bytes(&[a]); let b = BitSet::from_bytes(&[b]); let res = BitSet::from_bytes(&[res]); a.symmetric_difference_with(&b); assert_eq!(a, res);fn count(&self) -> usizeCounts the number of set bits in this set.
Note that this function scans the set to calculate the number.
fn len(&self) -> usizeCounts the number of set bits in this set.
Note that this function scans the set to calculate the number.
fn is_empty(&self) -> boolReturns whether there are no bits set in this set
fn make_empty(&mut self)Removes all elements of this set.
Different from
resetonly in that the capacity is preserved.fn reset(&mut self)Resets this set to an empty state.
Different from
make_emptyonly in that the capacity may NOT be preserved.fn clear(&mut self)Clears all bits in this set
fn contains(&self, value: usize) -> boolReturns
trueif this set contains the specified integer.fn is_disjoint(&self, other: &Self) -> boolReturns
trueif the set has no elements in common withother. This is equivalent to checking for an empty intersection.fn is_subset(&self, other: &Self) -> boolReturns
trueif the set is a subset of another.fn is_superset(&self, other: &Self) -> boolReturns
trueif the set is a superset of another.fn insert(&mut self, value: usize) -> boolAdds a value to the set. Returns
trueif the value was not already present in the set.fn remove(&mut self, value: usize) -> boolRemoves a value from the set. Returns
trueif the value was present in the set.fn truncate(&mut self, element: usize)Excludes
elementand all greater elements from theBitSet.
impl<B: BitBlock> BitSet<B>
fn union<'a>(&'a self, other: &'a Self) -> Union<'a, B>Iterator over each usize stored in
selfunionother. Seeunion_withfor an efficient in-place version.Examples
use bit_set::BitSet; let a = BitSet::from_bytes(&[0b01101000]); let b = BitSet::from_bytes(&[0b10100000]); // Print 0, 1, 2, 4 in arbitrary order for x in a.union(&b) { println!("{}", x); }fn intersection<'a>(&'a self, other: &'a Self) -> Intersection<'a, B>Iterator over each usize stored in
selfintersectother. Seeintersect_withfor an efficient in-place version.Examples
use bit_set::BitSet; let a = BitSet::from_bytes(&[0b01101000]); let b = BitSet::from_bytes(&[0b10100000]); // Print 2 for x in a.intersection(&b) { println!("{}", x); }fn difference<'a>(&'a self, other: &'a Self) -> Difference<'a, B>Iterator over each usize stored in the
selfsetminusother. Seedifference_withfor an efficient in-place version.Examples
use bit_set::BitSet; let a = BitSet::from_bytes(&[0b01101000]); let b = BitSet::from_bytes(&[0b10100000]); // Print 1, 4 in arbitrary order for x in a.difference(&b) { println!("{}", x); } // Note that difference is not symmetric, // and `b - a` means something else. // This prints 0 for x in b.difference(&a) { println!("{}", x); }fn symmetric_difference<'a>(&'a self, other: &'a Self) -> SymmetricDifference<'a, B>Iterator over each usize stored in the symmetric difference of
selfandother. Seesymmetric_difference_withfor an efficient in-place version.Examples
use bit_set::BitSet; let a = BitSet::from_bytes(&[0b01101000]); let b = BitSet::from_bytes(&[0b10100000]); // Print 0, 1, 4 in arbitrary order for x in a.symmetric_difference(&b) { println!("{}", x); }fn iter(&self) -> Iter<'_, B>Iterator over each usize stored in the
BitSet.Examples
use bit_set::BitSet; let s = BitSet::from_bytes(&[0b01001010]); // Print 1, 4, 6 in arbitrary order for x in s.iter() { println!("{}", x); }
Trait Implementations
impl<B: BitBlock> Clone for BitSet<B>
fn clone(&self) -> Selffn clone_from(&mut self, other: &Self)
impl<B: BitBlock> Debug for BitSet<B>
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result
impl<B: BitBlock> Default for BitSet<B>
fn default() -> Self
impl<B: BitBlock> Display for BitSet<B>
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result
impl<B: BitBlock> Eq for BitSet<B>
impl<B: BitBlock> Extend<usize> for BitSet<B>
fn extend<I: IntoIterator<Item = usize>>(&mut self, iter: I)
impl<B: BitBlock> FromIterator<usize> for BitSet<B>
fn from_iter<I: IntoIterator<Item = usize>>(iter: I) -> Self
impl<B: BitBlock> Hash for BitSet<B>
fn hash<H: Hasher>(&self, state: &mut H)
impl<B: BitBlock> Ord for BitSet<B>
fn cmp(&self, other: &Self) -> Ordering
impl<B: BitBlock> PartialEq for BitSet<B>
fn eq(&self, other: &Self) -> bool
impl<B: BitBlock> PartialOrd for BitSet<B>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>