dense_bitset

Struct BitSet

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

A variably sized, heap allocated, dense bitset implemented using no unsafe code.

§Examples

use dense_bitset::BitSet;

let mut set = BitSet::new();

set.insert(7);
set.set(4, true);
set.flip(5);

assert_eq!(set, [7, 4, 5].iter().collect());

set.remove(7);
set.flip(4);
set.set(5, false);

assert!(set.is_empty());

let a: BitSet = [2, 5, 12, 17].iter().collect();
let b: BitSet = [2, 12].iter().collect();

assert!(!a.is_disjoint(&b));
assert!(b.is_subset(&a));
assert!(a.is_superset(&b));

Implementations§

Source§

impl BitSet

Source

pub fn new() -> Self

Constructs a new, empty BitSet.

§Examples
let mut set = BitSet::new();

set.insert(7);
assert_eq!(1, set.element_count());
Source

pub fn with_capacity(capacity: usize) -> Self

Constructs a new, empty BitSet with at least the specified capacity. All indices which are smaller than this capacity, can be used without requiring a reallocation.

§Examples
let mut set = BitSet::with_capacity(100);
let capacity = set.capacity();
assert!(capacity >= 100);
set.insert(99);
assert_eq!(capacity, set.capacity());
Source

pub fn capacity(&self) -> usize

Returns the capacity of this BitSet. All indices which are smaller than this capacity, can be used without requiring a reallocation.

§Examples
let mut set = BitSet::with_capacity(100);
assert!(set.capacity() >= 100);
Source

pub fn is_empty(&self) -> bool

Returns true if no entries are set to false.

§Examples
let mut set = BitSet::new();
assert!(set.is_empty());

set.insert(99);
assert!(!set.is_empty());

set.remove(99);
assert!(set.is_empty());
Source

pub fn element_count(&self) -> usize

Returns the amount of entries set to true.

§Examples
let mut set = BitSet::new();
assert_eq!(0, set.element_count());

set.insert(1729);
set.insert(1337);
assert_eq!(2, set.element_count());
Source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity as much as possible.

While the capacity will drop down as close as possible to the biggest set idx, there might still be space for a few more elements.

§Examples
let mut set = BitSet::with_capacity(1000);
set.extend([100, 200, 300].iter());
assert!(set.capacity() >= 1000);

set.shrink_to_fit();
assert!(set.capacity() > 300);
Source

pub fn set(&mut self, idx: usize, value: bool)

Sets the entry at idx to value.

§Examples
let mut set = BitSet::new();

set.set(1337, true);
assert_eq!(true, set.get(1337));

set.set(1337, false);
assert_eq!(false, set.get(1337));
Source

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

Sets the entry at idx to true.

§Examples
let mut set = BitSet::new();

set.insert(69);
assert_eq!(true, set.get(69));

set.remove(69);
assert_eq!(false, set.get(69));
Source

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

Sets the entry at idx to false.

§Examples
let mut set = BitSet::new();

set.insert(42);
assert_eq!(true, set.get(42));

set.remove(42);
assert_eq!(false, set.get(42));
Source

pub fn flip(&mut self, idx: usize)

Inverts the value of the entry at idx.

§Examples
let mut set = BitSet::new();
assert_eq!(false, set.get(42));

set.flip(42);
assert_eq!(true, set.get(42));

set.flip(42);
assert_eq!(false, set.get(42));
Source

pub fn get(&self, idx: usize) -> bool

Returns the value of the entry at idx.

§Examples
let mut set = BitSet::new();
assert_eq!(false, set.get(7));

set.insert(6);
assert_eq!(false, set.get(7));

set.insert(7);
assert_eq!(true, set.get(7));
Source

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

Returns if self and other do not share a true element with the same index. This is equivalent to checking for an empty intersection.

§Examples
let a: BitSet = [1, 2, 3].iter().collect();
let mut b = BitSet::new();

assert_eq!(a.is_disjoint(&b), true);
b.insert(4);
assert_eq!(a.is_disjoint(&b), true);
b.insert(1);
assert_eq!(a.is_disjoint(&b), false);
Source

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

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

§Examples
let sup: BitSet = [1, 2, 3].iter().collect();
let mut set = BitSet::new();

assert_eq!(set.is_subset(&sup), true);
set.insert(2);
assert_eq!(set.is_subset(&sup), true);
set.insert(4);
assert_eq!(set.is_subset(&sup), false);
Source

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

Returns true if self is a superset of other, meaning self contains at least all the values in other.

§Examples
let sup: BitSet = [1, 2].iter().collect();
let mut set = BitSet::new();

assert_eq!(set.is_superset(&sup), false);

set.insert(0);
set.insert(1);
assert_eq!(set.is_superset(&sup), false);

set.insert(2);
assert_eq!(set.is_superset(&sup), true);
Source

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

Returns the highest index for which the entry which is set to true. In case there is no true entry, this method returns None.

§Examples
let mut set = BitSet::new();
assert_eq!(None, set.highest_bit());

set.insert(3);
set.insert(7);
set.insert(4);
assert_eq!(Some(7), set.highest_bit());
Source

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

Returns the lowest index for which the entry is set to true. In case there is no true entry, this method returns None.

§Examples
let mut set = BitSet::new();
assert_eq!(None, set.lowest_bit());

set.insert(3);
set.insert(7);
set.insert(4);
assert_eq!(Some(3), set.lowest_bit());
Source

pub fn iter(&self) -> IdxIter<&Self>

Returns an iterator over the bitset which returns all indices of entries set to true. The indices are sorted from lowest to highest.

§Examples
let set: BitSet = [1, 12, 19, 4].iter().copied().collect();
let mut iter = set.iter();

assert_eq!(Some(1), iter.next());
assert_eq!(Some(4), iter.next());
assert_eq!(Some(12), iter.next());
assert_eq!(Some(19), iter.next());
assert_eq!(None, iter.next());

Trait Implementations§

Source§

impl Clone for BitSet

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 BitSet

Source§

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

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

impl Default for BitSet

Source§

fn default() -> BitSet

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

impl<'a> Extend<&'a usize> for BitSet

Source§

fn extend<I: IntoIterator<Item = &'a 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 Extend<usize> for BitSet

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<'a> FromIterator<&'a bool> for BitSet

Source§

fn from_iter<U: IntoIterator<Item = &'a bool>>(iter: U) -> BitSet

Creates a value from an iterator. Read more
Source§

impl<'a> FromIterator<&'a usize> for BitSet

Source§

fn from_iter<U: IntoIterator<Item = &'a usize>>(iter: U) -> BitSet

Creates a value from an iterator. Read more
Source§

impl FromIterator<bool> for BitSet

Source§

fn from_iter<U: IntoIterator<Item = bool>>(iter: U) -> BitSet

Creates a value from an iterator. Read more
Source§

impl FromIterator<usize> for BitSet

Source§

fn from_iter<U: IntoIterator<Item = usize>>(iter: U) -> BitSet

Creates a value from an iterator. Read more
Source§

impl IntoIterator for BitSet

Source§

type Item = usize

The type of the elements being iterated over.
Source§

type IntoIter = IdxIter<BitSet>

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

fn into_iter(self) -> IdxIter<BitSet>

Creates an iterator from a value. Read more
Source§

impl PartialEq for BitSet

Source§

fn eq(&self, rhs: &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 Eq for BitSet

Auto Trait Implementations§

§

impl Freeze for BitSet

§

impl RefUnwindSafe for BitSet

§

impl Send for BitSet

§

impl Sync for BitSet

§

impl Unpin for BitSet

§

impl UnwindSafe for BitSet

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 T)

🔬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, 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.