Struct Tec

Source
pub struct Tec<IndexT, DataT> { /* private fields */ }
Expand description

Short for tombstone-based vector. Inspired by generational-arena, but without the generation stuff.

§Features

  • index stability when deleting an element
  • maintain freed list, and is basically free for large structs

Use case: you have compact data that needs to be inserted & deleted while other objects maintain their index-based references.

Don’t use it if:

  • the data are sparse (use a HashMap or Entities instead)
  • you don’t need to remove data (use a Vec with Sequence instead)
use stable_id::Tec;

// use the `derive_more` crate to shortern the list
#[derive(derive_stable_id::StableId, Debug)]
struct Id(u8);
struct Data { field: usize }

let mut storage: Tec<Id, Data> = Default::default();
assert_eq!(storage.alloc(Data {field: 123}), Id(0));
assert_eq!(storage.get(Id(0)).unwrap().field, 123);

Implementations§

Source§

impl<IndexT, DataT> Tec<IndexT, DataT>
where IndexT: CastUsize + Ord + Copy + Maximum,

Source

pub fn with_capacity(capacity: usize) -> Self

Source

pub fn len(&self) -> usize

Number of items in this data structure.

Source

pub fn is_empty(&self) -> bool

Source

pub fn clear(&mut self)

Source

pub fn alloc(&mut self, data: DataT) -> IndexT

Allocates an id from the given data. Note: can store at most IndexT::max_value() - 1 elements, because the next free node needs to be count + 1.

Source

pub fn remove(&mut self, index: IndexT) -> DataT

Panic if index is invalid

Source

pub fn get(&self, index: IndexT) -> Option<&DataT>

Source

pub fn get_mut(&mut self, index: IndexT) -> Option<&mut DataT>

Source

pub fn iter(&self) -> impl Iterator<Item = &DataT> + DoubleEndedIterator

Source

pub fn iter_with_id( &self, ) -> impl Iterator<Item = (IndexT, &DataT)> + DoubleEndedIterator

Source

pub fn iter_mut( &mut self, ) -> impl Iterator<Item = &mut DataT> + DoubleEndedIterator

Source

pub fn iter_mut_with_id( &mut self, ) -> impl Iterator<Item = (IndexT, &mut DataT)> + DoubleEndedIterator

Source

pub fn into_iter_with_id( self, ) -> impl Iterator<Item = (IndexT, DataT)> + DoubleEndedIterator

Source

pub fn capacity(&self) -> usize

The amount of occupied space in the underlying vec. Note:

self.len() <= self.capacity() == self.vec.len() <= self.vec.capacity()
Source

pub fn coalesce<F>(&mut self, f: F)
where F: FnMut(IndexT, IndexT),

Coalesce the data by removing the dead slots. Takes a function f(old_id, new_id) that allows you to deal with changes made by the process, i.e. say in your game model, you have an entity which occupied old_id, you would need to change all references to use the new_id. This is intended to be used before saving a game.

Note: this algorithm is O(n lg n) due to the use of binary heap.

Source§

impl<IndexT, DataT> Tec<IndexT, DataT>
where IndexT: CastUsize + Ord + Copy + Maximum, DataT: Clone,

Source

pub fn populate(data: DataT, count: usize) -> Self

Populate count number of items by cloning the given data.

Source§

impl<IndexT, DataT> Tec<IndexT, DataT>
where IndexT: CastUsize + Ord + Copy + Maximum, DataT: Clone + Default,

Source

pub fn populate_defaults(count: usize) -> Self

Populate count number of items with the default value.

Source§

impl<IndexT, DataT> Tec<IndexT, DataT>
where IndexT: CastUsize + Ord + Copy + Maximum, DataT: Default,

Source

pub fn alloc_default(&mut self) -> IndexT

Trait Implementations§

Source§

impl<IndexT: Clone, DataT: Clone> Clone for Tec<IndexT, DataT>

Source§

fn clone(&self) -> Tec<IndexT, DataT>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<IndexT, DataT> Debug for Tec<IndexT, DataT>
where IndexT: Debug, DataT: Debug,

Source§

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

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

impl<IndexT, DataT> Default for Tec<IndexT, DataT>
where IndexT: Maximum,

Source§

fn default() -> Self

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

impl<IndexT, DataT> Index<IndexT> for Tec<IndexT, DataT>
where IndexT: CastUsize + Ord + Copy + Maximum,

Source§

type Output = DataT

The returned type after indexing.
Source§

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

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

impl<IndexT, DataT> IndexMut<IndexT> for Tec<IndexT, DataT>
where IndexT: CastUsize + Ord + Copy + Maximum,

Source§

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

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

Auto Trait Implementations§

§

impl<IndexT, DataT> Freeze for Tec<IndexT, DataT>
where IndexT: Freeze,

§

impl<IndexT, DataT> RefUnwindSafe for Tec<IndexT, DataT>
where IndexT: RefUnwindSafe, DataT: RefUnwindSafe,

§

impl<IndexT, DataT> Send for Tec<IndexT, DataT>
where IndexT: Send, DataT: Send,

§

impl<IndexT, DataT> Sync for Tec<IndexT, DataT>
where IndexT: Sync, DataT: Sync,

§

impl<IndexT, DataT> Unpin for Tec<IndexT, DataT>
where IndexT: Unpin, DataT: Unpin,

§

impl<IndexT, DataT> UnwindSafe for Tec<IndexT, DataT>
where IndexT: UnwindSafe, DataT: UnwindSafe,

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, dest: *mut u8)

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