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>
impl<T> SparseArray<'_, T>
Sourcepub fn new<'a>(
allocator: &'a LinearAllocator<'_>,
array_len: u32,
loaded_len: u32,
) -> Option<SparseArray<'a, T>>
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.
Sourcepub fn increment_ages(&mut self)
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.
Sourcepub fn unload(&mut self, index: u32)
pub fn unload(&mut self, index: u32)
Removes the value from the index, freeing space for another value to be inserted anywhere.
Sourcepub fn insert(
&mut self,
index: u32,
init_fn: impl FnOnce() -> Option<T>,
) -> Option<&mut T>
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
.