Struct SparseSet

Source
pub struct SparseSet<T> { /* private fields */ }
Expand description

A container based on Sparse Set, that stores a set of items and provides a way to efficiently access them by a generated key.

Usage-wise it works similarly to an array, with exceptions that keys stay stable even after removals, and operations like insertions and removals have slight overhead. Also, it has higher memory consumption, since it needs to store additional data for each element.

Good for cache efficiency. Doesn’t require any hashing. Can’t be serialized.

Insertions are O(1) amortized. Removals are O(1) if the order of elements can be changed, O(n) if the order must be preserved. Accessing elements is O(1).

Extra memory consumption for each value is 4 * sizeof(usize) bytes on top of the size of the value (e.g. 32 bytes per element on 64-bit systems). The memory consumption will also grow by 2 * sizeof(usize) per 2^(sizeof(usize) * 8) elements removed (e.g. 16 bytes per 18446744073709551616 elements removed on 64-bit systems).

ZST (zero-sized types) are not supported, trying to use them will result in a panic.

Implementations§

Source§

impl<T> SparseSet<T>

Source

pub fn new() -> Self

Creates a new SparseSet. Does not allocate.

§Panics

Panics if the type T is zero-sized.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new SparseSet with allocated memory for the given number of elements.

§Panics
  • Panics if the type T is zero-sized.
  • Panics if the memory allocation fails.
Source

pub fn push(&mut self, value: T) -> SparseKey

Inserts a new value into the set and returns a key that can be used to access it.

This can heap-allocate (if the internal arrays need to grow) but it won’t invalidate any existing keys. If some objects were removed before, it will reclaim the previously freed space.

O(1) amortized time complexity.

§Panics

Panics if a memory allocation fails.

Source

pub fn swap_remove(&mut self, key: SparseKey) -> Option<T>

Removes an element from the set using the key, swapping it with the last element. Returns the removed value if it was present in the set.

O(1) time complexity, however changes the order of elements.

§Panics

Can panic if the used key is not from this SparseSet.

Source

pub fn remove(&mut self, key: SparseKey) -> Option<T>

Removes an element from the set using the key, keeping the order of elements. Returns the removed value if it was present in the set.

O(n) time complexity, however doesn’t change the order of elements.

§Panics

Can panic if the used key is not from this SparseSet.

Source

pub fn swap(&mut self, key1: SparseKey, key2: SparseKey)

Swaps two elements in the set using their keys.

O(1) time complexity.

§Panics
  • Panics if any of the keys are not present in the set (were removed)
  • Can panic if the used keys are not from this SparseSet.
Source

pub fn get(&self, key: SparseKey) -> Option<&T>

Returns a reference to the value stored at the given key. If the key is not valid, returns None.

O(1) time complexity.

§Panics

Can panic if the used key is not from this SparseSet.

Source

pub fn get_mut(&mut self, key: SparseKey) -> Option<&mut T>

Returns a mutable reference to the value stored at the given key. If the key is not valid, returns None.

O(1) time complexity.

§Panics

Can panic if the used key is not from this SparseSet.

Source

pub fn contains(&self, key: SparseKey) -> bool

Returns true if the key points to a valid element in the set.

O(1) time complexity.

§Panics

Can panic if the used key is not from this SparseSet.

Source

pub fn size(&self) -> usize

Returns the number of elements in the set.

O(1) time complexity.

Source

pub fn is_empty(&self) -> bool

Returns true if the set is empty.

O(1) time complexity.

Source

pub fn values(&self) -> impl DoubleEndedIterator<Item = &T>

Returns an iterator over the values of the set.

Source

pub fn values_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut T>

Returns an iterator over the mutable values of the set.

Source

pub fn keys(&self) -> impl DoubleEndedIterator<Item = SparseKey> + '_

Returns an iterator over the keys of the set.

Source

pub fn key_values(&self) -> impl DoubleEndedIterator<Item = (SparseKey, &T)>

Returns an iterator over the key-value pairs of the set.

Note that if you intend to iterate over key-values in time-critical code, it may be better to instead store the keys in the elements themselves to reduce CPU cache misses.

Trait Implementations§

Source§

impl<T: Clone> Clone for SparseSet<T>

Source§

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

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<T> Freeze for SparseSet<T>

§

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

§

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

§

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

§

impl<T> Unpin for SparseSet<T>

§

impl<T> UnwindSafe for SparseSet<T>
where T: RefUnwindSafe,

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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.