LinkedHashMap

Struct LinkedHashMap 

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

A hash map that maintains relative order using a doubly-linked list.

This data structure combines the O(1) lookup performance of a hash table with the ability to iterate over entries in relative order. It supports arbitrary key-value pairs where keys implement Hash + Eq.

The generic parameters are:

  • K: Key type, must implement Hash + Eq
  • T: Value type
  • S: Hash builder type, defaults to the standard hasher

§Examples

use tether_map::linked_hash_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("apple", 5);
map.insert("banana", 3);
map.insert("cherry", 8);

// Iterate in insertion order
for (key, value) in map.iter() {
    println!("{}: {}", key, value);
}
// Prints: apple: 5, banana: 3, cherry: 8

Implementations§

Source§

impl<K, T> LinkedHashMap<K, T>

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new linked hash map with the specified capacity.

The map will be able to hold at least capacity elements without reallocating. If capacity is 0, the map will not allocate.

§Examples
use tether_map::LinkedHashMap;

let mut map: LinkedHashMap<&str, i32> = LinkedHashMap::with_capacity(10);
assert_eq!(map.len(), 0);
Source

pub fn new() -> Self

Creates a new, empty linked hash map.

The map is initially created with a capacity of 0, so it will not allocate until the first element is inserted.

§Examples
use tether_map::LinkedHashMap;

let mut map: LinkedHashMap<&str, i32> = LinkedHashMap::new();
assert!(map.is_empty());
map.insert("key", 42);
assert!(!map.is_empty());
Source§

impl<K, T, S> LinkedHashMap<K, T, S>

Source

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

Creates a new linked hash map with the specified capacity and hasher.

The map will use the given hasher to hash keys and will be able to hold at least capacity elements without reallocating.

§Examples
use tether_map::linked_hash_map::LinkedHashMap;

let hasher = RandomState::default();
let mut map: LinkedHashMap<&str, i32, _> = LinkedHashMap::with_capacity_and_hasher(10, hasher);
map.insert("key", 42);
Source

pub fn move_after(&mut self, moved: Ptr, after: Ptr) -> Option<()>

Moves the entry at moved to be immediately after the entry at after in the linked list.

If moved is already immediately after after, returns None and no change is made. Both pointers must refer to valid entries in the map.

§Arguments
  • moved - The pointer to the entry to move
  • after - The pointer to the entry after which moved will be placed
§Returns
  • Some(()) if the move was successful
  • None if the move was unnecessary (already in correct position) or if either pointer is invalid
§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
let (ptr1, _) = map.insert_tail_full("a", 1);
let (ptr2, _) = map.insert_tail_full("b", 2);
let (ptr3, _) = map.insert_tail_full("c", 3);

// Move "c" to be after "a"
map.move_after(ptr3, ptr1);

// Order is now: a, c, b
let entries: Vec<_> = map.iter().collect();
assert_eq!(entries, [(&"a", &1), (&"c", &3), (&"b", &2)]);
Source

pub fn move_before(&mut self, moved: Ptr, before: Ptr) -> Option<()>

Moves the entry at moved to be immediately before the entry at before in the linked list.

If moved is already immediately before before, returns None and no change is made. Both pointers must refer to valid entries in the map.

§Arguments
  • moved - The pointer to the entry to move
  • before - The pointer to the entry before which moved will be placed
§Returns
  • Some(()) if the move was successful
  • None if the move was unnecessary (already in correct position) or if either pointer is invalid
§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
let (ptr1, _) = map.insert_tail_full("a", 1);
let (ptr2, _) = map.insert_tail_full("b", 2);
let (ptr3, _) = map.insert_tail_full("c", 3);

// Move "a" to be before "c"
map.move_before(ptr1, ptr3);

// Order is now: b, a, c
let entries: Vec<_> = map.iter().collect();
assert_eq!(entries, [(&"b", &2), (&"a", &1), (&"c", &3)]);

Links an entry as the new head of the linked list.

The entry at ptr must already exist in the map but not be part of the linked list.

