cranelift_bitset::compound

Struct CompoundBitSet

source
pub struct CompoundBitSet { /* private fields */ }
Expand description

A large bit set backed by dynamically-sized storage.

§Example

use cranelift_bitset::CompoundBitSet;

// Create a new bitset.
let mut bitset = CompoundBitSet::new();

// Bitsets are initially empty.
assert!(bitset.is_empty());
assert_eq!(bitset.len(), 0);

// Insert into the bitset.
bitset.insert(444);
bitset.insert(555);
bitset.insert(666);

// Now the bitset is not empty.
assert_eq!(bitset.len(), 3);
assert!(!bitset.is_empty());
assert!(bitset.contains(444));
assert!(bitset.contains(555));
assert!(bitset.contains(666));

// Remove an element from the bitset.
let was_present = bitset.remove(666);
assert!(was_present);
assert!(!bitset.contains(666));
assert_eq!(bitset.len(), 2);

// Can iterate over the elements in the set.
let elems: Vec<_> = bitset.iter().collect();
assert_eq!(elems, [444, 555]);

Implementations§

source§

impl CompoundBitSet

source

pub fn new() -> Self

Construct a new, empty bit set.

§Example
use cranelift_bitset::CompoundBitSet;

let bitset = CompoundBitSet::new();

assert!(bitset.is_empty());
source

pub fn with_capacity(capacity: usize) -> Self

Construct a new, empty bit set with space reserved to store any element x such that x < capacity.

The actual capacity reserved may be greater than that requested.

§Example
use cranelift_bitset::CompoundBitSet;

let bitset = CompoundBitSet::with_capacity(4096);

assert!(bitset.is_empty());
assert!(bitset.capacity() >= 4096);
source

pub fn len(&self) -> usize

Get the number of elements in this bitset.

§Example
use cranelift_bitset::CompoundBitSet;

let mut bitset = CompoundBitSet::new();

assert_eq!(bitset.len(), 0);

bitset.insert(24);
bitset.insert(130);
bitset.insert(3600);

assert_eq!(bitset.len(), 3);
source

pub fn capacity(&self) -> usize

Get n + 1 where n is the largest value that can be stored inside this set without growing the backing storage.

That is, this set can store any value x such that x < bitset.capacity() without growing the backing storage.

§Example
use cranelift_bitset::CompoundBitSet;

let mut bitset = CompoundBitSet::new();

// New bitsets have zero capacity -- they allocate lazily.
assert_eq!(bitset.capacity(), 0);

// Insert into the bitset, growing its capacity.
bitset.insert(999);

// The bitset must now have capacity for at least `999` elements,
// perhaps more.
assert!(bitset.capacity() >= 999);
source

pub fn is_empty(&self) -> bool

Is this bitset empty?

§Example
use cranelift_bitset::CompoundBitSet;

let mut bitset = CompoundBitSet::new();

assert!(bitset.is_empty());

bitset.insert(1234);

assert!(!bitset.is_empty());
source

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

Is i contained in this bitset?

§Example
use cranelift_bitset::CompoundBitSet;

let mut bitset = CompoundBitSet::new();

assert!(!bitset.contains(666));

bitset.insert(666);

assert!(bitset.contains(666));
source

pub fn ensure_capacity(&mut self, n: usize)

Ensure there is space in this bitset for the values 0..n, growing the backing storage if necessary.

After calling bitset.ensure_capacity(n), inserting any element i where i < n is guaranteed to succeed without growing the bitset’s backing storage.

§Example
// We are going to do a series of inserts where `1000` will be the
// maximum value inserted. Make sure that our bitset has capacity for
// these elements once up front, to avoid growing the backing storage
// multiple times incrementally.
bitset.ensure_capacity(1001);

for i in 0..=1000 {
    if i % 2 == 0 {
        // Inserting this value should not require growing the backing
        // storage.
        assert!(bitset.capacity() > i);
        bitset.insert(i);
    }
}
source

pub fn insert(&mut self, i: usize) -> bool

Insert i into this bitset.

Returns whether the value was newly inserted. That is:

  • If the set did not previously contain i then true is returned.

  • If the set already contained i then false is returned.

§Example
use cranelift_bitset::CompoundBitSet;

let mut bitset = CompoundBitSet::new();

// When an element is inserted that was not already present in the set,
// then `true` is returned.
let is_new = bitset.insert(1234);
assert!(is_new);

// The element is now present in the set.
assert!(bitset.contains(1234));

// And when the element is already in the set, `false` is returned from
// `insert`.
let is_new = bitset.insert(1234);
assert!(!is_new);
source

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

Remove i from this bitset.

Returns whether i was previously in this set or not.

§Example
use cranelift_bitset::CompoundBitSet;

let mut bitset = CompoundBitSet::new();

// Removing an element that was not present in the set returns `false`.
let was_present = bitset.remove(456);
assert!(!was_present);

// And when the element was in the set, `true` is returned.
bitset.insert(456);
let was_present = bitset.remove(456);
assert!(was_present);
source

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

Get the largest value in this set, or None if this set is empty.

§Example
use cranelift_bitset::CompoundBitSet;

let mut bitset = CompoundBitSet::new();

// Returns `None` if the bitset is empty.
assert!(bitset.max().is_none());

bitset.insert(123);
bitset.insert(987);
bitset.insert(999);

// Otherwise, it returns the largest value in the set.
assert_eq!(bitset.max(), Some(999));
source

pub fn pop(&mut self) -> Option<usize>

Removes and returns the largest value in this set.

Returns None if this set is empty.

§Example
use cranelift_bitset::CompoundBitSet;

let mut bitset = CompoundBitSet::new();

bitset.insert(111);
bitset.insert(222);
bitset.insert(333);
bitset.insert(444);
bitset.insert(555);

assert_eq!(bitset.pop(), Some(555));
assert_eq!(bitset.pop(), Some(444));
assert_eq!(bitset.pop(), Some(333));
assert_eq!(bitset.pop(), Some(222));
assert_eq!(bitset.pop(), Some(111));
assert_eq!(bitset.pop(), None);
source

pub fn clear(&mut self)

Remove all elements from this bitset.

§Example
use cranelift_bitset::CompoundBitSet;

let mut bitset = CompoundBitSet::new();

bitset.insert(100);
bitset.insert(200);
bitset.insert(300);

bitset.clear();

assert!(bitset.is_empty());
source

pub fn iter(&self) -> Iter<'_>

Iterate over the elements in this bitset.

The elements are always yielded in sorted order.

§Example
use cranelift_bitset::CompoundBitSet;

let mut bitset = CompoundBitSet::new();

bitset.insert(0);
bitset.insert(4096);
bitset.insert(123);
bitset.insert(456);
bitset.insert(789);

assert_eq!(
    bitset.iter().collect::<Vec<_>>(),
    [0, 123, 456, 789, 4096],
);

Trait Implementations§

source§

impl Clone for CompoundBitSet

source§

fn clone(&self) -> CompoundBitSet

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for CompoundBitSet

source§

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

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

impl Default for CompoundBitSet

source§

fn default() -> CompoundBitSet

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

impl<'de> Deserialize<'de> for CompoundBitSet

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<'a> IntoIterator for &'a CompoundBitSet

source§

type Item = usize

The type of the elements being iterated over.
source§

type IntoIter = Iter<'a>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl PartialEq for CompoundBitSet

source§

fn eq(&self, other: &CompoundBitSet) -> 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 Serialize for CompoundBitSet

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

source§

impl StructuralPartialEq for CompoundBitSet

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 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.
source§

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