Struct SparseArray

Source
pub struct SparseArray<'a, T> { /* private fields */ }
Expand description

Sparse array of T.

Can operate with less memory than a FixedVec of the same length, since the array length and the backing T storage have separate capacities. Useful for cases where stable indices are important, but having the entire array in-memory is infeasible.

Implementations§

Source§

impl<T> SparseArray<'_, T>

Source

pub fn new<'a>( allocator: &'a LinearAllocator<'_>, array_len: u32, loaded_len: u32, ) -> Option<SparseArray<'a, T>>

Creates a new sparse array of T with length array_len, allowing for loaded_len elements to be loaded at a time.

Source

pub fn increment_ages(&mut self)

Increments the age of each loaded element.

These ages are used to determine which elements get discarded first when calling SparseArray::insert. SparseArray::get resets the age of the returned element.

Source

pub fn unload(&mut self, index: u32)

Removes the value from the index, freeing space for another value to be inserted anywhere.

Source

pub fn insert( &mut self, index: u32, init_fn: impl FnOnce() -> Option<T>, ) -> Option<&mut T>

Allocates space for the index, returning a mutable borrow to fill it with.

If reusing an old unloaded T is not possible, but there’s and a new T needs to be created, init_fn is used. init_fn can fail by returning None, in which case nothing else happens and None is returned.

If the backing memory is full, the least recently used T is assigned to this index and returned, implicitly “unloading the value at its old index.”

If the backing memory is full, and every T has been used since the last call to SparseArray::increment_ages, this returns None.

Source

pub fn get(&self, index: u32) -> Option<&T>

Returns the value at the index if it’s loaded.

Source

pub fn array_len(&self) -> usize

Returns the length of the whole array (not the amount of loaded elements).

Auto Trait Implementations§

§

impl<'a, T> Freeze for SparseArray<'a, T>

§

impl<'a, T> RefUnwindSafe for SparseArray<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for SparseArray<'a, T>
where T: Send,

§

impl<'a, T> Sync for SparseArray<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for SparseArray<'a, T>

§

impl<'a, T> !UnwindSafe for SparseArray<'a, T>

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.