This is a low-level method intended for advanced use cases with entries that have been inserted in an unlinked state. Using this with already linked entries may screw up the list, causing issues during iteration or other operations. It will not cause undefined behavior, but it may lead to logical errors.

§Arguments
  • ptr - The pointer to the entry to link as the new head
§Returns
  • Some(()) if the linking was successful
  • None if the pointer is invalid

Links an entry as the new tail of the linked list.

The entry at ptr must already exist in the map but not be part of the linked list.

This is a low-level method intended for advanced use cases with entries that have been inserted in an unlinked state. Using this with already linked entries may screw up the list, causing issues during iteration or other operations. It will not cause undefined behavior, but it may lead to logical errors.

§Arguments
  • ptr - The pointer to the entry to link as the new tail
§Returns
  • Some(()) if the linking was successful
  • None if the pointer is invalid

Links an entry into the doubly-linked list at a specific position after another entry.

This is a low-level method intended for advanced use cases with entries that have been inserted in an unlinked state. Using this with already linked entries may screw up the list, causing issues during iteration or other operations. It will not cause undefined behavior, but it may lead to logical errors.

Links an entry into the doubly-linked list at a specific position before another entry.

This is a low-level method intended for advanced use cases with entries that have been inserted in an unlinked state. Using this with already linked entries may screw up the list, causing issues during iteration or other operations. It will not cause undefined behavior, but it may lead to logical errors.

Source

pub fn move_to_tail(&mut self, moved: Ptr) -> Option<()>

Moves an entry to the tail (end) of the linked list.

This is equivalent to calling move_after(moved, tail_ptr()).

§Arguments
  • moved - The pointer to the entry to move to the tail
§Returns
  • Some(()) if the move was successful
  • None if the entry is already at the tail or if the pointer is invalid
§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
let (ptr1, _) = map.insert_tail_full("a", 1);
let (ptr2, _) = map.insert_tail_full("b", 2);
let (ptr3, _) = map.insert_tail_full("c", 3);

// Move "a" to the tail
map.move_to_tail(ptr1);

// Order is now: b, c, a
let entries: Vec<_> = map.iter().collect();
assert_eq!(entries, [(&"b", &2), (&"c", &3), (&"a", &1)]);
Source

pub fn move_to_head(&mut self, moved: Ptr) -> Option<()>

Moves an entry to the head (beginning) of the linked list.

This is equivalent to calling move_before(moved, head_ptr()).

§Arguments
  • moved - The pointer to the entry to move to the head
§Returns
  • Some(()) if the move was successful
  • None if the entry is already at the head or if the pointer is invalid
§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
let (ptr1, _) = map.insert_tail_full("a", 1);
let (ptr2, _) = map.insert_tail_full("b", 2);
let (ptr3, _) = map.insert_tail_full("c", 3);

// Move "c" to the head
map.move_to_head(ptr3);

// Order is now: c, a, b
let entries: Vec<_> = map.iter().collect();
assert_eq!(entries, [(&"c", &3), (&"a", &1), (&"b", &2)]);
Source

pub fn ptr_cursor_mut(&mut self, ptr: Ptr) -> CursorMut<'_, K, T, S>

Creates a mutable cursor positioned at the entry with the given pointer.

A cursor provides a way to navigate and modify the linked list structure while maintaining efficient access to specific entries.

§Arguments
  • ptr - The pointer to the entry where the cursor should be positioned
§Returns

A CursorMut positioned at the specified entry. If the pointer is invalid, the cursor will be positioned at a null entry.

§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
let (ptr, _) = map.insert_tail_full("a", 1);

let mut cursor = map.ptr_cursor_mut(ptr);
if let Some((key, value)) = cursor.current_mut() {
    *value = 42;
}
Source

pub fn next_ptr(&self, ptr: Ptr) -> Option<Ptr>

Returns the pointer to the next entry in the linked list.

§Arguments
  • ptr - The pointer to the current entry
§Returns
  • Some(next_ptr) if there is a next entry
  • None if the pointer is invalid
