pub struct SparseChunk<A, const N: usize>where
BitsImpl<N>: Bits,{ /* private fields */ }
Expand description
A fixed capacity sparse array.
An inline sparse array of up to N
items of type A
. You can think of it as an array
of Option<A>
, where the discriminant (whether the value is Some<A>
or
None
) is kept in a bitmap instead of adjacent to the value.
§Examples
// Construct a chunk with a 20 item capacity
let mut chunk = SparseChunk::<i32, 20>::new();
// Set the 18th index to the value 5.
chunk.insert(18, 5);
// Set the 5th index to the value 23.
chunk.insert(5, 23);
assert_eq!(chunk.len(), 2);
assert_eq!(chunk.get(5), Some(&23));
assert_eq!(chunk.get(6), None);
assert_eq!(chunk.get(18), Some(&5));
Implementations§
Source§impl<A, const N: usize> SparseChunk<A, N>where
BitsImpl<N>: Bits,
impl<A, const N: usize> SparseChunk<A, N>where
BitsImpl<N>: Bits,
Sourcepub fn pair(index1: usize, value1: A, index2: usize, value2: A) -> Self
pub fn pair(index1: usize, value1: A, index2: usize, value2: A) -> Self
Construct a new chunk with two items.
Sourcepub fn insert(&mut self, index: usize, value: A) -> Option<A>
pub fn insert(&mut self, index: usize, value: A) -> Option<A>
Insert a new value at a given index.
Returns the previous value at that index, if any.
Sourcepub fn remove(&mut self, index: usize) -> Option<A>
pub fn remove(&mut self, index: usize) -> Option<A>
Remove the value at a given index.
Returns the value, or None
if the index had no value.
Sourcepub fn pop(&mut self) -> Option<A>
pub fn pop(&mut self) -> Option<A>
Remove the first value present in the array.
Returns the value that was removed, or None
if the array was empty.
Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut A>
pub fn get_mut(&mut self, index: usize) -> Option<&mut A>
Get a mutable reference to the value at a given index.
Sourcepub unsafe fn get_unchecked(&self, index: usize) -> &A
pub unsafe fn get_unchecked(&self, index: usize) -> &A
Get an unchecked reference to the value at a given index.
§Safety
Uninhabited indices contain uninitialised data, so make sure you validate the index before using this method.
Sourcepub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut A
pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut A
Get an unchecked mutable reference to the value at a given index.
§Safety
Uninhabited indices contain uninitialised data, so make sure you validate the index before using this method.
Sourcepub fn indices(&self) -> BitmapIter<'_, N>
pub fn indices(&self) -> BitmapIter<'_, N>
Make an iterator over the indices which contain values.
Sourcepub fn first_index(&self) -> Option<usize>
pub fn first_index(&self) -> Option<usize>
Find the first index which contains a value.
Sourcepub fn iter(&self) -> Iter<'_, A, N> ⓘ
pub fn iter(&self) -> Iter<'_, A, N> ⓘ
Make an iterator of references to the values contained in the array.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, A, N> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, A, N> ⓘ
Make an iterator of mutable references to the values contained in the array.
Sourcepub fn drain(self) -> Drain<A, N> ⓘ
pub fn drain(self) -> Drain<A, N> ⓘ
Turn the chunk into an iterator over the values contained within it.
Sourcepub fn entries(&self) -> impl Iterator<Item = (usize, &A)>
pub fn entries(&self) -> impl Iterator<Item = (usize, &A)>
Make an iterator of pairs of indices and references to the values contained in the array.
Sourcepub fn option_iter(&self) -> OptionIter<'_, A, N> ⓘ
pub fn option_iter(&self) -> OptionIter<'_, A, N> ⓘ
Make an iterator of Option
s of references to the values contained in the array.
Iterates over every index in the SparseChunk
, from zero to its full capacity,
returning an Option<&A>
for each index.
Sourcepub fn option_iter_mut(&mut self) -> OptionIterMut<'_, A, N> ⓘ
pub fn option_iter_mut(&mut self) -> OptionIterMut<'_, A, N> ⓘ
Make an iterator of Option
s of mutable references to the values contained in the array.
Iterates over every index in the SparseChunk
, from zero to its full capacity,
returning an Option<&mut A>
for each index.
Sourcepub fn option_drain(self) -> OptionDrain<A, N> ⓘ
pub fn option_drain(self) -> OptionDrain<A, N> ⓘ
Make a draining iterator of `Option’s of the values contained in the array.
Iterates over every index in the SparseChunk
, from zero to its full capacity,
returning an Option<A>
for each index.