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>

source

pub fn with_capacity(n: usize) -> Self

Initializes VecMap with capacity to hold exactly n elements in the index range of 0..n.

source

pub fn from_elem(elem: V, n: usize) -> Self

Initializes VecMap with n occurences of elem.

source

pub fn clear(&mut self)

Clears all data from the VecMap without changing the capacity.

source

pub fn reserve(&mut self, additional: usize)

Reserve capacity for additional key-value pairs.

source§

impl<K: IndexKey, V> VecMap<K, V>

source

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.

source

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.

source

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.

source

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.

source

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.

source

pub fn retain<F>(&mut self, keep: F)
where F: FnMut(K, &mut V) -> bool,

Iterates over each key-value pair in the map and keep those where the closure keep returns true.

The elements are visited in order.

source

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.

source

pub fn get(&self, key: K) -> Option<&V>

Returns a reference to the value associated with key if it exists, otherwise returns None.

source

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.

source

pub const fn len(&self) -> usize

Return the number of key-value pairs in the map.

source

pub const fn is_empty(&self) -> bool

Returns true if the map contains no elements.

source

pub fn contains_key(&self, key: K) -> bool

Returns true if the map contains an item with the specified key.

source

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.

source

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.

source

pub fn keys(&self) -> Keys<'_, K, V>

Returns an iterator over the keys of the map following the natural order of the keys.

source

pub fn values(&self) -> Values<'_, V>

Returns an iterator over the values of the map following the natural order of the keys.

Trait Implementations§

source§

impl<K: Clone, V: Clone> Clone for VecMap<K, V>

source§

fn clone(&self) -> VecMap<K, V>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<K: IndexKey + Debug, V: Debug> Debug for VecMap<K, V>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<K: IndexKey, V> Default for VecMap<K, V>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<K: IndexKey, V: Clone> Extend<(K, V)> for VecMap<K, V>

source§

fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<K: IndexKey, V: Clone> FromIterator<(K, V)> for VecMap<K, V>

source§

fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self

Creates a value from an iterator. Read more
source§

impl<K: IndexKey, V> Index<K> for VecMap<K, V>

§

type Output = V

The returned type after indexing.
source§

fn index(&self, key: K) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<K: IndexKey, V> IndexMut<K> for VecMap<K, V>

source§

fn index_mut(&mut self, key: K) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl<'a, K: IndexKey, V> IntoIterator for &'a VecMap<K, V>

§

type Item = (K, &'a V)

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, K, V>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a, K: IndexKey, V> IntoIterator for &'a mut VecMap<K, V>

§

type Item = (K, &'a mut V)

The type of the elements being iterated over.
§

type IntoIter = IterMut<'a, K, V>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<K: IndexKey, V> IntoIterator for VecMap<K, V>

§

type Item = (K, V)

The type of the elements being iterated over.
§

type IntoIter = IntoIter<K, V>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<K, V: PartialEq> PartialEq for VecMap<K, V>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

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>

§

impl<K, V> Send for VecMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for VecMap<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for VecMap<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for VecMap<K, V>
where K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.