imbl_sized_chunks::sparse_chunk

Struct SparseChunk

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

Source§

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,

Source§

type Item = A

The type of the elements being iterated over.
Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
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> !Freeze for SparseChunk<A, N>

§

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

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

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