fixedbitset

Struct FixedBitSet

Source
pub struct FixedBitSet { /* private fields */ }
Expand description

FixedBitSet is a simple fixed size set of bits that each can be enabled (1 / true) or disabled (0 / false).

The bit set has a fixed capacity in terms of enabling bits (and the capacity can grow using the grow method).

Derived traits depend on both the zeros and ones, so [0,1] is not equal to [0,1,0].

Implementations§

Source§

impl FixedBitSet

Source

pub const fn new() -> Self

Create a new empty FixedBitSet.

Source

pub fn with_capacity(bits: usize) -> Self

Create a new FixedBitSet with a specific number of bits, all initially clear.

Source

pub fn with_capacity_and_blocks<I: IntoIterator<Item = Block>>( bits: usize, blocks: I, ) -> Self

Create a new FixedBitSet with a specific number of bits, initialized from provided blocks.

If the blocks are not the exact size needed for the capacity they will be padded with zeros (if shorter) or truncated to the capacity (if longer).

For example:

let data = vec![4];
let bs = fixedbitset::FixedBitSet::with_capacity_and_blocks(4, data);
assert_eq!(format!("{:b}", bs), "0010");
Source

pub fn grow(&mut self, bits: usize)

Grow capacity to bits, all new bits initialized to zero

Source

pub fn grow_and_insert(&mut self, bits: usize)

Grows the internal size of the bitset before inserting a bit

Unlike insert, this cannot panic, but may allocate if the bit is outside of the existing buffer’s range.

This is faster than calling grow then insert in succession.

Source

pub fn len(&self) -> usize

The length of the FixedBitSet in bits.

Note: len includes both set and unset bits.

let bitset = FixedBitSet::with_capacity(10);
// there are 0 set bits, but 10 unset bits
assert_eq!(bitset.len(), 10);

len does not return the count of set bits. For that, use bitset.count_ones(..) instead.

Source

pub fn is_empty(&self) -> bool

true if the FixedBitSet is empty.

Note that an “empty” FixedBitSet is a FixedBitSet with no bits (meaning: it’s length is zero). If you want to check if all bits are unset, use FixedBitSet::is_clear.

let bitset = FixedBitSet::with_capacity(10);
assert!(!bitset.is_empty());

let bitset = FixedBitSet::with_capacity(0);
assert!(bitset.is_empty());
Source

pub fn is_clear(&self) -> bool

true if all bits in the FixedBitSet are unset.

As opposed to FixedBitSet::is_empty, which is true only for sets without any bits, set or unset.

let mut bitset = FixedBitSet::with_capacity(10);
assert!(bitset.is_clear());

bitset.insert(2);
assert!(!bitset.is_clear());

This is equivalent to bitset.count_ones(..) == 0.

Source

pub fn minimum(&self) -> Option<usize>

Finds the lowest set bit in the bitset.

Returns None if there aren’t any set bits.

let mut bitset = FixedBitSet::with_capacity(10);
assert_eq!(bitset.minimum(), None);

bitset.insert(2);
assert_eq!(bitset.minimum(), Some(2));
bitset.insert(8);
assert_eq!(bitset.minimum(), Some(2));
Source

pub fn maximum(&self) -> Option<usize>

Finds the highest set bit in the bitset.

Returns None if there aren’t any set bits.

let mut bitset = FixedBitSet::with_capacity(10);
assert_eq!(bitset.maximum(), None);

bitset.insert(8);
assert_eq!(bitset.maximum(), Some(8));
bitset.insert(2);
assert_eq!(bitset.maximum(), Some(8));
Source

pub fn is_full(&self) -> bool

true if all bits in the FixedBitSet are set.

let mut bitset = FixedBitSet::with_capacity(10);
assert!(!bitset.is_full());

bitset.insert_range(..);
assert!(bitset.is_full());

This is equivalent to bitset.count_ones(..) == bitset.len().

Source

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

Return true if the bit is enabled in the FixedBitSet, false otherwise.

Note: bits outside the capacity are always disabled.

Note: Also available with index syntax: bitset[bit].

Source

pub unsafe fn contains_unchecked(&self, bit: usize) -> bool

Return true if the bit is enabled in the FixedBitSet, false otherwise.

Note: unlike contains, calling this with an invalid bit is undefined behavior.

§Safety

bit must be less than self.len()

Source

pub fn clear(&mut self)

Clear all bits.

Source

pub fn insert(&mut self, bit: usize)

Enable bit.

Panics if bit is out of bounds.

Source

pub unsafe fn insert_unchecked(&mut self, bit: usize)

Enable bit without any length checks.

§Safety

bit must be less than self.len()

Source

pub fn remove(&mut self, bit: usize)

Disable bit.

Panics if bit is out of bounds.

Source

pub unsafe fn remove_unchecked(&mut self, bit: usize)

Disable bit without any bounds checking.

§Safety

bit must be less than self.len()

Source

pub fn put(&mut self, bit: usize) -> bool