§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
let (ptr1, _) = map.insert_tail_full("a", 1);
let (ptr2, _) = map.insert_tail_full("b", 2);

// Get the next entry after "a"
let next = map.next_ptr(ptr1).unwrap();
assert_eq!(map.ptr_get(next), Some(&2));
Source

pub fn prev_ptr(&self, ptr: Ptr) -> Option<Ptr>

Returns the pointer to the previous entry in the linked list.

§Arguments
  • ptr - The pointer to the current entry
§Returns
  • Some(prev_ptr) if there is a previous entry
  • None if the pointer is invalid
§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
let (ptr1, _) = map.insert_tail_full("a", 1);
let (ptr2, _) = map.insert_tail_full("b", 2);

// Get the previous entry before "b"
let prev = map.prev_ptr(ptr2).unwrap();
assert_eq!(map.ptr_get(prev), Some(&1));
Source

pub fn head_cursor_mut(&mut self) -> CursorMut<'_, K, T, S>

Creates a mutable cursor positioned at the head (first entry) of the linked list.

§Returns

A CursorMut positioned at the head entry. If the map is empty, the cursor will be positioned at a null entry.

§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert_tail("a", 1);
map.insert_tail("b", 2);

let mut cursor = map.head_cursor_mut();
if let Some((key, value)) = cursor.current_mut() {
    assert_eq!(key, &"a");
    *value = 42;
}
Source

pub fn tail_cursor_mut(&mut self) -> CursorMut<'_, K, T, S>

Creates a mutable cursor positioned at the tail (last entry) of the linked list.

§Returns

A CursorMut positioned at the tail entry. If the map is empty, the cursor will be positioned at a null entry.

§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert_tail("a", 1);
map.insert_tail("b", 2);

let mut cursor = map.tail_cursor_mut();
if let Some((key, value)) = cursor.current_mut() {
    assert_eq!(key, &"b");
    *value = 42;
}
Source

pub fn head_ptr(&self) -> Option<Ptr>

Returns the pointer to the head (first entry) of the linked list.

Source

pub fn tail_ptr(&self) -> Option<Ptr>

Returns the pointer to the tail (last entry) of the linked list.

Source

pub fn ptr_get(&self, ptr: Ptr) -> Option<&T>

Returns a reference to the value associated with the given pointer.

Source

pub fn ptr_get_entry(&self, ptr: Ptr) -> Option<(&K, &T)>

Returns a reference to the key-value pair associated with the given pointer.

Source

pub fn ptr_get_entry_mut(&mut self, ptr: Ptr) -> Option<(&K, &mut T)>

Returns a mutable reference to the key-value pair associated with the given pointer.

Source

pub fn ptr_get_mut(&mut self, ptr: Ptr) -> Option<&mut T>

Returns a mutable reference to the value associated with the given pointer.

Source

pub fn ptr_get_key(&self, ptr: Ptr) -> Option<&K>

Returns a reference to the key associated with the given pointer.

Source

pub fn len(&self) -> usize

Returns the number of elements in the map.

§Examples
use tether_map::LinkedHashMap;

let mut a = LinkedHashMap::new();
assert_eq!(a.len(), 0);
a.insert(1, "a");
assert_eq!(a.len(), 1);
Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

§Examples
use tether_map::LinkedHashMap;

let mut a = LinkedHashMap::new();
assert!(a.is_empty());
a.insert(1, "a");
assert!(!a.is_empty());
Source

pub fn clear(&mut self)

Clears the map, removing all key-value pairs.

Keeps the allocated memory for reuse.

§Examples
use tether_map::LinkedHashMap;

let mut a = LinkedHashMap::new();
a.insert(1, "a");
a.clear();
assert!(a.is_empty());
Source

pub fn iter<'s>(&'s self) -> Iter<'s, K, T>

Returns an iterator over the key-value pairs of the map, in relative order.

The iterator element type is (&'a K, &'a V).

§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

