pub struct DenseMap<T> { /* private fields */ }
Expand description
A generic container for T
keyed by densely packed integers.
DenseMap
is a generic container keyed by usize
that manages its own key
pool. DenseMap
reuses keys that are free to keep its key pool as dense as
possible.
The main guarantee provided by DenseMap
is that all get
operations are
provided in O(1) without the need to hash the keys.
All operations that mutate the DenseMap
may be O(n) during resizing or
compression but averages at O(1).
push
will grab the lowest free id
and assign it to the given value,
returning the assigned id
. insert
can be used for assigning a specific
id
to an object, and returns the previous object at that id
if any.
Implementations§
Source§impl<T> DenseMap<T>
impl<T> DenseMap<T>
Sourcepub fn get(&self, key: Key) -> Option<&T>
pub fn get(&self, key: Key) -> Option<&T>
Returns a reference to the item indexed by key
, or None
if the key
doesn’t exist.
Sourcepub fn get_mut(&mut self, key: Key) -> Option<&mut T>
pub fn get_mut(&mut self, key: Key) -> Option<&mut T>
Returns a mutable reference to the item indexed by key
, or None
if
the key
doesn’t exist.
Sourcepub fn remove(&mut self, key: Key) -> Option<T>
pub fn remove(&mut self, key: Key) -> Option<T>
Removes item indexed by key
from the container.
Returns the removed item if it exists, or None
otherwise.
Note: the worst case complexity of remove
is O(key) if the backing
data structure of the DenseMap
is too sparse.
Sourcepub fn push(&mut self, item: T) -> Key
pub fn push(&mut self, item: T) -> Key
Inserts item
into the DenseMap
.
push
inserts a new item
into the DenseMap
and returns the key value
allocated for item
. push
will allocate any key that is currently
free in the internal structure, so it may return a key that was used
previously but has since been removed.
Note: The worst case complexity of push
is O(n) where n is the number
of items held by the DenseMap
. This can happen if the internal
structure gets fragmented.
Sourcepub fn push_entry(&mut self, item: T) -> OccupiedEntry<'_, usize, T>
pub fn push_entry(&mut self, item: T) -> OccupiedEntry<'_, usize, T>
Inserts item
into the DenseMap
and returns an OccupiedEntry
for
it.
Like DenseMap::push
except that it returns an entry instead of an
index.
Sourcepub fn push_with(
&mut self,
make_item: impl FnOnce(Key) -> T,
) -> OccupiedEntry<'_, usize, T>
pub fn push_with( &mut self, make_item: impl FnOnce(Key) -> T, ) -> OccupiedEntry<'_, usize, T>
Creates an item
in the DenseMap
via functor.
Like DenseMap::push
except that the item is constructed by the provided
function, which is passed its index.
Sourcepub fn insertion_ordered_iter(&self) -> InsertionOrderedIter<'_, T> ⓘ
pub fn insertion_ordered_iter(&self) -> InsertionOrderedIter<'_, T> ⓘ
Creates an iterator over the containing items and their associated keys.
The iterator will return items in insertion order.
Sourcepub fn key_ordered_iter(&self) -> KeyOrderedIter<'_, T> ⓘ
pub fn key_ordered_iter(&self) -> KeyOrderedIter<'_, T> ⓘ
Creates an iterator over the containing items and their associated keys.
The iterator will return items in key order.
Sourcepub fn key_ordered_iter_mut(&mut self) -> KeyOrderedIterMut<'_, T> ⓘ
pub fn key_ordered_iter_mut(&mut self) -> KeyOrderedIterMut<'_, T> ⓘ
Creates a mutable iterator over the containing items and their associated keys.
The iterator will return items in key order.
Sourcepub fn key_ordered_into_iter(self) -> IntoKeyOrderedIter<T> ⓘ
pub fn key_ordered_into_iter(self) -> IntoKeyOrderedIter<T> ⓘ
Consumes the DenseMap
and returns an iterator over the contained items
and their associated keys.
The iterator will return items in key order.
Sourcepub fn entry(&mut self, key: usize) -> Entry<'_, usize, T>
pub fn entry(&mut self, key: usize) -> Entry<'_, usize, T>
Gets the given key’s corresponding entry in the map for in-place manipulation.
Sourcepub fn key_ordered_retain<F: FnMut(&T) -> bool>(&mut self, should_retain: F)
pub fn key_ordered_retain<F: FnMut(&T) -> bool>(&mut self, should_retain: F)
Retains only the elements specified by the predicate.
In other words, remove all elements e for which f(&e) returns false. The elements are visited in ascending key order.