GrowSet

Struct GrowSet 

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

A GrowSet is a set of integers that supports efficient addition and clearing.

It supports the following additional operations with the associated complexity:

  • add - add an integer to the set in O(1) time
  • clear - remove all members from the set in O(1) time
  • pop - remove and return a random member of the set in O(1) time

GrowSets are useful for sets that need to be cleared frequently and rebuilt.

The implementation of GrowSet is based on “An Efficient Represtation of Sparse Sets” by Briggs and Torczon (1993).

Implementations§

Source§

impl<'a> GrowSet

Source

pub fn with_capacity(size: usize) -> Self

Returns a GrowSet with the given capacity.

§Arguments
  • size - the set can store all integers up to, but not including, this value.
§Examples
// This set can store the integers 0, 1, 2, 3, and 4.
use intset::GrowSet;
let mut set = GrowSet::with_capacity(5);
set.add(3);
assert!(set.contains(3));
Source

pub fn capacity(&self) -> usize

Returns one greater than the largest integer that can be stored in this set. That is, a set that can store integers from 0 to 5 will have a capacity of 6.

Source

pub fn len(&self) -> usize

Return the number of integers in the set.

§Examples
use intset::GrowSet;
let mut set = GrowSet::with_capacity(5);
set.add(1);
set.add(4);
assert_eq!(set.len(), 2);
Source

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

Returns true if the specified value is in the set.

§Arguments
  • value - the element to test
§Examples
use intset::GrowSet;
let mut set = GrowSet::with_capacity(5);
set.add(1);
assert!(set.contains(1));
assert!(!set.contains(3));
Source

pub fn clear(&mut self)

Remove all items from the set.

Source

pub fn add(&mut self, value: usize)

Add an item to the set. The item must be less than the capacity of the set.

§Arguments
  • value - the value to store
§Examples
use intset::GrowSet;
let mut set = GrowSet::with_capacity(100);
set.add(42);
assert!(set.contains(42));
Source

pub fn iter(&'a self) -> impl Iterator<Item = &usize> + 'a

Returns an iterator over the values in the set. Uniqueness is guaranteed; ordering is not.

Source

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

Returns a random element of the set in constant time, or None if the set is empty.

Trait Implementations§

Source§

impl Debug for GrowSet

Source§

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

Formats the value using the given formatter. Read more

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