for (key, val) in map.iter() {
    println!("key: {} val: {}", key, val);
}
Source

pub fn contains_ptr(&self, ptr: Ptr) -> bool

Checks if the map contains an entry for the given pointer.

§Arguments
  • ptr - The pointer to check for existence in the map
§Returns
  • true if the map contains an entry for the pointer
  • false otherwise
§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
let (ptr, _) = map.insert_tail_full("a", 1);
assert!(map.contains_ptr(ptr));
map.remove_ptr(ptr);
assert!(!map.contains_ptr(ptr));
Source

pub fn keys(&self) -> impl Iterator<Item = &K>

Returns an iterator over the keys of the map in their relative order.

The keys are returned in the order they were inserted into the map, which is maintained by the underlying doubly-linked list.

§Returns

An iterator that yields &K values in relative order.

§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

let keys: Vec<_> = map.keys().collect();
assert_eq!(keys, [&"a", &"b", &"c"]);
Source

pub fn values(&self) -> impl Iterator<Item = &T>

Returns an iterator over the values of the map in their relative order.

The values are returned in the order their corresponding keys were inserted into the map, which is maintained by the underlying doubly-linked list.

§Returns

An iterator that yields &T values in relative order.

§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

let values: Vec<_> = map.values().collect();
assert_eq!(values, [&1, &2, &3]);
Source

pub fn values_mut<'s>(&'s mut self) -> ValuesMut<'s, K, T>

Returns a mutable iterator over the values of the map in their relative order.

The values are returned in the order their corresponding keys were inserted into the map, which is maintained by the underlying doubly-linked list. This method allows for in-place mutation of the values.

§Returns

A mutable iterator that yields &mut T values in relative order.

§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

for value in map.values_mut() {
    *value *= 2;
}

let values: Vec<_> = map.values().collect();
assert_eq!(values, [&2, &4, &6]);
Source

pub fn iter_mut<'s>(&'s mut self) -> IterMut<'s, K, T>

Returns a mutable iterator over the key-value pairs of the map in their relative order.

The key-value pairs are returned in the order they were inserted into the map, which is maintained by the underlying doubly-linked list. This method allows for in-place mutation of the values while keeping the keys immutable.

§Returns

A mutable iterator that yields (&K, &mut T) pairs in relative order.

§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

for (key, value) in map.iter_mut() {
    if key == &"b" {
        *value *= 10;
    }
}

assert_eq!(map.get(&"b"), Some(&20));
Source§

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

Source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of the map as much as possible.

This will drop down the number of buckets in the underlying hash table as much as possible while maintaining the current number of elements.

It does not affect the capacity of the arena used for storing entries, as it’s not possible to safely shrink that without risking invalidating existing pointers.

Source

pub fn remove_tail(&mut self) -> Option<(Ptr, RemovedEntry<K, T>)>

Removes the entry at the tail (end) of the linked list.

This is equivalent to calling remove_ptr(tail_ptr()), but is more performant.

Source

pub fn remove_head(&mut self) -> Option<(Ptr, RemovedEntry<K, T>)>

Removes the entry at the head (beginning) of the linked list.

This is equivalent to calling remove_ptr(head_ptr()), but is more performant.

Source

pub fn remove_ptr(&mut self, ptr: Ptr) -> Option<RemovedEntry<K, T>>

Removes the entry at the given pointer from the map.

If the pointer is invalid or does not correspond to an occupied entry, returns None. Otherwise, removes the entry and returns a RemovedEntry containing the key, value, and neighboring pointers.

Source

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

Retains only the entries specified by the predicate.

In other words, removes all entries for which f(&key, &mut value) returns false. The entries are visited in relative order, and the predicate is allowed to modify the values.

§Arguments
  • f - A closure that returns true for entries that should be kept. It receives references to the key and a mutable reference to the value.
§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
map.insert("d", 4);

// Keep only entries with even values, and double them
map.retain(|_key, value| {
    if *value % 2 == 0 {
        *value *= 2;
        true
    } else {
        false
    }
});

