ShrinkSet

Struct ShrinkSet 

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

A ShrinkSet is a set of integers that supports efficient removal and refilling.

ShrinkSets are automatically initialized such that they contain all integers up to, but not including, their capacity. For example, a ShrinkSet with a capacity of 5 will contain the integers 0, 1, 2, 3, and 4 upon initialization.

A ShrinkSet supports the following additional operations with the associated time complexity:

  • remove - remove an integer from the set in O(1) time
  • refill - adds all removed elements back into the set in O(1) time
  • pop - remove and return a random member of the set in O(1) time

ShrinkSets are useful for situations where we want to prune search spaces or work queues (for example) and then reset them to their initial state efficiently.

The algorithm used by ShrinkSet is believed to be novel, but if there is existing literature, please let me know so I can reference it here.

Implementations§

Source§

impl<'a> ShrinkSet

Source

pub fn new(size: usize) -> Self

Returns a ShrinkSet with the given capacity.

§Arguments
  • size - at creation time, the set will contain all integers up to, but not including, this value
§Examples
// This set stores all integers 0..=4.
use intset::ShrinkSet;
let mut set = ShrinkSet::new(5);
assert!(set.contains(3));
assert!(set.contains(4));
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::ShrinkSet;
let mut set = ShrinkSet::new(5);
set.remove(1);
set.remove(4);
assert_eq!(set.len(), 3);
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::ShrinkSet;
let mut set = ShrinkSet::new(5);
set.remove(1);
assert!(!set.contains(1));
assert!(set.contains(3));
Source

pub fn refill(&mut self)

Refills the set by adding all removed elements back.

§Examples
use intset::ShrinkSet;
let mut set = ShrinkSet::new(5);
set.remove(0);
set.remove(2);
assert!(set.contains(1));
assert!(set.contains(3));
assert!(set.contains(4));
assert!(!set.contains(0));
assert!(!set.contains(2));

set.refill();
assert!(set.contains(1));
assert!(set.contains(3));
assert!(set.contains(4));
assert!(set.contains(0));
assert!(set.contains(2));
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 remove(&mut self, item: usize) -> usize

Remove an element from the set.

§Arguments
  • item - the item to remove
§Examples
use intset::ShrinkSet;
let mut set = ShrinkSet::new(5);
set.remove(0);
set.remove(2);
assert!(set.contains(1));
assert!(set.contains(3));
assert!(!set.contains(0));
assert!(!set.contains(2));
Source

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

Remove and return a random element from the set in constant time, or None if the set is empty.

Trait Implementations§

Source§

impl Debug for ShrinkSet

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.