Enable bit, and return its previous value.

Panics if bit is out of bounds.

Source

pub unsafe fn put_unchecked(&mut self, bit: usize) -> bool

Enable bit, and return its previous value without doing any bounds checking.

§Safety

bit must be less than self.len()

Source

pub fn toggle(&mut self, bit: usize)

Toggle bit (inverting its state).

Panics if bit is out of bounds

Source

pub unsafe fn toggle_unchecked(&mut self, bit: usize)

Toggle bit (inverting its state) without any bounds checking.

§Safety

bit must be less than self.len()

Source

pub fn set(&mut self, bit: usize, enabled: bool)

Sets a bit to the provided enabled value.

Panics if bit is out of bounds.

Source

pub unsafe fn set_unchecked(&mut self, bit: usize, enabled: bool)

Sets a bit to the provided enabled value without doing any bounds checking.

§Safety

bit must be less than self.len()

Source

pub fn copy_bit(&mut self, from: usize, to: usize)

Copies boolean value from specified bit to the specified bit.

If from is out-of-bounds, to will be unset.

Panics if to is out of bounds.

Source

pub unsafe fn copy_bit_unchecked(&mut self, from: usize, to: usize)

Copies boolean value from specified bit to the specified bit.

Note: unlike copy_bit, calling this with an invalid from is undefined behavior.

§Safety

to must both be less than self.len()

Source

pub fn count_ones<T: IndexRange>(&self, range: T) -> usize

Count the number of set bits in the given bit range.

This function is potentially much faster than using ones(other).count(). Use .. to count the whole content of the bitset.

Panics if the range extends past the end of the bitset.

Source

pub fn count_zeroes<T: IndexRange>(&self, range: T) -> usize

Count the number of unset bits in the given bit range.

This function is potentially much faster than using zeroes(other).count(). Use .. to count the whole content of the bitset.

Panics if the range extends past the end of the bitset.

Source

pub fn set_range<T: IndexRange>(&mut self, range: T, enabled: bool)

Sets every bit in the given range to the given state (enabled)

Use .. to set the whole bitset.

Panics if the range extends past the end of the bitset.

Source

pub fn insert_range<T: IndexRange>(&mut self, range: T)

Enables every bit in the given range.

Use .. to make the whole bitset ones.

Panics if the range extends past the end of the bitset.

Source

pub fn remove_range<T: IndexRange>(&mut self, range: T)

Disables every bit in the given range.

Use .. to make the whole bitset ones.

Panics if the range extends past the end of the bitset.

Source

pub fn toggle_range<T: IndexRange>(&mut self, range: T)

Toggles (inverts) every bit in the given range.

Use .. to toggle the whole bitset.

Panics if the range extends past the end of the bitset.

Source

pub fn contains_all_in_range<T: IndexRange>(&self, range: T) -> bool

Checks if the bitset contains every bit in the given range.

Panics if the range extends past the end of the bitset.

Source

pub fn contains_any_in_range<T: IndexRange>(&self, range: T) -> bool

Checks if the bitset contains at least one set bit in the given range.

Panics if the range extends past the end of the bitset.

Source

pub fn as_slice(&self) -> &[Block]

View the bitset as a slice of Block blocks

Source

pub fn as_mut_slice(&mut self) -> &mut [Block]

View the bitset as a mutable slice of Block blocks. Writing past the bitlength in the last will cause contains to return potentially incorrect results for bits past the bitlength.

Source

pub fn ones(&self) -> Ones<'_>

Iterates over all enabled bits.

Iterator element is the index of the 1 bit, type usize.

Source

pub fn into_ones(self) -> IntoOnes

Iterates over all enabled bits.

Iterator element is the index of the 1 bit, type usize. Unlike ones, this function consumes the FixedBitset.

Source

pub fn zeroes(&self) -> Zeroes<'_>

Iterates over all disabled bits.

Iterator element is the index of the 0 bit, type usize.

Source

pub fn intersection<'a>(&'a self, other: &'a FixedBitSet) -> Intersection<'a>

Returns a lazy iterator over the intersection of two FixedBitSets

Source

pub fn union<'a>(&'a self, other: &'a FixedBitSet) -> Union<'a>

Returns a lazy iterator over the union of two FixedBitSets.

Source

pub fn difference<'a>(&'a self, other: &'a FixedBitSet) -> Difference<'a>

Returns a lazy iterator over the difference of two FixedBitSets. The difference of a and b is the elements of a which are not in b.

Source

pub fn symmetric_difference<'a>( &'a self, other: &'a FixedBitSet, ) -> SymmetricDifference<'a>

Returns a lazy iterator over the symmetric difference of two FixedBitSets. The symmetric difference of a and b is the elements of one, but not both, sets.

Source

pub fn union_with(&mut self, other: &FixedBitSet)

In-place union of two FixedBitSets.

On calling this method, self’s capacity may be increased to match other’s.

Source

pub fn intersect_with(&mut self, other: &FixedBitSet)

