pub struct VecMap<K, V> { /* private fields */ }Expand description
A Vec-backed map that provides HashMap-like lookup by key.
§Duplicates
Duplicates are tolerated: VecMap::insert always appends, and VecMap::get/VecMap::get_mut return the last matching entry so that later writes shadow earlier ones. This optimizes for fast insertion and construction (that might happen on the client’s application hot path), avoiding a linear scan on each insert, or a potential full re-hashing with a hashmap. Additionally, while overriding a metric or a meta definitively happens, it’s assumed to be rare enough so such that the size penalty of duplication is expected to be reasonable.
Important: note that only VecMap::get and VecMap::get_mut are duplicate-aware, so to
speak. VecMap::len, VecMap::iter, and others just delegates to the underlying Vec, and
won’t deduplicate.
Explicit deduplication is currently being done on-demand by VecMap::dedup. An internal flag is
used to avoid undue deduplication (see VecMap::dedup). VecMap is automatically deduped
before serialization.
In the future, we could trigger deduplication on other events, for example at insertion if the
size is bigger than a threshold (and we haven’t deduped for x operations).
§Ordering
As this is a map, iteration order is not defined nor guaranteed. In practice, iteration follows insertion order, but Self::dedup will reverse the underlying vector.
Implementations§
Source§impl<K, V> VecMap<K, V>
impl<K, V> VecMap<K, V>
pub fn new() -> Self
pub fn with_capacity(capacity: usize) -> Self
pub fn insert(&mut self, key: K, value: V)
pub fn get<Q>(&self, key: &Q) -> Option<&V>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn contains_key<Q>(&self, key: &Q) -> bool
Sourcepub fn remove_slow<Q>(&mut self, key: &Q)
pub fn remove_slow<Q>(&mut self, key: &Q)
Remove all entries matching this key from the map. This method uses Vec::retain, and is thus potentially costly (like any removal in a vector-like datastructure).
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, (K, V)>
pub fn iter_mut(&mut self) -> IterMut<'_, (K, V)>
Iterate mutably over the elements, including duplicate entries.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Return the length of the underlying vector, thus including duplicate entries.
pub fn is_empty(&self) -> bool
Sourcepub fn is_deduped(&self) -> bool
pub fn is_deduped(&self) -> bool
Return true if the map hasn’t been extended since the last call to Self::dedup,
guaranteeing that the underlying vector doesn’t have any duplicate key.
If is_deduped returns false, the map may have duplicate keys.
Trait Implementations§
Source§impl<K, V> Extend<(K, V)> for VecMap<K, V>
impl<K, V> Extend<(K, V)> for VecMap<K, V>
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)