// Only "b" and "d" remain, with values doubled
assert_eq!(map.len(), 2);
assert_eq!(map.get(&"b"), Some(&4));
assert_eq!(map.get(&"d"), Some(&8));
Source

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

Inserts a key-value pair into the map.

If the map did not have this key present, None is returned and the entry is inserted at the tail (most recently inserted position).

If the map did have this key present, the value is updated and the old value is returned. The entry is not moved in the linked list.

§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
assert_eq!(map.insert(37, "a"), None);
assert_eq!(map.is_empty(), false);

map.insert(37, "b");
assert_eq!(map.insert(37, "c"), Some("b"));
assert_eq!(map.get(&37), Some(&"c"));
Source

pub fn insert_full(&mut self, key: K, value: T) -> (Ptr, Option<T>)

Inserts a key-value pair and returns the pointer and any previous value.

This method provides the same functionality as insert but returns additional information: the pointer to the inserted/updated entry and any previous value that was replaced. The insertion position depends on whether the key already exists:

  • If the key is new, the entry is inserted at the tail
  • If the key exists, the value is updated in-place without moving the entry
§Arguments
  • key - The key to insert or update
  • value - The value to insert
§Returns

A tuple containing:

  • Ptr - The pointer to the inserted or updated entry
  • Option<T> - Some(old_value) if the key existed, None if it was newly inserted
§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();

// Insert new entry
let (ptr1, old) = map.insert_full("key1", 10);
assert_eq!(old, None); // No previous value

// Update existing entry
let (ptr2, old) = map.insert_full("key1", 20);
assert_eq!(old, Some(10)); // Previous value returned
assert_eq!(ptr1, ptr2); // Same pointer since key existed

// Use the pointer for direct access
assert_eq!(map.ptr_get(ptr1), Some(&20));
Source

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

Inserts a key-value pair at the tail of the linked list.

If the key already exists, updates the value and moves the entry to the tail position, returning the old value.

§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert_tail("first", 1);
map.insert_tail("second", 2);

let keys: Vec<_> = map.keys().cloned().collect();
assert_eq!(keys, ["first", "second"]);
Source

pub fn insert_tail_full(&mut self, key: K, value: T) -> (Ptr, Option<T>)

Inserts a key-value pair at the tail and returns the pointer and any previous value.

This method is similar to insert_full but with explicit tail positioning behavior:

  • If the key is new, the entry is inserted at the tail
  • If the key exists, the value is updated AND the entry is moved to the tail

This is useful when you want to ensure that updated entries are moved to the most recent position, implementing LRU (Least Recently Used) cache behavior.

§Arguments
  • key - The key to insert or update
  • value - The value to insert
§Returns

A tuple containing:

  • Ptr - The pointer to the inserted or updated entry
  • Option<T> - Some(old_value) if the key existed, None if it was newly inserted
§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

// Update existing entry - it moves to tail
let (ptr, old) = map.insert_tail_full("a", 10);
assert_eq!(old, Some(1));

// Order is now: b, c, a (with updated value)
let entries: Vec<_> = map.iter().collect();
assert_eq!(entries, [(&"b", &2), (&"c", &3), (&"a", &10)]);
Source

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

Inserts a key-value pair at the head of the linked list.

If the key already exists, updates the value and moves the entry to the head position, returning the old value.

§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert_head("first", 1);
map.insert_head("second", 2);

let keys: Vec<_> = map.keys().cloned().collect();
assert_eq!(keys, ["second", "first"]); // second is now first
Source

pub fn insert_head_full(&mut self, key: K, value: T) -> (Ptr, Option<T>)

Inserts a key-value pair at the head and returns the pointer and any previous value.

This method is similar to insert_full but with explicit head positioning behavior:

  • If the key is new, the entry is inserted at the head
  • If the key exists, the value is updated AND the entry is moved to the head

This is useful when you want to ensure that updated entries are moved to the most recent position at the beginning of the list, implementing MRU (Most Recently Used) behavior or priority queuing where recent updates should be processed first.

