pub struct IdMap<K: IntegerId, V, T: EntryTable<K, V> = DenseEntryTable<K, V>> { /* private fields */ }Expand description
A map of mostly-contiguous IntegerId keys to values, backed by a Vec.
This is parametric over the type of the underlying EntryTable, which controls its behavior
By default it’s equivalent to an OrderedIdMap,
though you can explicitly request a DirectIdMap instead.
From the user’s perspective, this is equivalent to a nice wrapper around a Vec<Option<(K, V)>>,
that preserves insertion order and saves some space for missing keys.
More details on the possible internal representations
are documented in the OrderedIdMap and DirectIdMap aliases.
Implementations§
Source§impl<K: IntegerId, V> IdMap<K, V, DirectEntryTable<K, V>>
impl<K: IntegerId, V> IdMap<K, V, DirectEntryTable<K, V>>
Sourcepub fn new_direct() -> Self
pub fn new_direct() -> Self
Create a new direct IdMap.
This stores its entries directly in a Vector
Sourcepub fn with_capacity_direct(capacity: usize) -> Self
pub fn with_capacity_direct(capacity: usize) -> Self
Create new direct IdMap, initialized with the specified capacity
Because a direct id map stores its values directly, the capacity hints at the maximum id and not the length
Source§impl<K: IntegerId, V> IdMap<K, V>
impl<K: IntegerId, V> IdMap<K, V>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create an IdMap with the specified capacity, using an OrderedIdTable
Source§impl<K: IntegerId, V, T: EntryTable<K, V>> IdMap<K, V, T>
impl<K: IntegerId, V, T: EntryTable<K, V>> IdMap<K, V, T>
Sourcepub fn with_capacity_other(capacity: usize) -> Self
pub fn with_capacity_other(capacity: usize) -> Self
Create a new IdMap with the specified capacity but a custom entry table.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
The length of this map
This doesn’t necessarily equal the maximum integer id
Sourcepub fn contains_key<Q: Borrow<K>>(&self, key: Q) -> bool
pub fn contains_key<Q: Borrow<K>>(&self, key: Q) -> bool
If this map contains the specified key
Sourcepub fn get<Q: Borrow<K>>(&self, key: Q) -> Option<&V>
pub fn get<Q: Borrow<K>>(&self, key: Q) -> Option<&V>
Retrieve the value associated with the specified key,
or None if the value is not present
Keys that implement IntegerId are expected to be cheap,
so we don’t borrow the key.
Sourcepub fn get_mut<Q: Borrow<K>>(&mut self, key: Q) -> Option<&mut V>
pub fn get_mut<Q: Borrow<K>>(&mut self, key: Q) -> Option<&mut V>
Retrieve a mutable reference to the value associated with the specified key,
or None if the value is not present
Sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Insert the specified value, associating it with a key
Returns the value previously associated with they key,
or None if there wasn’t any
Sourcepub fn remove<Q: Borrow<K>>(&mut self, key: Q) -> Option<V>
pub fn remove<Q: Borrow<K>>(&mut self, key: Q) -> Option<V>
Remove the value associated with the specified key,
or None if there isn’t any
Sourcepub fn entry(&mut self, key: K) -> Entry<'_, K, V, T>
pub fn entry(&mut self, key: K) -> Entry<'_, K, V, T>
Retrieve the entry associated with the specified key
Mimics the HashMap entry aPI
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V, T> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V, T> ⓘ
Iterate over all the entries in this map, giving mutable references to the keys
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V, T> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V, T> ⓘ
Iterate over mutable references to all the values in the map
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear all the entries the map
Like Vec::clear this should retain the underlying memory for future use.
Sourcepub fn retain<F>(&mut self, func: F)
pub fn retain<F>(&mut self, func: F)
Retains only the elements specified by the predicate.
let mut map: IdMap<usize, usize> = (0..8).map(|x|(x, x*10)).collect();
map.retain(|k, _| k % 2 == 0);
assert_eq!(
map.into_iter().collect::<Vec<_>>(),
vec![(0, 0), (2, 20), (4, 40), (6, 60)]
);Trait Implementations§
Source§impl<'de, K, V, T> Deserialize<'de> for IdMap<K, V, T>
impl<'de, K, V, T> Deserialize<'de> for IdMap<K, V, T>
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Source§impl<'a, K, V, T> Extend<(&'a K, &'a V)> for IdMap<K, V, T>
impl<'a, K, V, T> Extend<(&'a K, &'a V)> for IdMap<K, V, T>
Source§fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<K: IntegerId, V, T: EntryTable<K, V>> Extend<(K, V)> for IdMap<K, V, T>
impl<K: IntegerId, V, T: EntryTable<K, V>> Extend<(K, V)> for IdMap<K, V, T>
Source§fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)