Struct ordered_hash_map::OrderedHashMap
source · pub struct OrderedHashMap<K, V, S = DefaultHashBuilder> { /* private fields */ }Expand description
A hash map which preserved the order of insertion.
Backed by hashbrown and its default hashing algorithm, currently AHash. The hashing
algorithm can be changed on a per-map basis with with_hasher and with_capacity_and_hasher.
More details on hashing algorithms and why AHash was selected in the hashbrown crate.
To switch to the same hasher as HashMap use RandomState. This will switch to SipHash, a
HashDos resistant algorithm but it may be slower in some cases.
Being backed by a HashMap, this container has all the same requirements on its keys as
HashMap. Specifically, Keys must implement Eq and Hash. You can typically derive
these for types with #[derive(PartialEq, Eq, hash)]. If you implement these yourself you must
ensure the following holds:
k1 == k2 -> hash(k1) == hash(k2)
You are also responsible to ensure Keys do not mutate such that their hash can change while in
the map. For more information, check HashMap.
This container has some additional overhead on top of its backing HashMap. OrderedHashMap
holds two pointers to maintain the linked list. Each Value inserted has the overhead of three pointers, one for the key
and two for the linked list properties.
Implementations§
source§impl<K, V> OrderedHashMap<K, V, DefaultHashBuilder>
impl<K, V> OrderedHashMap<K, V, DefaultHashBuilder>
sourcepub fn new() -> Self
pub fn new() -> Self
Create an empty map.
Capacity is 0 so the map will not allocated until inserted into.
sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create an empty map with at least the specified capacity.
The map will allocate if capacity is nonzero.
sourcepub fn drain(&mut self) -> Drain<'_, K, V> ⓘ
pub fn drain(&mut self) -> Drain<'_, K, V> ⓘ
Returns a draining iterator over the elements in insertion order.
Performance
This iterator visits each element once instead of each bucket once. This iterator is O(len) which a usual map iterator is O(capacity) and may visit empty buckets.
source§impl<K, V, S> OrderedHashMap<K, V, S>
impl<K, V, S> OrderedHashMap<K, V, S>
sourcepub const fn with_hasher(hash_builder: S) -> Self
pub const fn with_hasher(hash_builder: S) -> Self
Create an empty map which will use the given hasher.
Capacity is 0 so the map will not allocate until inserted into.
sourcepub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self
pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self
Create an empty map with the given capacity and which will use the given hasher.
The map will allocate if capacity is nonzero.
sourcepub fn keys(&self) -> Keys<'_, K, V> ⓘ
pub fn keys(&self) -> Keys<'_, K, V> ⓘ
Returns an iterator over the keys in insertion order.
Performance
This iterator visits each element once instead of each bucket once. This iterator is O(len) which a usual map iterator is O(capacity) and may visit empty buckets.
sourcepub fn into_keys(self) -> IntoKeys<K, V> ⓘ
pub fn into_keys(self) -> IntoKeys<K, V> ⓘ
Returns an iterator over the keys in insertion order.
Performance
This iterator visits each element once instead of each bucket once. This iterator is O(len) which a usual map iterator is O(capacity) and may visit empty buckets.
sourcepub fn values(&self) -> Values<'_, K, V> ⓘ
pub fn values(&self) -> Values<'_, K, V> ⓘ
Returns an iterator over the values in insertion order.
Performance
This iterator visits each element once instead of each bucket once. This iterator is O(len) which a usual map iterator is O(capacity) and may visit empty buckets.
sourcepub fn into_values(self) -> IntoValues<K, V> ⓘ
pub fn into_values(self) -> IntoValues<K, V> ⓘ
Returns an iterator over the values in insertion order.
Performance
This iterator visits each element once instead of each bucket once. This iterator is O(len) which a usual map iterator is O(capacity) and may visit empty buckets.
sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
Returns a mutable value iterator over the values in insertion order.
Performance
This iterator visits each element once instead of each bucket once. This iterator is O(len) which a usual map iterator is O(capacity) and may visit empty buckets.
sourcepub fn iter(&self) -> Iter<'_, K, V> ⓘ
pub fn iter(&self) -> Iter<'_, K, V> ⓘ
Returns an iterator over the elements in insertion order.
Performance
This iterator visits each element once instead of each bucket once. This iterator is O(len) which a usual map iterator is O(capacity) and may visit empty buckets.
sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
Returns a mutable value iterator over the elements in insertion order.
Performance
This iterator visits each element once instead of each bucket once. This iterator is O(len) which a usual map iterator is O(capacity) and may visit empty buckets.
source§impl<K, V, S> OrderedHashMap<K, V, S>where
K: Eq + Hash,
S: BuildHasher,
impl<K, V, S> OrderedHashMap<K, V, S>where K: Eq + Hash, S: BuildHasher,
sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Insert a new element into the map. If that key was associated with a value, returns the old element, otherwise None.
sourcepub fn get<Q>(&self, key: &Q) -> Option<&V>where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
pub fn get<Q>(&self, key: &Q) -> Option<&V>where K: Borrow<Q>, Q: Hash + Eq + ?Sized,
Returns a reference to the value stored in the map.
sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>where K: Borrow<Q>, Q: Hash + Eq + ?Sized,
Returns a mutable reference to the value stored in the map.
sourcepub fn get_key_value<Q>(&mut self, key: &Q) -> Option<(&K, &V)>where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
pub fn get_key_value<Q>(&mut self, key: &Q) -> Option<(&K, &V)>where K: Borrow<Q>, Q: Hash + Eq + ?Sized,
Returns a reference to the key and value stored in the map
sourcepub fn get_key_value_mut<Q>(&mut self, key: &Q) -> Option<(&K, &mut V)>where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
pub fn get_key_value_mut<Q>(&mut self, key: &Q) -> Option<(&K, &mut V)>where K: Borrow<Q>, Q: Hash + Eq + ?Sized,
Returns a reference to key and mutable reference to value stored in the map
sourcepub fn remove<Q>(&mut self, key: &Q) -> Option<V>where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>where K: Borrow<Q>, Q: Hash + Eq + ?Sized,
Remove an element from the map, retuning the stored value if there is one.
sourcepub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>where K: Borrow<Q>, Q: Hash + Eq + ?Sized,
Remove an element from the map, returning the key and value if here is one.
sourcepub fn contains_key<Q>(&self, key: &Q) -> boolwhere
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
pub fn contains_key<Q>(&self, key: &Q) -> boolwhere K: Borrow<Q>, Q: Hash + Eq + ?Sized,
Check if the map contains this key.
sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more elements.
Panics
Panics if the new size would overflow a usize.
sourcepub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
Tries to reserve capacity for at least additional more elements.
Errors
Returns an error if the new size would overflow a usize.
sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the map as much as possible. May leave some space in accordance with the resize policy.
sourcepub fn shrink_to(&mut self, min_capacity: usize)
pub fn shrink_to(&mut self, min_capacity: usize)
Shrinks the map down to a lower limit. May leave some space in accordance with the resize policy.
sourcepub fn pop_back_entry(&mut self) -> Option<(K, V)>
pub fn pop_back_entry(&mut self) -> Option<(K, V)>
Removes and returns the most recently inserted key and value, if there is one.
sourcepub fn pop_back(&mut self) -> Option<V>
pub fn pop_back(&mut self) -> Option<V>
Removed and returns the most recently inserted element, if there is one.
sourcepub fn back_mut(&mut self) -> Option<&mut V>
pub fn back_mut(&mut self) -> Option<&mut V>
Get a mutable reference to the element at the back
sourcepub fn pop_front_entry(&mut self) -> Option<(K, V)>
pub fn pop_front_entry(&mut self) -> Option<(K, V)>
Remove and return the least recently inserted key and value, if there is one.
sourcepub fn pop_front(&mut self) -> Option<V>
pub fn pop_front(&mut self) -> Option<V>
Remove and return the least recently inserted element, if there is one.
Trait Implementations§
source§impl<K, V, S> Clone for OrderedHashMap<K, V, S>where
K: Hash + Eq + Clone,
V: Clone,
S: BuildHasher + Clone,
impl<K, V, S> Clone for OrderedHashMap<K, V, S>where K: Hash + Eq + Clone, V: Clone, S: BuildHasher + Clone,
source§impl<K, V, S> Default for OrderedHashMap<K, V, S>where
S: Default,
impl<K, V, S> Default for OrderedHashMap<K, V, S>where S: Default,
source§impl<K, V, S> Drop for OrderedHashMap<K, V, S>
impl<K, V, S> Drop for OrderedHashMap<K, V, S>
source§impl<'a, K, V, S> Extend<(&'a K, &'a V)> for OrderedHashMap<K, V, S>where
K: Eq + Hash + Copy,
V: Copy,
S: BuildHasher,
impl<'a, K, V, S> Extend<(&'a K, &'a V)> for OrderedHashMap<K, V, S>where K: Eq + Hash + Copy, V: Copy, S: BuildHasher,
source§fn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (&'a K, &'a 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<K, V, S> Extend<(K, V)> for OrderedHashMap<K, V, S>where
K: Eq + Hash,
S: BuildHasher,
impl<K, V, S> Extend<(K, V)> for OrderedHashMap<K, V, S>where K: Eq + Hash, S: BuildHasher,
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, V, S> FromIterator<(&'a K, &'a V)> for OrderedHashMap<K, V, S>where
K: Eq + Hash + Copy,
V: Copy,
S: BuildHasher + Default,
impl<'a, K, V, S> FromIterator<(&'a K, &'a V)> for OrderedHashMap<K, V, S>where K: Eq + Hash + Copy, V: Copy, S: BuildHasher + Default,
source§impl<K, V, S> FromIterator<(K, V)> for OrderedHashMap<K, V, S>where
K: Eq + Hash,
S: BuildHasher + Default,
impl<K, V, S> FromIterator<(K, V)> for OrderedHashMap<K, V, S>where K: Eq + Hash, S: BuildHasher + Default,
source§impl<K, Q, V, S> Index<&Q> for OrderedHashMap<K, V, S>where
K: Eq + Hash + Borrow<Q>,
Q: Eq + Hash + ?Sized,
S: BuildHasher,
impl<K, Q, V, S> Index<&Q> for OrderedHashMap<K, V, S>where K: Eq + Hash + Borrow<Q>, Q: Eq + Hash + ?Sized, S: BuildHasher,
source§impl<'a, K, V, S> IntoIterator for &'a OrderedHashMap<K, V, S>
impl<'a, K, V, S> IntoIterator for &'a OrderedHashMap<K, V, S>
source§impl<'a, K, V, S> IntoIterator for &'a mut OrderedHashMap<K, V, S>
impl<'a, K, V, S> IntoIterator for &'a mut OrderedHashMap<K, V, S>
source§impl<K, V, S> IntoIterator for OrderedHashMap<K, V, S>
impl<K, V, S> IntoIterator for OrderedHashMap<K, V, S>
source§impl<K, V, S> PartialEq<OrderedHashMap<K, V, S>> for OrderedHashMap<K, V, S>where
K: Eq + Hash,
V: PartialEq,
S: BuildHasher,
impl<K, V, S> PartialEq<OrderedHashMap<K, V, S>> for OrderedHashMap<K, V, S>where K: Eq + Hash, V: PartialEq, S: BuildHasher,
source§fn eq(&self, other: &OrderedHashMap<K, V, S>) -> bool
fn eq(&self, other: &OrderedHashMap<K, V, S>) -> bool
self and other values to be equal, and is used
by ==.