§Arguments
  • key - The key to insert or update
  • value - The value to insert
§Returns

A tuple containing:

  • Ptr - The pointer to the inserted or updated entry
  • Option<T> - Some(old_value) if the key existed, None if it was newly inserted
§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

// Update existing entry - it moves to head
let (ptr, old) = map.insert_head_full("b", 20);
assert_eq!(old, Some(2));

// Order is now: b (with updated value), a, c
let entries: Vec<_> = map.iter().collect();
assert_eq!(entries, [(&"b", &20), (&"a", &1), (&"c", &3)]);
Source

pub fn key_cursor_mut<Q>(&mut self, key: &Q) -> CursorMut<'_, K, T, S>
where Q: Equivalent<K> + Hash + Borrow<K> + ?Sized,

Creates a mutable cursor positioned at the entry with the given key.

This method performs a hash table lookup to find the entry with the specified key, then returns a cursor positioned at that entry.

§Arguments
  • key - The key to search for
§Returns

A CursorMut positioned at the entry with the given key. If the key is not found, the cursor will be positioned at a null entry.

§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert_tail("a", 1);
map.insert_tail("b", 2);

let mut cursor = map.key_cursor_mut(&"a");
if let Some((key, value)) = cursor.current_mut() {
    assert_eq!(key, &"a");
    *value = 42;
}
Source

pub fn entry(&mut self, key: K) -> Entry<'_, K, T>

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

This method provides an efficient way to insert, update, or conditionally modify entries without performing multiple lookups. The entry API is particularly useful for complex operations that depend on whether a key exists.

§Arguments
  • key - The key to get the entry for
§Returns

An Entry enum which can be either:

  • Entry::Occupied if the key exists, providing access to the existing value
  • Entry::Vacant if the key doesn’t exist, allowing insertion of a new value
§Examples
use tether_map::Entry;
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();

// Insert a value if key doesn't exist
match map.entry("key") {
    Entry::Vacant(entry) => {
        entry.insert_tail(42);
    }
    Entry::Occupied(_) => {
        // Key already exists
    }
}

// Update existing value or insert default
let value = match map.entry("counter") {
    Entry::Occupied(mut entry) => {
        *entry.get_mut() += 1;
        *entry.get()
    }
    Entry::Vacant(entry) => *entry.insert_tail(1).1,
};
Source

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

Removes a key from the map, returning the value at the key if the key was previously in the map.

§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert(1, "a");
assert_eq!(map.remove(&1), Some("a"));
assert_eq!(map.remove(&1), None);
Source

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

Removes a key from the map, returning the stored key and value if the key was previously in the map.

§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert(1, "a");
assert_eq!(map.remove_entry(&1), Some((1, "a")));
assert_eq!(map.remove(&1), None);
Source

pub fn remove_full<Q>(&mut self, key: &Q) -> Option<(Ptr, RemovedEntry<K, T>)>
where Q: Equivalent<K> + Hash + Borrow<K> + ?Sized,

Removes a key from the map, returning the pointer to the removed entry and the stored key, value, and neighboring pointers if the key was
previously in the map.

§Arguments
  • key - The key to remove from the map
§Returns
  • Some((Ptr, RemovedEntry<K, T>)) if the key was found and removed, where Ptr is the pointer to the removed entry and RemovedEntry<K , T> contains the key, value, and neighboring pointers
  • None if the key was not found in the map
§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
let (ptr, _) = map.insert_tail_full("a", 1);

// Remove by key
let result = map.remove_full(&"a");
assert!(result.is_some());
let (removed_ptr, removed_entry) = result.unwrap();
assert_eq!(removed_ptr, ptr);
assert_eq!(removed_entry.key, "a");
assert_eq!(removed_entry.value, 1);
assert_eq!(removed_entry.prev, None);
assert_eq!(removed_entry.next, None);
// Removing a non-existent key returns None
assert_eq!(map.remove_full(&"b"), None);
Source

