Skip to main content

BitSet

Struct BitSet 

Source
pub struct BitSet<B = u32> { /* private fields */ }

Implementations§

Source§

impl BitSet<u32>

Source

pub fn new() -> Self

Creates a new empty BitSet.

§Examples
use bit_set::BitSet;

let mut s = BitSet::new();
Source

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

pub fn from_bit_vec(bit_vec: BitVec) -> Self

Creates a new BitSet from 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);
}
Source

pub fn from_bytes(bytes: &[u8]) -> Self

Source§

impl<B: BitBlock> BitSet<B>

Source

pub fn new_general() -> Self

Creates a new empty BitSet.

§Examples
use bit_set::BitSet;

let mut s = <BitSet>::new_general();
Source

pub fn with_capacity_general(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_general(100);
assert!(s.capacity() >= 100);
Source

pub fn from_bit_vec_general(bit_vec: BitVec<B>) -> Self

Creates a new BitSet from 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);
}
Source

pub fn from_bytes_general(bytes: &[u8]) -> Self

Source

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

pub fn count(&self) -> usize

Counts the number of set bits in this set.

Note that this function scans the set to calculate the number.

Source

pub fn len(&self) -> usize

👎Deprecated:

use BitSet::count() instead

Counts the number of set bits in this set.

Note that this function scans the set to calculate the number.

Source

pub fn is_empty(&self) -> bool

Returns whether there are no bits set in this set

Source

pub fn make_empty(&mut self)

Removes all elements of this set.

Different from reset only in that the capacity is preserved.

Source

pub fn reset(&mut self)

Resets this set to an empty state.

Different from make_empty only in that the capacity may NOT be preserved.

Source

pub fn clear(&mut self)

👎Deprecated since 0.9.0:

please use fn make_empty instead

Clears all bits in this set

Source

pub fn contains(&self, value: usize) -> bool

Returns true if this set contains the specified integer.

Source

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.

Source

pub fn is_subset(&self, other: &Self) -> bool

Returns true if the set is a subset of another.

Source

pub fn is_superset(&self, other: &Self) -> bool

Returns true if the set is a superset of another.

Source

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.

Source

pub fn remove(&mut self, value: usize) -> bool

Removes a value from the set. Returns true if the value was present in the set.

Source

pub fn truncate(&mut self, element: usize)

Excludes element and all greater elements from the BitSet.

Trait Implementations§

Source§

impl<B> BorshDeserialize for BitSet<B>

Source§

fn deserialize_reader<__R: Read>(reader: &mut __R) -> Result<Self, Error>

Source§

fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>

Deserializes this instance from a given slice of bytes. Updates the buffer to point at the remaining bytes.
Source§

fn try_from_slice(v: &[u8]) -> Result<Self, Error>

Deserialize this instance from a slice of bytes.
Source§

fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>
where R: Read,

Source§

impl<B> BorshSerialize for BitSet<B>
where B: BorshSerialize,

Source§

fn serialize<__W: Write>(&self, writer: &mut __W) -> Result<(), Error>

Source§

impl<B: BitBlock> Clone for BitSet<B>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
Source§

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

Performs copy-assignment from source. Read more
Source§

impl<B: DeBin> DeBin for BitSet<B>

Source§

fn de_bin(o: &mut usize, d: &[u8]) -> Result<Self, DeBinErr>

Parse Self from the input bytes starting at index offset. Read more
Source§

fn deserialize_bin(d: &[u8]) -> Result<Self, DeBinErr>

Parse Self from the input bytes. Read more
Source§

impl<B: DeJson> DeJson for BitSet<B>

Source§

fn de_json(s: &mut DeJsonState, i: &mut Chars<'_>) -> Result<Self, DeJsonErr>

Parse Self from the input string. Read more
Source§

fn deserialize_json(input: &str) -> Result<Self, DeJsonErr>

Parse Self from the input string. Read more
Source§

impl DeRon for BitSet

Source§

fn de_ron(s: &mut DeRonState, i: &mut Chars<'_>) -> Result<Self, DeRonErr>

Parse Self from a RON string. Read more
Source§

fn deserialize_ron(input: &str) -> Result<Self, DeRonErr>

Parse Self from a RON string. Read more
Source§

impl<B: BitBlock> Debug for BitSet<B>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<B: BitBlock> Default for BitSet<B>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de, B> Deserialize<'de> for BitSet<B>
where B: Deserialize<'de>,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<B> Deserialize for BitSet<B>
where B: Deserialize,

Source§

fn begin(__out: &mut Option<Self>) -> &mut dyn Visitor

The only correct implementation of this method is: Read more
Source§

impl<B: BitBlock> Display for BitSet<B>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<B: BitBlock> Extend<usize> for BitSet<B>

Source§

fn extend<I: IntoIterator<Item = usize>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<B: BitBlock> FromIterator<usize> for BitSet<B>

Source§

fn from_iter<I: IntoIterator<Item = usize>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<B: BitBlock> Hash for BitSet<B>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a, B: BitBlock> IntoIterator for &'a BitSet<B>

Source§

type Item = usize

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, B>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Iter<'a, B>

Creates an iterator from a value. Read more
Source§

impl<B: BitBlock> Ord for BitSet<B>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<B: BitBlock> PartialEq for BitSet<B>

Source§

fn eq(&self, other: &Self) -> 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.
Source§

impl<B: BitBlock> PartialOrd for BitSet<B>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<B: SerBin> SerBin for BitSet<B>

Source§

fn ser_bin(&self, s: &mut Vec<u8>)

Serialize Self to bytes. Read more
Source§

fn serialize_bin(&self) -> Vec<u8>

Serialize Self to bytes. Read more
Source§

impl<B: SerJson> SerJson for BitSet<B>

Source§

fn ser_json(&self, d: usize, s: &mut SerJsonState)

Serialize Self to a JSON string. Read more
Source§

fn serialize_json(&self) -> String

Serialize Self to a JSON string. Read more
Source§

impl SerRon for BitSet

Source§

fn ser_ron(&self, d: usize, s: &mut SerRonState)

Serialize Self to a RON string. Read more
Source§

fn serialize_ron(&self) -> String

Serialize Self to a RON string. Read more
Source§

impl<B> Serialize for BitSet<B>
where B: Serialize,

Source§

fn begin(&self) -> Fragment<'_>

Source§

impl<B> Serialize for BitSet<B>
where B: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<B: BitBlock> Eq for BitSet<B>

Auto Trait Implementations§

§

impl<B> Freeze for BitSet<B>

§

impl<B> RefUnwindSafe for BitSet<B>
where B: RefUnwindSafe,

§

impl<B> Send for BitSet<B>
where B: Send,

§

impl<B> Sync for BitSet<B>
where B: Sync,

§

impl<B> Unpin for BitSet<B>
where B: Unpin,

§

impl<B> UnsafeUnpin for BitSet<B>

§

impl<B> UnwindSafe for BitSet<B>
where B: UnwindSafe,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,