Struct fixedbitset::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
impl FixedBitSet
sourcepub fn with_capacity(bits: usize) -> Self
pub fn with_capacity(bits: usize) -> Self
Create a new FixedBitSet with a specific number of bits, all initially clear.
sourcepub fn with_capacity_and_blocks<I: IntoIterator<Item = Block>>(
bits: usize,
blocks: I
) -> Self
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");
sourcepub fn grow_and_insert(&mut self, bits: usize)
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.
sourcepub fn len(&self) -> usize
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.
sourcepub fn is_empty(&self) -> bool
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());
sourcepub fn is_clear(&self) -> bool
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
.
sourcepub fn minimum(&self) -> Option<usize>
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));
sourcepub fn maximum(&self) -> Option<usize>
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));
sourcepub fn is_full(&self) -> bool
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()
.
sourcepub fn contains(&self, bit: usize) -> bool
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]
.
sourcepub unsafe fn contains_unchecked(&self, bit: usize) -> bool
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()
sourcepub unsafe fn insert_unchecked(&mut self, bit: usize)
pub unsafe fn insert_unchecked(&mut self, bit: usize)
sourcepub unsafe fn remove_unchecked(&mut self, bit: usize)
pub unsafe fn remove_unchecked(&mut self, bit: usize)
sourcepub fn put(&mut self, bit: usize) -> bool
pub fn put(&mut self, bit: usize) -> bool
Enable bit
, and return its previous value.
Panics if bit is out of bounds.
sourcepub unsafe fn put_unchecked(&mut self, bit: usize) -> bool
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()
sourcepub fn toggle(&mut self, bit: usize)
pub fn toggle(&mut self, bit: usize)
Toggle bit
(inverting its state).
Panics if bit is out of bounds
sourcepub unsafe fn toggle_unchecked(&mut self, bit: usize)
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()
sourcepub fn set(&mut self, bit: usize, enabled: bool)
pub fn set(&mut self, bit: usize, enabled: bool)
Sets a bit to the provided enabled
value.
Panics if bit is out of bounds.
sourcepub unsafe fn set_unchecked(&mut self, bit: usize, enabled: bool)
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()
sourcepub fn copy_bit(&mut self, from: usize, to: usize)
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.
sourcepub unsafe fn copy_bit_unchecked(&mut self, from: usize, to: usize)
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()
sourcepub fn count_ones<T: IndexRange>(&self, range: T) -> usize
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.
sourcepub fn count_zeroes<T: IndexRange>(&self, range: T) -> usize
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.
sourcepub fn set_range<T: IndexRange>(&mut self, range: T, enabled: bool)
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.
sourcepub fn insert_range<T: IndexRange>(&mut self, range: T)
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.
sourcepub fn remove_range<T: IndexRange>(&mut self, range: T)
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.
sourcepub fn toggle_range<T: IndexRange>(&mut self, range: T)
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.
sourcepub fn contains_all_in_range<T: IndexRange>(&self, range: T) -> bool
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.
sourcepub fn contains_any_in_range<T: IndexRange>(&self, range: T) -> bool
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.
sourcepub fn as_mut_slice(&mut self) -> &mut [Block]
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.
sourcepub fn ones(&self) -> Ones<'_> ⓘ
pub fn ones(&self) -> Ones<'_> ⓘ
Iterates over all enabled bits.
Iterator element is the index of the 1
bit, type usize
.
sourcepub fn into_ones(self) -> IntoOnes ⓘ
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
.
sourcepub fn zeroes(&self) -> Zeroes<'_> ⓘ
pub fn zeroes(&self) -> Zeroes<'_> ⓘ
Iterates over all disabled bits.
Iterator element is the index of the 0
bit, type usize
.
sourcepub fn intersection<'a>(&'a self, other: &'a FixedBitSet) -> Intersection<'a> ⓘ
pub fn intersection<'a>(&'a self, other: &'a FixedBitSet) -> Intersection<'a> ⓘ
Returns a lazy iterator over the intersection of two FixedBitSet
s
sourcepub fn union<'a>(&'a self, other: &'a FixedBitSet) -> Union<'a> ⓘ
pub fn union<'a>(&'a self, other: &'a FixedBitSet) -> Union<'a> ⓘ
Returns a lazy iterator over the union of two FixedBitSet
s.
sourcepub fn difference<'a>(&'a self, other: &'a FixedBitSet) -> Difference<'a> ⓘ
pub fn difference<'a>(&'a self, other: &'a FixedBitSet) -> Difference<'a> ⓘ
Returns a lazy iterator over the difference of two FixedBitSet
s. The difference of a
and b
is the elements of a
which are not in b
.
sourcepub fn symmetric_difference<'a>(
&'a self,
other: &'a FixedBitSet
) -> SymmetricDifference<'a> ⓘ
pub fn symmetric_difference<'a>( &'a self, other: &'a FixedBitSet ) -> SymmetricDifference<'a> ⓘ
Returns a lazy iterator over the symmetric difference of two FixedBitSet
s.
The symmetric difference of a
and b
is the elements of one, but not both, sets.
sourcepub fn union_with(&mut self, other: &FixedBitSet)
pub fn union_with(&mut self, other: &FixedBitSet)
In-place union of two FixedBitSet
s.
On calling this method, self
’s capacity may be increased to match other
’s.
sourcepub fn intersect_with(&mut self, other: &FixedBitSet)
pub fn intersect_with(&mut self, other: &FixedBitSet)
In-place intersection of two FixedBitSet
s.
On calling this method, self
’s capacity will remain the same as before.
sourcepub fn difference_with(&mut self, other: &FixedBitSet)
pub fn difference_with(&mut self, other: &FixedBitSet)
In-place difference of two FixedBitSet
s.
On calling this method, self
’s capacity will remain the same as before.
sourcepub fn symmetric_difference_with(&mut self, other: &FixedBitSet)
pub fn symmetric_difference_with(&mut self, other: &FixedBitSet)
In-place symmetric difference of two FixedBitSet
s.
On calling this method, self
’s capacity may be increased to match other
’s.
sourcepub fn union_count(&self, other: &FixedBitSet) -> usize
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.
sourcepub fn intersection_count(&self, other: &FixedBitSet) -> usize
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.
sourcepub fn difference_count(&self, other: &FixedBitSet) -> usize
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.
sourcepub fn symmetric_difference_count(&self, other: &FixedBitSet) -> usize
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.
sourcepub fn is_disjoint(&self, other: &FixedBitSet) -> bool
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.
sourcepub fn is_subset(&self, other: &FixedBitSet) -> bool
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
.
sourcepub fn is_superset(&self, other: &FixedBitSet) -> bool
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
impl Binary for FixedBitSet
source§impl<'a> BitAnd for &'a FixedBitSet
impl<'a> BitAnd for &'a FixedBitSet
§type Output = FixedBitSet
type Output = FixedBitSet
&
operator.source§fn bitand(self, other: &FixedBitSet) -> FixedBitSet
fn bitand(self, other: &FixedBitSet) -> FixedBitSet
&
operation. Read moresource§impl BitAndAssign<&FixedBitSet> for FixedBitSet
impl BitAndAssign<&FixedBitSet> for FixedBitSet
source§fn bitand_assign(&mut self, other: &Self)
fn bitand_assign(&mut self, other: &Self)
&=
operation. Read moresource§impl BitAndAssign for FixedBitSet
impl BitAndAssign for FixedBitSet
source§fn bitand_assign(&mut self, other: Self)
fn bitand_assign(&mut self, other: Self)
&=
operation. Read moresource§impl<'a> BitOr for &'a FixedBitSet
impl<'a> BitOr for &'a FixedBitSet
§type Output = FixedBitSet
type Output = FixedBitSet
|
operator.source§fn bitor(self, other: &FixedBitSet) -> FixedBitSet
fn bitor(self, other: &FixedBitSet) -> FixedBitSet
|
operation. Read moresource§impl BitOrAssign<&FixedBitSet> for FixedBitSet
impl BitOrAssign<&FixedBitSet> for FixedBitSet
source§fn bitor_assign(&mut self, other: &Self)
fn bitor_assign(&mut self, other: &Self)
|=
operation. Read moresource§impl BitOrAssign for FixedBitSet
impl BitOrAssign for FixedBitSet
source§fn bitor_assign(&mut self, other: Self)
fn bitor_assign(&mut self, other: Self)
|=
operation. Read moresource§impl<'a> BitXor for &'a FixedBitSet
impl<'a> BitXor for &'a FixedBitSet
§type Output = FixedBitSet
type Output = FixedBitSet
^
operator.source§fn bitxor(self, other: &FixedBitSet) -> FixedBitSet
fn bitxor(self, other: &FixedBitSet) -> FixedBitSet
^
operation. Read moresource§impl BitXorAssign<&FixedBitSet> for FixedBitSet
impl BitXorAssign<&FixedBitSet> for FixedBitSet
source§fn bitxor_assign(&mut self, other: &Self)
fn bitxor_assign(&mut self, other: &Self)
^=
operation. Read moresource§impl BitXorAssign for FixedBitSet
impl BitXorAssign for FixedBitSet
source§fn bitxor_assign(&mut self, other: Self)
fn bitxor_assign(&mut self, other: Self)
^=
operation. Read moresource§impl Clone for FixedBitSet
impl Clone for FixedBitSet
source§impl Debug for FixedBitSet
impl Debug for FixedBitSet
source§impl Default for FixedBitSet
impl Default for FixedBitSet
source§impl Display for FixedBitSet
impl Display for FixedBitSet
source§impl Drop for FixedBitSet
impl Drop for FixedBitSet
source§impl Extend<usize> for FixedBitSet
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)
fn extend<I: IntoIterator<Item = usize>>(&mut self, src: 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 FromIterator<usize> for FixedBitSet
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§impl Hash for FixedBitSet
impl Hash for FixedBitSet
source§impl Index<usize> for FixedBitSet
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§impl Ord for FixedBitSet
impl Ord for FixedBitSet
source§impl PartialEq for FixedBitSet
impl PartialEq for FixedBitSet
source§impl PartialOrd for FixedBitSet
impl PartialOrd for FixedBitSet
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