pub struct BitSet<B = u32> {
pub(crate) bit_vec: BitVec<B>,
}
Fields§
§bit_vec: BitVec<B>
Implementations§
source§impl BitSet<u32>
impl BitSet<u32>
sourcepub fn with_capacity(nbits: usize) -> Self
pub fn with_capacity(nbits: usize) -> Self
Creates a new BitSet
with initially no contents, able to
hold nbits
elements without resizing.
§Examples
use bit_set::BitSet;
let mut s = BitSet::with_capacity(100);
assert!(s.capacity() >= 100);
sourcepub fn from_bit_vec(bit_vec: BitVec) -> Self
pub fn from_bit_vec(bit_vec: BitVec) -> Self
Creates a new BitSet
from the given bit vector.
§Examples
extern crate bit_vec;
extern crate bit_set;
fn main() {
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);
}
}
pub fn from_bytes(bytes: &[u8]) -> Self
source§impl<B: BitBlock> BitSet<B>
impl<B: BitBlock> BitSet<B>
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns 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);
sourcepub fn reserve_len(&mut self, len: usize)
pub fn reserve_len(&mut self, len: usize)
Reserves capacity for the given BitSet
to contain len
distinct elements. In the case
of BitSet
this means reallocations will not occur as long as all inserted elements
are less than len
.
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);
sourcepub fn reserve_len_exact(&mut self, len: usize)
pub fn reserve_len_exact(&mut self, len: usize)
Reserves the minimum capacity for the given BitSet
to contain len
distinct elements.
In the case of BitSet
this means reallocations will not occur as long as all inserted
elements are less than len
.
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_len
if future
insertions are expected.
§Examples
use bit_set::BitSet;
let mut s = BitSet::new();
s.reserve_len_exact(10);
assert!(s.capacity() >= 10);
sourcepub fn into_bit_vec(self) -> BitVec<B>
pub 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]);
sourcepub fn get_ref(&self) -> &BitVec<B>
pub 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);
sourcepub fn get_mut(&mut self) -> &mut BitVec<B>
pub 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));
pub(crate) fn other_op<F>(&mut self, other: &Self, f: F)where
F: FnMut(B, B) -> B,
sourcepub fn shrink_to_fit(&mut self)
pub 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());
sourcepub fn iter(&self) -> Iter<'_, B> ⓘ
pub 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);
}
sourcepub fn union<'a>(&'a self, other: &'a Self) -> Union<'a, B> ⓘ
pub fn union<'a>(&'a self, other: &'a Self) -> Union<'a, B> ⓘ
Iterator over each usize stored in self
union other
.
See union_with
for 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);
}
sourcepub fn intersection<'a>(&'a self, other: &'a Self) -> Intersection<'a, B> ⓘ
pub fn intersection<'a>(&'a self, other: &'a Self) -> Intersection<'a, B> ⓘ
Iterator over each usize stored in self
intersect other
.
See intersect_with
for 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);
}
sourcepub fn difference<'a>(&'a self, other: &'a Self) -> Difference<'a, B> ⓘ
pub fn difference<'a>(&'a self, other: &'a Self) -> Difference<'a, B> ⓘ
Iterator over each usize stored in the self
setminus other
.
See difference_with
for 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);
}
sourcepub fn symmetric_difference<'a>(
&'a self,
other: &'a Self,
) -> SymmetricDifference<'a, B> ⓘ
pub fn symmetric_difference<'a>( &'a self, other: &'a Self, ) -> SymmetricDifference<'a, B> ⓘ
Iterator over each usize stored in the symmetric difference of self
and other
.
See symmetric_difference_with
for 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);
}
sourcepub fn union_with(&mut self, other: &Self)
pub 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);
sourcepub fn intersect_with(&mut self, other: &Self)
pub 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);
sourcepub fn difference_with(&mut self, other: &Self)
pub 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);
sourcepub fn symmetric_difference_with(&mut self, other: &Self)
pub 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);
sourcepub fn contains(&self, value: usize) -> bool
pub fn contains(&self, value: usize) -> bool
Returns true
if this set contains the specified integer.
sourcepub fn is_disjoint(&self, other: &Self) -> bool
pub fn is_disjoint(&self, other: &Self) -> bool
Returns true
if the set has no elements in common with other
.
This is equivalent to checking for an empty intersection.
sourcepub fn is_superset(&self, other: &Self) -> bool
pub fn is_superset(&self, other: &Self) -> bool
Returns true
if the set is a superset of another.
sourcepub fn insert(&mut self, value: usize) -> bool
pub fn insert(&mut self, value: usize) -> bool
Adds a value to the set. Returns true
if the value was not already
present in the set.
Trait Implementations§
source§impl<B: BitBlock> Extend<usize> for BitSet<B>
impl<B: BitBlock> Extend<usize> for BitSet<B>
source§fn extend<I: IntoIterator<Item = usize>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = usize>>(&mut self, iter: I)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl<'a, B: BitBlock> IntoIterator for &'a BitSet<B>
impl<'a, B: BitBlock> IntoIterator for &'a BitSet<B>
source§impl<B: BitBlock> Ord for BitSet<B>
impl<B: BitBlock> Ord for BitSet<B>
source§impl<B: BitBlock> PartialEq for BitSet<B>
impl<B: BitBlock> PartialEq for BitSet<B>
source§impl<B: BitBlock> PartialOrd for BitSet<B>
impl<B: BitBlock> PartialOrd for BitSet<B>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more