pub fn get_ptr<Q>(&self, key: &Q) -> Option<Ptr>
where Q: Equivalent<K> + Hash + Borrow<K> + ?Sized,

Returns the pointer to the entry with the given key.

This method performs a hash table lookup to find the entry with the specified key and returns its pointer. The pointer can then be used for direct access operations or cursor positioning without additional key lookups.

§Arguments
  • key - The key to search for
§Returns
  • Some(ptr) if the key exists in the map
  • None if the key is not found
§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
let (inserted_ptr, _) = map.insert_tail_full("key", 42);

// Get pointer for the key
let found_ptr = map.get_ptr(&"key").unwrap();
assert_eq!(inserted_ptr, found_ptr);

// Use the pointer for direct access
assert_eq!(map.ptr_get(found_ptr), Some(&42));

// Non-existent key returns None
assert_eq!(map.get_ptr(&"missing"), None);
Source

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

Returns a reference to the value corresponding to the key.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert(1, "a");
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);
Source

pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut T>
where Q: Equivalent<K> + Hash + Borrow<K> + ?Sized,

Returns a mutable reference to the value corresponding to the key.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert(1, "a");
if let Some(x) = map.get_mut(&1) {
    *x = "b";
}
assert_eq!(map.get(&1), Some(&"b"));
Source

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

Returns true if the map contains a value for the specified key.

The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.

§Examples
use tether_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert(1, "a");
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&2), false);

Trait Implementations§

Source§

impl<K: Hash + Eq + Clone, T: Clone, S: BuildHasher + Clone> Clone for LinkedHashMap<K, T, 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: Debug, T: Debug, S> Debug for LinkedHashMap<K, T, S>

Source§

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

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

impl<K, T, S: BuildHasher + Default> Default for LinkedHashMap<K, T, S>

Source§

fn default() -> Self

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

impl<'a, K, T, S> Extend<(&'a K, &'a T)> for LinkedHashMap<K, T, S>
where K: Hash + Eq + Clone, T: Clone, S: BuildHasher,

Source§

fn extend<I: IntoIterator<Item = (&'a K, &'a T)>>(&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, T, S> Extend<(K, T)> for LinkedHashMap<K, T, S>
where K: Hash + Eq, S: BuildHasher,

Source§

fn extend<I: IntoIterator<Item = (K, T)>>(&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, T, S> FromIterator<(K, T)> for LinkedHashMap<K, T, S>
where K: Hash + Eq, S: BuildHasher + Default,

Source§

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

Creates a value from an iterator. Read more
Source§

impl<K, T, S> Index<&K> for LinkedHashMap<K, T, S>
where K: Hash + Eq, S: BuildHasher,

Source§

type Output = T

The returned type after indexing.
Source§

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

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

impl<K, T, S> Index<Ptr> for LinkedHashMap<K, T, S>

Source§

type Output = T

The returned type after indexing.
Source§

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

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

impl<K, T, S> IndexMut<&K> for LinkedHashMap<K, T, S>
where K: Hash + Eq, S: BuildHasher,

Source§

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

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

impl<K, T, S> IndexMut<Ptr> for LinkedHashMap<K, T, S>

Source§

fn index_mut(&mut self, index: Ptr) -> &mut Self::Output

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

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

Source§

type IntoIter = IntoIter<K, T>

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

type Item = (K, T)

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

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

Source§

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

Tests for self and other values to be equal, and is used by ==.
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, T, S> Eq for LinkedHashMap<K, T, S>
where K: Hash + Eq, T: Eq, S: BuildHasher,

Auto Trait Implementations§

§

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

§

impl<K, T, S = RandomState> !RefUnwindSafe for LinkedHashMap<K, T, S>

§

impl<K, T, S = RandomState> !Send for LinkedHashMap<K, T, S>

§

impl<K, T, S = RandomState> !Sync for LinkedHashMap<K, T, S>

§

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

§

impl<K, T, S = RandomState> !UnwindSafe for LinkedHashMap<K, T, 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

Checks if this value is equivalent to the given key. Read more
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.