cranelift_bitset::scalar

Struct ScalarBitSet

Source
pub struct ScalarBitSet<T>(pub T);
Expand description

A small bitset built on top of a single primitive integer type.

§Example

use cranelift_bitset::ScalarBitSet;

// Create a new bitset backed with a `u32`.
let mut bitset = ScalarBitSet::<u32>::new();

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

// Insert into the bitset.
bitset.insert(4);
bitset.insert(5);
bitset.insert(6);

// Now the bitset is not empty.
assert_eq!(bitset.len(), 3);
assert!(!bitset.is_empty());
assert!(bitset.contains(4));
assert!(bitset.contains(5));
assert!(bitset.contains(6));

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

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

Tuple Fields§

§0: T

Implementations§

Source§

impl<T> ScalarBitSet<T>

Source

pub fn new() -> Self

Create a new, empty bitset.

§Example
use cranelift_bitset::ScalarBitSet;

let bitset = ScalarBitSet::<u64>::new();

assert!(bitset.is_empty());
Source

pub fn from_range(lo: u8, hi: u8) -> Self

Construct a bitset with the half-open range [lo, hi) inserted.

§Example
use cranelift_bitset::ScalarBitSet;

let bitset = ScalarBitSet::<u64>::from_range(3, 6);

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

assert!(bitset.contains(3));
assert!(bitset.contains(4));
assert!(bitset.contains(5));
§Panics

Panics if lo > hi or if hi > Self::capacity().

use cranelift_bitset::ScalarBitSet;

// The lower bound may not be larger than the upper bound.
let bitset = ScalarBitSet::<u64>::from_range(6, 3);
use cranelift_bitset::ScalarBitSet;

// The bounds must fit within the backing scalar type.
let bitset = ScalarBitSet::<u64>::from_range(3, 69);
Source

pub fn capacity() -> u8

The maximum number of bits that can be stored in this bitset.

If you need more bits than this, use a CompoundBitSet instead of a ScalarBitSet.

§Example
use cranelift_bitset::ScalarBitSet;

assert_eq!(ScalarBitSet::<u8>::capacity(), 8);
assert_eq!(ScalarBitSet::<u64>::capacity(), 64);
Source

pub fn len(&self) -> u8

Get the number of elements in this set.

§Example
use cranelift_bitset::ScalarBitSet;

let mut bitset = ScalarBitSet::<u64>::new();

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

bitset.insert(24);
bitset.insert(13);
bitset.insert(36);

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

pub fn is_empty(&self) -> bool

Is this bitset empty?

§Example
use cranelift_bitset::ScalarBitSet;

let mut bitset = ScalarBitSet::<u16>::new();

assert!(bitset.is_empty());

bitset.insert(10);

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

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

Check whether this bitset contains i.

§Example
use cranelift_bitset::ScalarBitSet;

let mut bitset = ScalarBitSet::<u8>::new();

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

bitset.insert(7);

assert!(bitset.contains(7));
§Panics

Panics if i is greater than or equal to Self::capacity().

use cranelift_bitset::ScalarBitSet;

let mut bitset = ScalarBitSet::<u8>::new();

// A `ScalarBitSet<u8>` can only hold the elements `0..=7`, so `8` is
// out of bounds and will trigger a panic.
bitset.contains(8);
Source

pub fn insert(&mut self, i: u8) -> 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::ScalarBitSet;

let mut bitset = ScalarBitSet::<u8>::new();

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

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

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

Panics if i is greater than or equal to Self::capacity().

use cranelift_bitset::ScalarBitSet;

let mut bitset = ScalarBitSet::<u32>::new();

// A `ScalarBitSet<u32>` can only hold the elements `0..=31`, so `42` is
// out of bounds and will trigger a panic.
bitset.insert(42);
Source

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

Remove i from this bitset.

Returns whether i was previously in this set or not.

§Example
use cranelift_bitset::ScalarBitSet;

let mut bitset = ScalarBitSet::<u128>::new();

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

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

Panics if i is greater than or equal to Self::capacity().

use cranelift_bitset::ScalarBitSet;

let mut bitset = ScalarBitSet::<u16>::new();

// A `ScalarBitSet<u16>` can only hold the elements `0..=15`, so `20` is
// out of bounds and will trigger a panic.
bitset.remove(20);
Source

pub fn clear(&mut self)

Remove all entries from this bitset.

§Example
use cranelift_bitset::ScalarBitSet;

let mut bitset = ScalarBitSet::<u32>::new();

