Struct stable_id::Entities

source ·
pub struct Entities<IndexT, DataT> { /* private fields */ }
Expand description

This is a lazily memory-compact version of SparseEntities. Use cases are the same but there are different tradeoffs.

Tradeoff vs SparseEntities.

  • this struct uses a hash-based virtual table to translate issued ids into an id used internally by its backing collection Tec. So accessing items should be similar – it’s dictated by HashMap’s access complexity, since once it finds the internal id, a random access follows.
  • removing items is O(Tec::remove()) = O(n lg n) though I have plans to make it O(n). An added benefits is [remove()] will also try to compact the memory by removing dead slots from Tec when there’s a majority of dead slots – it’s another O(n) pass.

Implementations§

source§

impl<IndexT, DataT> Entities<IndexT, DataT>where IndexT: Default + Successor + Clone + Copy + Hash + Eq + CastUsize + Ord + Maximum,

source

pub fn with_capacity(capacity: usize) -> Self

Reserves spaces similar to Vec::with_capacity().

source

pub fn len(&self) -> usize

Returns the number of items in this data structure.

source

pub fn is_empty(&self) -> bool

Tells you if the collection is empty.

source

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

Try getting the item with the given id.

source

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

Mutable version of get.

source

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

Removes an element for the given id.

source

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

Allocate an entity with monotonically increase ids, just like crate::SparseEntities.

source

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

Return all data’s references.

source

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

Return all data in mutable references.

source

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

Iterate every entries. This takes O(HashMap::iter()) to iterate the entire collection.

source§

impl<IndexT, DataT> Entities<IndexT, DataT>where IndexT: Successor + CastUsize + Ord + Copy + Maximum + Hash, 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> Entities<IndexT, DataT>where IndexT: CastUsize + Ord + Copy + Maximum + Successor + Hash, DataT: Clone + Default,

source

pub fn populate_defaults(count: usize) -> Self

Populate count number of items with the default value.

Trait Implementations§

source§

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

source§

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

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<IndexT, DataT> Default for Entities<IndexT, DataT>where IndexT: Default + Maximum,

source§

fn default() -> Self

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

impl<IndexT, DataT> Index<IndexT> for Entities<IndexT, DataT>where IndexT: Successor + Clone + Copy + Hash + Eq + Default + CastUsize + Ord + Maximum,

§

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 Entities<IndexT, DataT>where IndexT: Successor + Clone + Copy + Hash + Eq + Default + CastUsize + Ord + 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> RefUnwindSafe for Entities<IndexT, DataT>where DataT: RefUnwindSafe, IndexT: RefUnwindSafe,

§

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

§

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

§

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

§

impl<IndexT, DataT> UnwindSafe for Entities<IndexT, DataT>where DataT: UnwindSafe, IndexT: 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,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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.
const: unstable · 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.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.