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 slow_compare(&self, other: &Self) -> bool
pub fn slow_compare(&self, other: &Self) -> bool
Compares two maps for equality, ignoring insertion order and duplicate entries (the last
value for a given key wins on both sides). This allocates two intermediate HashMaps, so
it’s exposed as a named method rather than PartialEq/Eq, to keep that cost visible at
the call site instead of hiding it behind ==.
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(&self) -> Iter<'_, (K, V)> ⓘ
pub fn iter(&self) -> Iter<'_, (K, V)> ⓘ
Iterate over the element, including duplicate entries.
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.
Sourcepub fn mark_deduped(&mut self)
pub fn mark_deduped(&mut self)
Assert, without scanning, that this map holds no duplicate keys, setting the deduped flag.
For builders whose source guarantees key uniqueness (e.g. msgpack decoding, where the wire format is a map), to skip the Self::dedup pass. A later mutation re-dirties the flag.
Caution: if the source can actually contain duplicate keys, prefer Self::dedup.
pub fn clear(&mut self)
pub fn drain<R: RangeBounds<usize>>(&mut self, range: R) -> Drain<'_, (K, V)> ⓘ
Source§impl<K: Eq + Hash, V> VecMap<K, V>
impl<K: Eq + Hash, V> VecMap<K, V>
Sourcepub fn as_deduped_map(&self) -> DedupedVecMap<'_, K, V>
pub fn as_deduped_map(&self) -> DedupedVecMap<'_, K, V>
Returns a deduped map, that either borrows from self without performing any work if the
map is already deduped, or dedup the entries in a new separate vec otherwise. As opposed to
Self::dedup, as_deduped_map takes an immutable reference to self but might allocate.
Prefer Self::dedup when applicable.
Sourcepub fn defensive_dedup(&self) -> DedupedVecMap<'_, K, V>
pub fn defensive_dedup(&self) -> DedupedVecMap<'_, K, V>
This is a convenience wrapper around Self::as_deduped_map used in the msgpack encoder,
where we expect the map to be deduped, but call as_deduped_map as a defensive measure. If
the latter had to deduplicate and allocate a new vec, we log a warning (at most once).
Sourcepub fn dedup(&mut self)
pub fn dedup(&mut self)
Remove entries with a duplicate key, only keeping the last one. After this, a flag is set internally, such that as long as the map isn’t extended or mutably iterated, the next Self::dedup doesn’t perform the work again.
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)