Skip to main content

LinkedHashMap

Struct LinkedHashMap 

Source
pub struct LinkedHashMap<K, V, S = RandomState> { /* private fields */ }
Expand description

A hash map that preserves insertion order and exposes a [VecDeque]-like API with [insert_back], [insert_front], [pop_front], [pop_back], [front], and [back].

All the usual [HashMap] operations (get, get_mut, remove, contains_key, len, is_empty, clear, …) are also available.

§Ordering contract

  • [insert_back] and [insert_front] preserve the position of an existing key: only the value is updated in-place. Use [move_to_back] / [move_to_front] to explicitly reorder an entry.

Implementations§

Source§

impl<K, V> LinkedHashMap<K, V>

Source

pub fn new() -> Self

Creates an empty LinkedHashMap.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates an empty LinkedHashMap with the specified initial capacity.

Source§

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

Source

pub fn with_hasher(hash_builder: S) -> Self

Creates an empty LinkedHashMap using the supplied hasher builder.

Source

pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self

Creates an empty LinkedHashMap with the given capacity and hasher.

Source

pub fn len(&self) -> usize

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

Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no key-value pairs.

Source

pub fn capacity(&self) -> usize

Returns the number of elements the map can hold without reallocating.

Source

pub fn hasher(&self) -> &S

Returns a reference to the map’s BuildHasher.

Source§

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

Source

pub fn entry<'a>(&'a mut self, key: K) -> Entry<'a, K, V, S>

Gets the given key’s corresponding entry in the map for in-place manipulation.

Source

pub fn insert_back(&mut self, key: K, value: V) -> Option<V>

Inserts a key-value pair at the back (most-recently-inserted end).

If the key already exists, the value is replaced in-place and the node’s position in the ordering is preserved (it is not moved). Returns the old value in that case.

To also move the node to the back, call move_to_back after insertion.

Source

pub fn insert_front(&mut self, key: K, value: V) -> Option<V>

Inserts a key-value pair at the front (least-recently-inserted end).

If the key already exists, the value is replaced in-place and the node’s position in the ordering is preserved (it is not moved). Returns the old value in that case.

To also move the node to the front, call move_to_front after insertion.

Source

pub fn insert(&mut self, key: K, value: V) -> Option<V>

Alias for insert_back: matches the HashMap::insert signature.

Source

pub fn get<Q>(&self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns a reference to the value associated with key, if present.

Source

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 associated with key, if present.

Source

pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns (&key, &value) for the given key, if present.

Source

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

Returns true if the map contains a value for key.

Source

pub fn front(&self) -> Option<(&K, &V)>

Returns references to the front (oldest inserted) key-value pair, or None if the map is empty.

Source

pub fn front_mut(&mut self) -> Option<(&K, &mut V)>

Returns a mutable reference to the front key-value pair, or None.

Source

pub fn back(&self) -> Option<(&K, &V)>

Returns references to the back (most recently inserted) key-value pair, or None if the map is empty.

Source

pub fn back_mut(&mut self) -> Option<(&K, &mut V)>

Returns a mutable reference to the back key-value pair, or None.

Source

pub fn pop_front(&mut self) -> Option<(K, V)>

Removes and returns the front (oldest) key-value pair, or None.

Source

pub fn pop_back(&mut self) -> Option<(K, V)>

Removes and returns the back (newest) key-value pair, or None.

Source

pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes the entry for key and returns the value, if present.

Source

pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes the entry for key and returns (key, value), if present.

Source

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

Retains only entries for which f(&key, &mut value) returns true. Elements are visited in insertion order (front → back).

Source

pub fn clear(&mut self)

Removes all key-value pairs from the map.

Source

pub fn move_to_back<Q>(&mut self, key: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Moves the entry for key to the back of the ordering. Returns true if the key was found, false otherwise.

Source

pub fn move_to_front<Q>(&mut self, key: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Moves the entry for key to the front of the ordering. Returns true if the key was found, false otherwise.

Source

pub fn drain(&mut self) -> Drain<'_, K, V>

Removes all elements in insertion order, returning (K, V) pairs via an iterator. The map is empty after this call (even if the iterator is dropped before it is fully consumed).

Source§

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

Source

pub fn iter(&self) -> Iter<'_, K, V>

Returns an iterator over (&K, &V) pairs in insertion order.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, K, V>

Returns an iterator over (&K, &mut V) pairs in insertion order.

Source

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

Returns an iterator over keys in insertion order.

Source

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

Returns an iterator over values in insertion order.

Source

pub fn values_mut(&mut self) -> ValuesMut<'_, K, V>

Returns a mutable iterator over values in insertion order.

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Self

Returns a duplicate 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, V, S> Debug for LinkedHashMap<K, V, S>
where K: Debug, V: Debug,

Source§

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

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

impl<K, V> Default for LinkedHashMap<K, V>

Source§

fn default() -> Self

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

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

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

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

Source§

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

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: Hash + Eq, V> FromIterator<(K, V)> for LinkedHashMap<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, V, S, Q> Index<&Q> for LinkedHashMap<K, V, S>
where K: Hash + Eq + Borrow<Q>, Q: Hash + Eq + ?Sized, S: BuildHasher,

Source§

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

Returns a reference to the value for key.

§Panics

Panics if key is not present in the map.

Source§

type Output = V

The returned type after indexing.
Source§

impl<K, V, S, Q> IndexMut<&Q> for LinkedHashMap<K, V, S>
where K: Hash + Eq + Borrow<Q>, Q: Hash + Eq + ?Sized, S: BuildHasher,

Source§

fn index_mut(&mut self, key: &Q) -> &mut V

Returns a mutable reference to the value for key.

§Panics

Panics if key is not present in the map.

Source§

impl<'a, K, V, S> IntoIterator for &'a LinkedHashMap<K, V, S>

Shared-reference iterator: yields (&K, &V) in insertion order.

Source§

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

The type of the elements being iterated over.
Source§

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

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

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

Creates an iterator from a value. Read more
Source§

impl<'a, K, V, S> IntoIterator for &'a mut LinkedHashMap<K, V, S>

Mutable-reference iterator: yields (&K, &mut V) in insertion order.

Source§

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

The type of the elements being iterated over.
Source§

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

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

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

Creates an iterator from a value. Read more
Source§

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

Consuming iterator: yields all (K, V) pairs in insertion order.

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, V>

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

fn into_iter(self) -> IntoIter<K, V>

Creates an iterator from a value. Read more
Source§

impl<K, V, S1, S2> PartialEq<LinkedHashMap<K, V, S2>> for LinkedHashMap<K, V, S1>
where K: PartialEq, V: PartialEq,

Source§

fn eq(&self, other: &LinkedHashMap<K, V, S2>) -> bool

Two maps are equal only when they contain the same key-value pairs in the same order.

1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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

Source§

impl<K: Send, V: Send, S: Send> Send for LinkedHashMap<K, V, S>

Source§

impl<K: Sync, V: Sync, S: Sync> Sync for LinkedHashMap<K, V, S>

Auto Trait Implementations§

§

impl<K, V, S> Freeze for LinkedHashMap<K, V, S>
where S: Freeze,

§

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

§

impl<K, V, S> Unpin for LinkedHashMap<K, V, S>
where S: Unpin,

§

impl<K, V, S> UnsafeUnpin for LinkedHashMap<K, V, S>
where S: UnsafeUnpin,

§

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

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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>,

Source§

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>,

Source§

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.