Struct thunderdome::Arena

source ·
pub struct Arena<T> { /* private fields */ }
Expand description

Container that can have elements inserted into it and removed from it.

Indices use the Index type, created by inserting values with Arena::insert.

Implementations§

source§

impl<T> Arena<T>

source

pub const fn new() -> Self

Construct an empty arena.

source

pub fn with_capacity(capacity: usize) -> Self

Construct an empty arena with space to hold exactly capacity elements without reallocating.

source

pub const fn len(&self) -> usize

Return the number of elements contained in the arena.

source

pub fn capacity(&self) -> usize

Return the number of elements the arena can hold without allocating, including the elements currently in the arena.

source

pub const fn is_empty(&self) -> bool

Returns whether the arena is empty.

source

pub fn insert(&mut self, value: T) -> Index

Insert a new value into the arena, returning an index that can be used to later retrieve the value.

source

pub fn insert_at(&mut self, index: Index, value: T) -> Option<T>

Insert a new value at a given index, returning the old value if present. The entry’s generation is set to the given index’s generation.

Caveats

This method is capable of “resurrecting” an old Index. This is unavoidable; if we already have an occupied entry (or had) at this index of some generation M, and then insert_at that same slot but with a generation N < M, eventually after some number of insertions and removals it is possible we could end up with an index matching that old index. There are few cases where this is likely to be a problem, but it is still possible.

source

pub fn insert_at_slot(&mut self, slot: u32, value: T) -> (Index, Option<T>)

Insert a new value at a given slot, returning the old value if present. If the slot is already occupied, this will increment the generation of the slot, and invalidate any previous indices pointing to it.

source

pub fn contains(&self, index: Index) -> bool

Returns true if the given index is valid for the arena.

source

pub fn contains_slot(&self, slot: u32) -> Option<Index>

Checks to see whether a slot is occupied in the arena, and if it is, returns Some with the true Index of that slot (slot plus generation.) Otherwise, returns None.

source

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

Get an immutable reference to a value inside the arena by Index, returning None if the index is not contained in the arena.

source

pub fn get_mut(&mut self, index: Index) -> Option<&mut T>

Get a mutable reference to a value inside the arena by Index, returning None if the index is not contained in the arena.

source

pub fn get2_mut( &mut self, index1: Index, index2: Index ) -> (Option<&mut T>, Option<&mut T>)

Get mutable references of two values inside this arena at once by Index, returning None if the corresponding index is not contained in this arena.

Panics

This function panics when the two indices are equal (having the same slot number and generation).

source

pub fn remove(&mut self, index: Index) -> Option<T>

Remove the value contained at the given index from the arena, returning it if it was present.

source

pub fn invalidate(&mut self, index: Index) -> Option<Index>

Invalidate the given index and return a new index to the same value. This is roughly equivalent to remove followed by insert, but much faster. If the old index is already invalid, this method returns None.

source

pub fn get_by_slot(&self, slot: u32) -> Option<(Index, &T)>

Attempt to look up the given slot in the arena, disregarding any generational information, and retrieve an immutable reference to it. Returns None if the slot is empty.

source

pub fn get_by_slot_mut(&mut self, slot: u32) -> Option<(Index, &mut T)>

Attempt to look up the given slot in the arena, disregarding any generational information, and retrieve a mutable reference to it. Returns None if the slot is empty.

source

pub fn remove_by_slot(&mut self, slot: u32) -> Option<(Index, T)>

Remove an entry in the arena by its slot, disregarding any generational info. Returns None if the slot was already empty.

source

pub fn clear(&mut self)

Clear the arena and drop all elements.

source

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

Iterate over all of the indexes and values contained in the arena.

Iteration order is not defined.

source

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

Iterate over all of the indexes and values contained in the arena, with mutable access to each value.

Iteration order is not defined.

source

pub fn drain(&mut self) -> Drain<'_, T>

Returns an iterator that removes each element from the arena.

Iteration order is not defined.

If the iterator is dropped before it is fully consumed, any uniterated items will be dropped from the arena, and the arena will be empty. The arena’s capacity will not be changed.

source

pub fn retain<F: FnMut(Index, &mut T) -> bool>(&mut self, f: F)

Remove all entries in the Arena which don’t satisfy the provided predicate.

Trait Implementations§

source§

impl<T: Clone> Clone for Arena<T>

source§

fn clone(&self) -> Arena<T>

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<T: Debug> Debug for Arena<T>

source§

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

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

impl<T> Default for Arena<T>

source§

fn default() -> Self

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

impl<T> Index<Index> for Arena<T>

§

type Output = T

The returned type after indexing.
source§

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

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

impl<T> IndexMut<Index> for Arena<T>

source§

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

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

impl<'a, T> IntoIterator for &'a Arena<T>

§

type Item = (Index, &'a T)

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, T>

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, T> IntoIterator for &'a mut Arena<T>

§

type Item = (Index, &'a mut T)

The type of the elements being iterated over.
§

type IntoIter = IterMut<'a, T>

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<T> IntoIterator for Arena<T>

§

type Item = (Index, T)

The type of the elements being iterated over.
§

type IntoIter = IntoIter<T>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Arena<T>where T: RefUnwindSafe,

§

impl<T> Send for Arena<T>where T: Send,

§

impl<T> Sync for Arena<T>where T: Sync,

§

impl<T> Unpin for Arena<T>where T: Unpin,

§

impl<T> UnwindSafe for Arena<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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 Twhere 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.