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,

source

pub const CAPACITY: usize = N

The maximum number of elements a SparseChunk can contain.

source

pub fn new() -> Self

Construct a new empty chunk.

source

pub fn unit(index: usize, value: A) -> Self

Construct a new chunk with one item.

source

pub fn pair(index1: usize, value1: A, index2: usize, value2: A) -> Self

Construct a new chunk with two items.

source

pub fn len(&self) -> usize

Get the length of the chunk.

source

pub fn is_empty(&self) -> bool

Test if the chunk is empty.

source

pub fn is_full(&self) -> bool

Test if the chunk is at capacity.

source

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.

source

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.

source

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.

source

pub fn get(&self, index: usize) -> Option<&A>

Get the value at a given index.

source

pub fn get_mut(&mut self, index: usize) -> Option<&mut A>

Get a mutable reference to the value at a given index.

source

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.

source

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.

source

pub fn indices(&self) -> BitmapIter<'_, N>

Make an iterator over the indices which contain values.

source

pub fn first_index(&self) -> Option<usize>

Find the first index which contains a value.

source

pub fn iter(&self) -> Iter<'_, A, N>

Make an iterator of references to the values contained in the array.

source

pub fn iter_mut(&mut self) -> IterMut<'_, A, N>

Make an iterator of mutable references to the values contained in the array.

source

pub fn drain(self) -> Drain<A, N>

Turn the chunk into an iterator over the values contained within it.

source

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.

source

pub fn option_iter(&self) -> OptionIter<'_, A, N>

Make an iterator of Options 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.

source

pub fn option_iter_mut(&mut self) -> OptionIterMut<'_, A, N>

Make an iterator of Options 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.

source

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.

Trait Implementations§

source§

impl<'a, A, const N: usize> Arbitrary<'a> for SparseChunk<A, N>
where A: Clone, Option<A>: Arbitrary<'a>, BitsImpl<N>: Bits,

source§

fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>

Generate an arbitrary value of Self from the given unstructured data. Read more
source§

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self>

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more
source§

fn size_hint(depth: usize) -> (usize, Option<usize>)

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
source§

impl<A: Clone, const N: usize> Clone for SparseChunk<A, N>
where BitsImpl<N>: Bits,

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<A, const N: usize> Debug for SparseChunk<A, N>
where A: Debug, BitsImpl<N>: Bits,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl<A, const N: usize> Default for SparseChunk<A, N>
where BitsImpl<N>: Bits,

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<A, const N: usize> Drop for SparseChunk<A, N>
where BitsImpl<N>: Bits,

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<A, const N: usize> FromIterator<Option<A>> for SparseChunk<A, N>
where BitsImpl<N>: Bits,

source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = Option<A>>,

Creates a value from an iterator. Read more
source§

impl<A, const N: usize> Index<usize> for SparseChunk<A, N>
where BitsImpl<N>: Bits,

§

type Output = A

The returned type after indexing.
source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<A, const N: usize> IndexMut<usize> for SparseChunk<A, N>
where BitsImpl<N>: Bits,

source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl<A, const N: usize> IntoIterator for SparseChunk<A, N>
where BitsImpl<N>: Bits,

§

type Item = A

The type of the elements being iterated over.
§

type IntoIter = Drain<A, N>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<A, const N: usize> PartialEq<BTreeMap<usize, A>> for SparseChunk<A, N>
where A: PartialEq, BitsImpl<N>: Bits,

source§

fn eq(&self, other: &BTreeMap<usize, A>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<A, const N: usize> PartialEq<HashMap<usize, A>> for SparseChunk<A, N>
where A: PartialEq, BitsImpl<N>: Bits,

source§

fn eq(&self, other: &HashMap<usize, A>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<A, const N: usize> PartialEq for SparseChunk<A, N>
where A: PartialEq, BitsImpl<N>: Bits,

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<A, const N: usize> PoolClone for SparseChunk<A, N>
where A: Clone, BitsImpl<N>: Bits,

source§

unsafe fn clone_uninit(&self, target: &mut MaybeUninit<Self>)

Clone an instance of Self into an uninitialised instance of Self. Read more
source§

impl<A, const N: usize> PoolDefault for SparseChunk<A, N>
where BitsImpl<N>: Bits,

source§

unsafe fn default_uninit(target: &mut MaybeUninit<Self>)

Initialise an instance of Self to its default state. Read more
source§

impl<A, const N: usize> Eq for SparseChunk<A, N>
where A: Eq, BitsImpl<N>: Bits,

Auto Trait Implementations§

§

impl<A, const N: usize> !RefUnwindSafe for SparseChunk<A, N>

§

impl<A, const N: usize> !Send for SparseChunk<A, N>

§

impl<A, const N: usize> !Sync for SparseChunk<A, N>

§

impl<A, const N: usize> !Unpin for SparseChunk<A, N>

§

impl<A, const N: usize> !UnwindSafe for SparseChunk<A, N>

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> ToOwned for T
where T: Clone,

§

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>,

§

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>,

§

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.