Struct linked_hash_map::LinkedHashMap [] [src]

pub struct LinkedHashMap<K, V, S = RandomState> {
    // some fields omitted
}

A linked hash map.

Methods

impl<K: Hash + Eq, V> LinkedHashMap<K, V>

fn new() -> Self

Creates a linked hash map.

fn with_capacity(capacity: usize) -> Self

Creates an empty linked hash map with the given initial capacity.

impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S>

fn with_hash_state(hash_state: S) -> Self

Creates an empty linked hash map with the given initial hash state.

fn with_capacity_and_hash_state(capacity: usize, hash_state: S) -> Self

Creates an empty linked hash map with the given initial capacity and hash state.

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.

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.

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");

fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool where K: Borrow<Q>, Q: Eq + Hash

Checks if the map contains the given key.

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"));

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"));

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());

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);

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();

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));

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)));

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);

fn back(&mut 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)));

fn len(&self) -> usize

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

fn is_empty(&self) -> bool

Returns whether the map is currently empty.

fn clear(&mut self)

Clears the map of all key-value pairs.

fn iter(&self) -> Iter<K, 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());

fn iter_mut(&mut self) -> IterMut<K, 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());

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

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());

fn values<'a>(&'a self) -> Values<'a, K, 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

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

type Output = V

fn index(&self, index: &'a Q) -> &V

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

fn index_mut(&mut self, index: &'a Q) -> &mut V

impl<K: Hash + Eq + Clone, V: Clone> Clone for LinkedHashMap<K, V>

fn clone(&self) -> Self

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

impl<K: Hash + Eq, V, S: BuildHasher + Default> Default for LinkedHashMap<K, V, S>

fn default() -> Self

impl<K: Hash + Eq, V, S: BuildHasher> Extend<(K, V)> for LinkedHashMap<K, V, S>

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

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

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

impl<K: Hash + Eq, V, S: BuildHasher + Default> FromIterator<(K, V)> for LinkedHashMap<K, V, S>

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

impl<A: Debug + Hash + Eq, B: Debug, S: BuildHasher> Debug for LinkedHashMap<A, B, S>

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

impl<K: Hash + Eq, V: PartialEq, S: BuildHasher> PartialEq for LinkedHashMap<K, V, S>

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

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

impl<K: Hash + Eq, V: Eq, S: BuildHasher> Eq for LinkedHashMap<K, V, S>

impl<K: Hash + Eq + PartialOrd, V: PartialOrd, S: BuildHasher> PartialOrd for LinkedHashMap<K, V, S>

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

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

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

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

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

impl<K: Hash + Eq + Ord, V: Ord, S: BuildHasher> Ord for LinkedHashMap<K, V, S>

fn cmp(&self, other: &Self) -> Ordering

impl<K: Hash + Eq, V: Hash, S: BuildHasher> Hash for LinkedHashMap<K, V, S>

fn hash<H: Hasher>(&self, h: &mut H)

1.3.0fn hash_slice<H>(data: &[Self], state: &mut H) where H: Hasher

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>

impl<K, V, S> Drop for LinkedHashMap<K, V, S>

fn drop(&mut self)

impl<'a, K: Hash + Eq, V, S: BuildHasher> IntoIterator for &'a LinkedHashMap<K, V, S>

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

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

fn into_iter(self) -> Iter<'a, K, V>

impl<'a, K: Hash + Eq, V, S: BuildHasher> IntoIterator for &'a mut LinkedHashMap<K, V, S>

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

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

fn into_iter(self) -> IterMut<'a, K, V>