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 fromTecwhen 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,
impl<IndexT, DataT> Entities<IndexT, DataT>where IndexT: Default + Successor + Clone + Copy + Hash + Eq + CastUsize + Ord + Maximum,
sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Reserves spaces similar to Vec::with_capacity().
sourcepub fn get_mut(&mut self, index: IndexT) -> Option<&mut DataT>
pub fn get_mut(&mut self, index: IndexT) -> Option<&mut DataT>
Mutable version of get.
sourcepub fn alloc(&mut self, data: DataT) -> IndexT
pub fn alloc(&mut self, data: DataT) -> IndexT
Allocate an entity with monotonically increase ids, just like crate::SparseEntities.
sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = &mut DataT>
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut DataT>
Return all data in mutable references.
sourcepub fn iter_with_id(&self) -> impl Iterator<Item = (IndexT, &DataT)>
pub fn iter_with_id(&self) -> impl Iterator<Item = (IndexT, &DataT)>
Iterate every entries. This takes O(HashMap::iter()) to iterate the entire collection.