Struct more_collections::vec_map::VecMap
source · pub struct VecMap<K, V> { /* private fields */ }
Expand description
A Vec
-backed map.
It has faster random access performance and slower iteration speed compared to other maps. Makes most sense for relatively dense maps or if iteration is needed significantly less than random access. In case of doubt, benchmark it for your usecase.
§Performance
VecMap
outperforms HashMap
, IndexMap
, and BTreeMap
for random access
(such as get()
) and random modifications (such as insert()
). For
modifications this is only true iff VecMap
does not need to do any
resizing. Therefore, if performance is essential, it is strongly
recommended to initialize VecMap
with with_capacity()
.
Iteration order follows the natural ordering of IndexKey::as_index()
.
Implementations§
source§impl<K: IndexKey, V: Clone> VecMap<K, V>
impl<K: IndexKey, V: Clone> VecMap<K, V>
sourcepub fn with_capacity(n: usize) -> Self
pub fn with_capacity(n: usize) -> Self
Initializes VecMap
with capacity to hold exactly n
elements in the
index range of 0..n
.
source§impl<K: IndexKey, V> VecMap<K, V>
impl<K: IndexKey, V> VecMap<K, V>
sourcepub const fn new() -> Self
pub const fn new() -> Self
Initializes an empty VecMap
.
For performance reasons it’s almost always better to avoid dynamic
resizing by using Self::with_capacity()
instead.
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the map can hold without reallocating.
The index range of items that the map can hold without reallocating is
0..capacity
.
sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Inserts a key-value pair into the map.
If the key is present in the map, the value is updated and the old value
is returned. Otherwise, None
is returned.
sourcepub fn remove(&mut self, key: K) -> Option<V>
pub fn remove(&mut self, key: K) -> Option<V>
Removes the key-value pair indicated by key
.
If the key was present, it is returned. Otherwise None
is returned.
sourcepub fn pop(&mut self) -> Option<(K, V)>
pub fn pop(&mut self) -> Option<(K, V)>
Removes the last key-value pair.
Worst case performance is O(n) in case the value is at the first index, where n = the capacity.
sourcepub fn retain<F>(&mut self, keep: F)
pub fn retain<F>(&mut self, keep: F)
Iterates over each key-value pair in the map and keep those where the
closure keep
returns true
.
The elements are visited in order.
sourcepub fn entry(&mut self, key: K) -> Entry<'_, K, V>
pub fn entry(&mut self, key: K) -> Entry<'_, K, V>
Get the given key’s entry in the map for insertion and/or in-place manipulation.
sourcepub fn get(&self, key: K) -> Option<&V>
pub fn get(&self, key: K) -> Option<&V>
Returns a reference to the value associated with key
if it exists,
otherwise returns None
.
sourcepub fn get_mut(&mut self, key: K) -> Option<&mut V>
pub fn get_mut(&mut self, key: K) -> Option<&mut V>
Returns a mutable reference to the value associated with key
if it
exists, otherwise returns None
.
sourcepub fn contains_key(&self, key: K) -> bool
pub fn contains_key(&self, key: K) -> bool
Returns true
if the map contains an item with the specified key
.
sourcepub fn iter(&self) -> Iter<'_, K, V> ⓘ
pub fn iter(&self) -> Iter<'_, K, V> ⓘ
Returns an iterator over the key-value pairs of the map, following the natural order of the keys.
sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
Returns an iterator over the key-value pairs of the map, with the values being mutable, following the natural order of the keys.
Trait Implementations§
source§impl<K: IndexKey, V: Clone> Extend<(K, V)> for VecMap<K, V>
impl<K: IndexKey, V: Clone> Extend<(K, V)> for VecMap<K, V>
source§fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
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<'a, K: IndexKey, V> IntoIterator for &'a VecMap<K, V>
impl<'a, K: IndexKey, V> IntoIterator for &'a VecMap<K, V>
source§impl<'a, K: IndexKey, V> IntoIterator for &'a mut VecMap<K, V>
impl<'a, K: IndexKey, V> IntoIterator for &'a mut VecMap<K, V>
source§impl<K: IndexKey, V> IntoIterator for VecMap<K, V>
impl<K: IndexKey, V> IntoIterator for VecMap<K, V>
source§impl<K, V: PartialEq> PartialEq for VecMap<K, V>
impl<K, V: PartialEq> PartialEq for VecMap<K, V>
impl<K, V: Eq> Eq for VecMap<K, V>
Auto Trait Implementations§
impl<K, V> Freeze for VecMap<K, V>
impl<K, V> RefUnwindSafe for VecMap<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for VecMap<K, V>
impl<K, V> Sync for VecMap<K, V>
impl<K, V> Unpin for VecMap<K, V>
impl<K, V> UnwindSafe for VecMap<K, V>where
K: UnwindSafe,
V: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.