Struct linked_hash_map::LinkedHashMap
source · [−]pub struct LinkedHashMap<K, V, S = RandomState> { /* private fields */ }
Expand description
A linked hash map.
Implementations
sourceimpl<K: Hash + Eq, V> LinkedHashMap<K, V>
impl<K: Hash + Eq, V> LinkedHashMap<K, V>
sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates an empty linked hash map with the given initial capacity.
sourceimpl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S>
impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S>
sourcepub fn with_hasher(hash_builder: S) -> Self
pub fn with_hasher(hash_builder: S) -> Self
Creates an empty linked hash map with the given initial hash builder.
sourcepub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self
pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self
Creates an empty linked hash map with the given initial capacity and hash builder.
sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more elements to be inserted into the map. The
map may reserve more space to avoid frequent allocations.
Panics
Panics if the new allocation size overflows usize.
sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the map as much as possible. It will drop down as much as possible while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.
sourcepub fn entry(&mut self, k: K) -> Entry<'_, K, V, S>
pub fn entry(&mut self, k: K) -> Entry<'_, K, V, S>
Gets the given key’s corresponding entry in the map for in-place manipulation.
Examples
use linked_hash_map::LinkedHashMap;
let mut letters = LinkedHashMap::new();
for ch in "a short treatise on fungi".chars() {
let counter = letters.entry(ch).or_insert(0);
*counter += 1;
}
assert_eq!(letters[&'s'], 2);
assert_eq!(letters[&'t'], 3);
assert_eq!(letters[&'u'], 1);
assert_eq!(letters.get(&'y'), None);
sourcepub fn entries(&mut self) -> Entries<'_, K, V, S>ⓘNotable traits for Entries<'a, K, V, S>impl<'a, K, V, S: BuildHasher> Iterator for Entries<'a, K, V, S> type Item = OccupiedEntry<'a, K, V, S>;
pub fn entries(&mut self) -> Entries<'_, K, V, S>ⓘNotable traits for Entries<'a, K, V, S>impl<'a, K, V, S: BuildHasher> Iterator for Entries<'a, K, V, S> type Item = OccupiedEntry<'a, K, V, S>;
Returns an iterator visiting all entries in insertion order.
Iterator element type is OccupiedEntry<K, V, S>
. Allows for removal
as well as replacing the entry.
Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert("a", 10);
map.insert("c", 30);
map.insert("b", 20);
{
let mut iter = map.entries();
let mut entry = iter.next().unwrap();
assert_eq!(&"a", entry.key());
*entry.get_mut() = 17;
}
assert_eq!(&17, map.get(&"a").unwrap());
sourcepub fn insert(&mut self, k: K, v: V) -> Option<V>
pub fn insert(&mut self, k: K, v: V) -> Option<V>
Inserts a key-value pair into the map. If the key already existed, the old value is returned.
Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, "a");
map.insert(2, "b");
assert_eq!(map[&1], "a");
assert_eq!(map[&2], "b");
sourcepub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool where
K: Borrow<Q>,
Q: Eq + Hash,
pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool where
K: Borrow<Q>,
Q: Eq + Hash,
Checks if the map contains the given key.
sourcepub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V> where
K: Borrow<Q>,
Q: Eq + Hash,
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V> where
K: Borrow<Q>,
Q: Eq + Hash,
Returns the value corresponding to the key in the map.
Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, "a");
map.insert(2, "b");
map.insert(2, "c");
map.insert(3, "d");
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), Some(&"c"));
sourcepub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V> where
K: Borrow<Q>,
Q: Eq + Hash,
pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V> where
K: Borrow<Q>,
Q: Eq + Hash,
Returns the mutable reference corresponding to the key in the map.
Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, "a");
map.insert(2, "b");
*map.get_mut(&1).unwrap() = "c";
assert_eq!(map.get(&1), Some(&"c"));
sourcepub fn get_refresh<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V> where
K: Borrow<Q>,
Q: Eq + Hash,
pub fn get_refresh<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V> where
K: Borrow<Q>,
Q: Eq + Hash,
Returns the value corresponding to the key in the map.
If value is found, it is moved to the end of the list. This operation can be used in implemenation of LRU cache.
Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, "a");
map.insert(2, "b");
map.insert(3, "d");
assert_eq!(map.get_refresh(&2), Some(&mut "b"));
assert_eq!((&2, &"b"), map.iter().rev().next().unwrap());
sourcepub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V> where
K: Borrow<Q>,
Q: Eq + Hash,
pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V> where
K: Borrow<Q>,
Q: Eq + Hash,
Removes and returns the value corresponding to the key from the map.
Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(2, "a");
assert_eq!(map.remove(&1), None);
assert_eq!(map.remove(&2), Some("a"));
assert_eq!(map.remove(&2), None);
assert_eq!(map.len(), 0);
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the maximum number of key-value pairs the map can hold without reallocating.
Examples
use linked_hash_map::LinkedHashMap;
let mut map: LinkedHashMap<i32, &str> = LinkedHashMap::new();
let capacity = map.capacity();
sourcepub fn pop_front(&mut self) -> Option<(K, V)>
pub fn pop_front(&mut self) -> Option<(K, V)>
Removes the first entry.
Can be used in implementation of LRU cache.
Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
map.pop_front();
assert_eq!(map.get(&1), None);
assert_eq!(map.get(&2), Some(&20));
sourcepub fn front(&self) -> Option<(&K, &V)>
pub fn front(&self) -> Option<(&K, &V)>
Gets the first entry.
Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
assert_eq!(map.front(), Some((&1, &10)));
sourcepub fn pop_back(&mut self) -> Option<(K, V)>
pub fn pop_back(&mut self) -> Option<(K, V)>
Removes the last entry.
Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
map.pop_back();
assert_eq!(map.get(&1), Some(&10));
assert_eq!(map.get(&2), None);
sourcepub fn back(&self) -> Option<(&K, &V)>
pub fn back(&self) -> Option<(&K, &V)>
Gets the last entry.
Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
assert_eq!(map.back(), Some((&2, &20)));
sourcepub fn iter(&self) -> Iter<'_, K, V>ⓘNotable traits for Iter<'a, K, V>impl<'a, K, V> Iterator for Iter<'a, K, V> type Item = (&'a K, &'a V);
pub fn iter(&self) -> Iter<'_, K, V>ⓘNotable traits for Iter<'a, K, V>impl<'a, K, V> Iterator for Iter<'a, K, V> type Item = (&'a K, &'a V);
Returns a double-ended iterator visiting all key-value pairs in order of insertion.
Iterator element type is (&'a K, &'a V)
Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert("a", 10);
map.insert("c", 30);
map.insert("b", 20);
let mut iter = map.iter();
assert_eq!((&"a", &10), iter.next().unwrap());
assert_eq!((&"c", &30), iter.next().unwrap());
assert_eq!((&"b", &20), iter.next().unwrap());
assert_eq!(None, iter.next());
sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V>ⓘNotable traits for IterMut<'a, K, V>impl<'a, K, V> Iterator for IterMut<'a, K, V> type Item = (&'a K, &'a mut V);
pub fn iter_mut(&mut self) -> IterMut<'_, K, V>ⓘNotable traits for IterMut<'a, K, V>impl<'a, K, V> Iterator for IterMut<'a, K, V> type Item = (&'a K, &'a mut V);
Returns a double-ended iterator visiting all key-value pairs in order of insertion.
Iterator element type is (&'a K, &'a mut V)
Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert("a", 10);
map.insert("c", 30);
map.insert("b", 20);
{
let mut iter = map.iter_mut();
let mut entry = iter.next().unwrap();
assert_eq!(&"a", entry.0);
*entry.1 = 17;
}
assert_eq!(&17, map.get(&"a").unwrap());
sourcepub fn drain(&mut self) -> Drain<'_, K, V>ⓘNotable traits for Drain<'a, K, V>impl<'a, K, V> Iterator for Drain<'a, K, V> type Item = (K, V);
pub fn drain(&mut self) -> Drain<'_, K, V>ⓘNotable traits for Drain<'a, K, V>impl<'a, K, V> Iterator for Drain<'a, K, V> type Item = (K, V);
Clears the map, returning all key-value pairs as an iterator. Keeps the allocated memory for reuse.
If the returned iterator is dropped before being fully consumed, it drops the remaining key-value pairs. The returned iterator keeps a mutable borrow on the vector to optimize its implementation.
Current performance implications (why to use this over into_iter()):
- Clears the inner HashMap instead of dropping it
- Puts all drained nodes in the free-list instead of deallocating them
- Avoids deallocating the sentinel node
sourcepub fn keys(&self) -> Keys<'_, K, V>ⓘNotable traits for Keys<'a, K, V>impl<'a, K, V> Iterator for Keys<'a, K, V> type Item = &'a K;
pub fn keys(&self) -> Keys<'_, K, V>ⓘNotable traits for Keys<'a, K, V>impl<'a, K, V> Iterator for Keys<'a, K, V> type Item = &'a K;
Returns a double-ended iterator visiting all key in order of insertion.
Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert('a', 10);
map.insert('c', 30);
map.insert('b', 20);
let mut keys = map.keys();
assert_eq!(&'a', keys.next().unwrap());
assert_eq!(&'c', keys.next().unwrap());
assert_eq!(&'b', keys.next().unwrap());
assert_eq!(None, keys.next());
sourcepub fn values(&self) -> Values<'_, K, V>ⓘNotable traits for Values<'a, K, V>impl<'a, K, V> Iterator for Values<'a, K, V> type Item = &'a V;
pub fn values(&self) -> Values<'_, K, V>ⓘNotable traits for Values<'a, K, V>impl<'a, K, V> Iterator for Values<'a, K, V> type Item = &'a V;
Returns a double-ended iterator visiting all values in order of insertion.
Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert('a', 10);
map.insert('c', 30);
map.insert('b', 20);
let mut values = map.values();
assert_eq!(&10, values.next().unwrap());
assert_eq!(&30, values.next().unwrap());
assert_eq!(&20, values.next().unwrap());
assert_eq!(None, values.next());
Trait Implementations
sourceimpl<K: Hash + Eq + Clone, V: Clone, S: BuildHasher + Clone> Clone for LinkedHashMap<K, V, S>
impl<K: Hash + Eq + Clone, V: Clone, S: BuildHasher + Clone> Clone for LinkedHashMap<K, V, S>
sourceimpl<A: Debug + Hash + Eq, B: Debug, S: BuildHasher> Debug for LinkedHashMap<A, B, S>
impl<A: Debug + Hash + Eq, B: Debug, S: BuildHasher> Debug for LinkedHashMap<A, B, S>
sourceimpl<K: Hash + Eq, V, S: BuildHasher + Default> Default for LinkedHashMap<K, V, S>
impl<K: Hash + Eq, V, S: BuildHasher + Default> Default for LinkedHashMap<K, V, S>
sourceimpl<K, V, S> Drop for LinkedHashMap<K, V, S>
impl<K, V, S> Drop for LinkedHashMap<K, V, S>
sourceimpl<'a, K, V, S> Extend<(&'a K, &'a V)> for LinkedHashMap<K, V, S> where
K: 'a + Hash + Eq + Copy,
V: 'a + Copy,
S: BuildHasher,
impl<'a, K, V, S> Extend<(&'a K, &'a V)> for LinkedHashMap<K, V, S> where
K: 'a + Hash + Eq + Copy,
V: 'a + Copy,
S: BuildHasher,
sourcefn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I)
Extends a collection with the contents of an iterator. Read more
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Extends a collection with exactly one element.
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
sourceimpl<K: Hash + Eq, V, S: BuildHasher> Extend<(K, V)> for LinkedHashMap<K, V, S>
impl<K: Hash + Eq, V, S: BuildHasher> Extend<(K, V)> for LinkedHashMap<K, V, S>
sourcefn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
Extends a collection with the contents of an iterator. Read more
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Extends a collection with exactly one element.
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
sourceimpl<K: Hash + Eq, V, S: BuildHasher + Default> FromIterator<(K, V)> for LinkedHashMap<K, V, S>
impl<K: Hash + Eq, V, S: BuildHasher + Default> FromIterator<(K, V)> for LinkedHashMap<K, V, S>
sourcefn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self
Creates a value from an iterator. Read more
sourceimpl<K: Hash + Eq, V: Hash, S: BuildHasher> Hash for LinkedHashMap<K, V, S>
impl<K: Hash + Eq, V: Hash, S: BuildHasher> Hash for LinkedHashMap<K, V, S>
sourceimpl<'a, K, V, S, Q: ?Sized> Index<&'a Q> for LinkedHashMap<K, V, S> where
K: Hash + Eq + Borrow<Q>,
S: BuildHasher,
Q: Eq + Hash,
impl<'a, K, V, S, Q: ?Sized> Index<&'a Q> for LinkedHashMap<K, V, S> where
K: Hash + Eq + Borrow<Q>,
S: BuildHasher,
Q: Eq + Hash,
sourceimpl<'a, K, V, S, Q: ?Sized> IndexMut<&'a Q> for LinkedHashMap<K, V, S> where
K: Hash + Eq + Borrow<Q>,
S: BuildHasher,
Q: Eq + Hash,
impl<'a, K, V, S, Q: ?Sized> IndexMut<&'a Q> for LinkedHashMap<K, V, S> where
K: Hash + Eq + Borrow<Q>,
S: BuildHasher,
Q: Eq + Hash,
sourceimpl<'a, K: Hash + Eq, V, S: BuildHasher> IntoIterator for &'a LinkedHashMap<K, V, S>
impl<'a, K: Hash + Eq, V, S: BuildHasher> IntoIterator for &'a LinkedHashMap<K, V, S>
sourceimpl<'a, K: Hash + Eq, V, S: BuildHasher> IntoIterator for &'a mut LinkedHashMap<K, V, S>
impl<'a, K: Hash + Eq, V, S: BuildHasher> IntoIterator for &'a mut LinkedHashMap<K, V, S>
sourceimpl<K: Hash + Eq, V, S: BuildHasher> IntoIterator for LinkedHashMap<K, V, S>
impl<K: Hash + Eq, V, S: BuildHasher> IntoIterator for LinkedHashMap<K, V, S>
sourceimpl<K: Hash + Eq + Ord, V: Ord, S: BuildHasher> Ord for LinkedHashMap<K, V, S>
impl<K: Hash + Eq + Ord, V: Ord, S: BuildHasher> Ord for LinkedHashMap<K, V, S>
sourceimpl<K: Hash + Eq, V: PartialEq, S: BuildHasher> PartialEq<LinkedHashMap<K, V, S>> for LinkedHashMap<K, V, S>
impl<K: Hash + Eq, V: PartialEq, S: BuildHasher> PartialEq<LinkedHashMap<K, V, S>> for LinkedHashMap<K, V, S>
sourceimpl<K: Hash + Eq + PartialOrd, V: PartialOrd, S: BuildHasher> PartialOrd<LinkedHashMap<K, V, S>> for LinkedHashMap<K, V, S>
impl<K: Hash + Eq + PartialOrd, V: PartialOrd, S: BuildHasher> PartialOrd<LinkedHashMap<K, V, S>> for LinkedHashMap<K, V, S>
sourcefn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
sourcefn lt(&self, other: &Self) -> bool
fn lt(&self, other: &Self) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
sourcefn le(&self, other: &Self) -> bool
fn le(&self, other: &Self) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl<K: Hash + Eq, V: Eq, S: BuildHasher> Eq for LinkedHashMap<K, V, S>
impl<K: Send, V: Send, S: Send> Send for LinkedHashMap<K, V, S>
impl<K: Sync, V: Sync, S: Sync> Sync for LinkedHashMap<K, V, S>
Auto Trait Implementations
impl<K, V, S> RefUnwindSafe for LinkedHashMap<K, V, S> where
K: RefUnwindSafe,
S: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V, S> Unpin for LinkedHashMap<K, V, S> where
S: Unpin,
impl<K, V, S> UnwindSafe for LinkedHashMap<K, V, S> where
K: RefUnwindSafe,
S: UnwindSafe,
V: RefUnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more