In-place intersection of two FixedBitSets.

On calling this method, self’s capacity will remain the same as before.

Source

pub fn difference_with(&mut self, other: &FixedBitSet)

In-place difference of two FixedBitSets.

On calling this method, self’s capacity will remain the same as before.

Source

pub fn symmetric_difference_with(&mut self, other: &FixedBitSet)

In-place symmetric difference of two FixedBitSets.

On calling this method, self’s capacity may be increased to match other’s.

Source

pub fn union_count(&self, other: &FixedBitSet) -> usize

Computes how many bits would be set in the union between two bitsets.

This is potentially much faster than using union(other).count(). Unlike other methods like using [union_with] followed by [count_ones], this does not mutate in place or require separate allocations.

Source

pub fn intersection_count(&self, other: &FixedBitSet) -> usize

Computes how many bits would be set in the intersection between two bitsets.

This is potentially much faster than using intersection(other).count(). Unlike other methods like using [intersect_with] followed by [count_ones], this does not mutate in place or require separate allocations.

Source

pub fn difference_count(&self, other: &FixedBitSet) -> usize

Computes how many bits would be set in the difference between two bitsets.

This is potentially much faster than using difference(other).count(). Unlike other methods like using [difference_with] followed by [count_ones], this does not mutate in place or require separate allocations.

Source

pub fn symmetric_difference_count(&self, other: &FixedBitSet) -> usize

Computes how many bits would be set in the symmetric difference between two bitsets.

This is potentially much faster than using symmetric_difference(other).count(). Unlike other methods like using [symmetric_difference_with] followed by [count_ones], this does not mutate in place or require separate allocations.

Source

pub fn is_disjoint(&self, other: &FixedBitSet) -> bool

Returns true if self has no elements in common with other. This is equivalent to checking for an empty intersection.

Source

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

Returns true if the set is a subset of another, i.e. other contains at least all the values in self.

Source

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

Returns true if the set is a superset of another, i.e. self contains at least all the values in other.

Trait Implementations§

Source§

impl Binary for FixedBitSet

Source§

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

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

impl<'a> BitAnd for &'a FixedBitSet

Source§

type Output = FixedBitSet

The resulting type after applying the & operator.
Source§

fn bitand(self, other: &FixedBitSet) -> FixedBitSet

Performs the & operation. Read more
Source§

impl BitAndAssign<&FixedBitSet> for FixedBitSet

Source§

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

Performs the &= operation. Read more
Source§

impl BitAndAssign for FixedBitSet

Source§

fn bitand_assign(&mut self, other: Self)

Performs the &= operation. Read more
Source§

impl<'a> BitOr for &'a FixedBitSet

Source§

type Output = FixedBitSet

The resulting type after applying the | operator.
Source§

fn bitor(self, other: &FixedBitSet) -> FixedBitSet

Performs the | operation. Read more
Source§

impl BitOrAssign<&FixedBitSet> for FixedBitSet

Source§

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

Performs the |= operation. Read more
Source§

impl BitOrAssign for FixedBitSet

Source§

fn bitor_assign(&mut self, other: Self)

Performs the |= operation. Read more
Source§

impl<'a> BitXor for &'a FixedBitSet

Source§

type Output = FixedBitSet

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, other: &FixedBitSet) -> FixedBitSet

Performs the ^ operation. Read more
Source§

impl BitXorAssign<&FixedBitSet> for FixedBitSet

Source§

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

Performs the ^= operation. Read more
Source§

impl BitXorAssign for FixedBitSet

Source§

fn bitxor_assign(&mut self, other: Self)

Performs the ^= operation. Read more
Source§

impl Clone for FixedBitSet

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for FixedBitSet

Source§

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

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

impl Default for FixedBitSet

Source§

fn default() -> Self

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

impl Display for FixedBitSet

Source§

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

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

impl Drop for FixedBitSet

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Extend<usize> for FixedBitSet

Sets the bit at index i to true for each item i in the input src.

Source§

fn extend<I: IntoIterator<Item = usize>>(&mut self, src: 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 FromIterator<usize> for FixedBitSet

Return a FixedBitSet containing bits set to true for every bit index in the iterator, other bits are set to false.

Source§

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

Creates a value from an iterator. Read more
Source§

impl Hash for FixedBitSet

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 Index<usize> for FixedBitSet

Return true if the bit is enabled in the bitset, or false otherwise.

Note: bits outside the capacity are always disabled, and thus indexing a FixedBitSet will not panic.

Source§

type Output = bool

The returned type after indexing.
Source§

fn index(&self, bit: usize) -> &bool

Performs the indexing (container[index]) operation. Read more
Source§

impl Ord for FixedBitSet

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 PartialEq for FixedBitSet

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 PartialOrd for FixedBitSet

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 Eq for FixedBitSet

Source§

impl Send for FixedBitSet

Source§

impl Sync for FixedBitSet

Auto Trait Implementations§

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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§

default 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.