bitset.insert(10);
bitset.insert(20);
bitset.insert(30);

bitset.clear();

assert!(bitset.is_empty());
Source

pub fn pop_min(&mut self) -> Option<u8>

Remove and return the smallest value in the bitset.

§Example
use cranelift_bitset::ScalarBitSet;

let mut bitset = ScalarBitSet::<u64>::new();

bitset.insert(0);
bitset.insert(24);
bitset.insert(13);
bitset.insert(36);

assert_eq!(bitset.pop_min(), Some(0));
assert_eq!(bitset.pop_min(), Some(13));
assert_eq!(bitset.pop_min(), Some(24));
assert_eq!(bitset.pop_min(), Some(36));
assert_eq!(bitset.pop_min(), None);
Source

pub fn pop_max(&mut self) -> Option<u8>

Remove and return the largest value in the bitset.

§Example
use cranelift_bitset::ScalarBitSet;

let mut bitset = ScalarBitSet::<u64>::new();

bitset.insert(0);
bitset.insert(24);
bitset.insert(13);
bitset.insert(36);

assert_eq!(bitset.pop_max(), Some(36));
assert_eq!(bitset.pop_max(), Some(24));
assert_eq!(bitset.pop_max(), Some(13));
assert_eq!(bitset.pop_max(), Some(0));
assert_eq!(bitset.pop_max(), None);
Source

pub fn min(&self) -> Option<u8>

Return the smallest number contained in this bitset or None if this bitset is empty.

§Example
use cranelift_bitset::ScalarBitSet;

let mut bitset = ScalarBitSet::<u64>::new();

// When the bitset is empty, `min` returns `None`.
assert_eq!(bitset.min(), None);

bitset.insert(28);
bitset.insert(1);
bitset.insert(63);

// When the bitset is not empty, it returns the smallest element.
assert_eq!(bitset.min(), Some(1));
Source

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

Return the largest number contained in the bitset or None if this bitset is empty

§Example
use cranelift_bitset::ScalarBitSet;

let mut bitset = ScalarBitSet::<u64>::new();

// When the bitset is empty, `max` returns `None`.
assert_eq!(bitset.max(), None);

bitset.insert(0);
bitset.insert(36);
bitset.insert(49);

// When the bitset is not empty, it returns the smallest element.
assert_eq!(bitset.max(), Some(49));
Source

pub fn iter(self) -> Iter<T>

Iterate over the items in this set.

Items are always yielded in sorted order.

§Example
use cranelift_bitset::ScalarBitSet;

let mut bitset = ScalarBitSet::<u64>::new();

bitset.insert(19);
bitset.insert(3);
bitset.insert(63);
bitset.insert(0);

assert_eq!(
    bitset.iter().collect::<Vec<_>>(),
    [0, 3, 19, 63],
);

Trait Implementations§

Source§

impl<T: Clone> Clone for ScalarBitSet<T>

Source§

fn clone(&self) -> ScalarBitSet<T>

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<T> Debug for ScalarBitSet<T>

Source§

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

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

impl<T> Default for ScalarBitSet<T>

Source§

fn default() -> Self

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

impl<T: ScalarBitSetStorage> From<T> for ScalarBitSet<T>

Source§

fn from(bits: T) -> Self

Converts to this type from the input type.
Source§

impl<'a, T> IntoIterator for &'a ScalarBitSet<T>

Source§

type Item = u8

The type of the elements being iterated over.
Source§

type IntoIter = Iter<T>

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<T> IntoIterator for ScalarBitSet<T>

Source§

type Item = u8

The type of the elements being iterated over.
Source§

type IntoIter = Iter<T>

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<T: PartialEq> PartialEq for ScalarBitSet<T>

Source§

fn eq(&self, other: &ScalarBitSet<T>) -> 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<T: Copy> Copy for ScalarBitSet<T>

Source§

impl<T: Eq> Eq for ScalarBitSet<T>

Source§

impl<T> StructuralPartialEq for ScalarBitSet<T>

Auto Trait Implementations§

§

impl<T> Freeze for ScalarBitSet<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for ScalarBitSet<T>
where T: RefUnwindSafe,

§

impl<T> Send for ScalarBitSet<T>
where T: Send,

§

impl<T> Sync for ScalarBitSet<T>
where T: Sync,

§

impl<T> Unpin for ScalarBitSet<T>
where T: Unpin,

§

impl<T> UnwindSafe for ScalarBitSet<T>
where T: 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, 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, 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.