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 implementHash + Eq
T
: Value typeS
: 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>
impl<K, T> LinkedHashMap<K, T>
Sourcepub fn with_capacity(capacity: usize) -> Self
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);
Sourcepub fn new() -> Self
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>
impl<K, T, S> LinkedHashMap<K, T, S>
Sourcepub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self
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);
Sourcepub fn move_after(&mut self, moved: Ptr, after: Ptr) -> Option<()>
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 moveafter
- The pointer to the entry after whichmoved
will be placed
§Returns
Some(())
if the move was successfulNone
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)]);
Sourcepub fn move_before(&mut self, moved: Ptr, before: Ptr) -> Option<()>
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 movebefore
- The pointer to the entry before whichmoved
will be placed
§Returns
Some(())
if the move was successfulNone
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)]);
Sourcepub fn link_as_head(&mut self, ptr: Ptr) -> Option<()>
pub fn link_as_head(&mut self, ptr: Ptr) -> Option<()>
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 successfulNone
if the pointer is invalid
Sourcepub fn link_as_tail(&mut self, ptr: Ptr) -> Option<()>
pub fn link_as_tail(&mut self, ptr: Ptr) -> Option<()>
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 successfulNone
if the pointer is invalid
Sourcepub fn link_after(&mut self, ptr: Ptr, prev: Ptr) -> Option<()>
pub fn link_after(&mut self, ptr: Ptr, prev: Ptr) -> Option<()>
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.
Sourcepub fn link_before(&mut self, ptr: Ptr, next: Ptr) -> Option<()>
pub fn link_before(&mut self, ptr: Ptr, next: Ptr) -> Option<()>
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.
Sourcepub fn move_to_tail(&mut self, moved: Ptr) -> Option<()>
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 successfulNone
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)]);
Sourcepub fn move_to_head(&mut self, moved: Ptr) -> Option<()>
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 successfulNone
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)]);
Sourcepub fn ptr_cursor_mut(&mut self, ptr: Ptr) -> CursorMut<'_, K, T, S>
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;
}
Sourcepub fn next_ptr(&self, ptr: Ptr) -> Option<Ptr>
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 entryNone
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));
Sourcepub fn prev_ptr(&self, ptr: Ptr) -> Option<Ptr>
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 entryNone
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));
Sourcepub fn head_cursor_mut(&mut self) -> CursorMut<'_, K, T, S>
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;
}
Sourcepub fn tail_cursor_mut(&mut self) -> CursorMut<'_, K, T, S>
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;
}
Sourcepub fn head_ptr(&self) -> Option<Ptr>
pub fn head_ptr(&self) -> Option<Ptr>
Returns the pointer to the head (first entry) of the linked list.
Sourcepub fn tail_ptr(&self) -> Option<Ptr>
pub fn tail_ptr(&self) -> Option<Ptr>
Returns the pointer to the tail (last entry) of the linked list.
Sourcepub fn ptr_get(&self, ptr: Ptr) -> Option<&T>
pub fn ptr_get(&self, ptr: Ptr) -> Option<&T>
Returns a reference to the value associated with the given pointer.
Sourcepub fn ptr_get_entry(&self, ptr: Ptr) -> Option<(&K, &T)>
pub fn ptr_get_entry(&self, ptr: Ptr) -> Option<(&K, &T)>
Returns a reference to the key-value pair associated with the given pointer.
Sourcepub fn ptr_get_entry_mut(&mut self, ptr: Ptr) -> Option<(&K, &mut T)>
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.
Sourcepub fn ptr_get_mut(&mut self, ptr: Ptr) -> Option<&mut T>
pub fn ptr_get_mut(&mut self, ptr: Ptr) -> Option<&mut T>
Returns a mutable reference to the value associated with the given pointer.
Sourcepub fn ptr_get_key(&self, ptr: Ptr) -> Option<&K>
pub fn ptr_get_key(&self, ptr: Ptr) -> Option<&K>
Returns a reference to the key associated with the given pointer.
Sourcepub fn len(&self) -> usize
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);
Sourcepub fn is_empty(&self) -> bool
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());
Sourcepub fn clear(&mut self)
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());
Sourcepub fn iter<'s>(&'s self) -> Iter<'s, K, T> ⓘ
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);
}
Sourcepub fn contains_ptr(&self, ptr: Ptr) -> bool
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 pointerfalse
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));
Sourcepub fn keys(&self) -> impl Iterator<Item = &K>
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"]);
Sourcepub fn values(&self) -> impl Iterator<Item = &T>
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]);
Sourcepub fn values_mut<'s>(&'s mut self) -> ValuesMut<'s, K, T> ⓘ
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]);
Sourcepub fn iter_mut<'s>(&'s mut self) -> IterMut<'s, K, T> ⓘ
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>
impl<K: Hash + Eq, T, S: BuildHasher> LinkedHashMap<K, T, S>
Sourcepub fn shrink_to_fit(&mut self)
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.
Sourcepub fn remove_tail(&mut self) -> Option<(Ptr, RemovedEntry<K, T>)>
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.
Sourcepub fn remove_head(&mut self) -> Option<(Ptr, RemovedEntry<K, T>)>
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.
Sourcepub fn remove_ptr(&mut self, ptr: Ptr) -> Option<RemovedEntry<K, T>>
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.
Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
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 returnstrue
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));
Sourcepub fn insert(&mut self, key: K, value: T) -> Option<T>
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"));
Sourcepub fn insert_full(&mut self, key: K, value: T) -> (Ptr, Option<T>)
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 updatevalue
- The value to insert
§Returns
A tuple containing:
Ptr
- The pointer to the inserted or updated entryOption<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));
Sourcepub fn insert_tail(&mut self, key: K, value: T) -> Option<T>
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"]);
Sourcepub fn insert_tail_full(&mut self, key: K, value: T) -> (Ptr, Option<T>)
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 updatevalue
- The value to insert
§Returns
A tuple containing:
Ptr
- The pointer to the inserted or updated entryOption<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)]);
Sourcepub fn insert_head(&mut self, key: K, value: T) -> Option<T>
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
Sourcepub fn insert_head_full(&mut self, key: K, value: T) -> (Ptr, Option<T>)
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 updatevalue
- The value to insert
§Returns
A tuple containing:
Ptr
- The pointer to the inserted or updated entryOption<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)]);
Sourcepub fn key_cursor_mut<Q>(&mut self, key: &Q) -> CursorMut<'_, K, T, S>
pub fn key_cursor_mut<Q>(&mut self, key: &Q) -> CursorMut<'_, K, T, S>
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;
}
Sourcepub fn entry(&mut self, key: K) -> Entry<'_, K, T>
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 valueEntry::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,
};
Sourcepub fn remove<Q>(&mut self, key: &Q) -> Option<T>
pub fn remove<Q>(&mut self, key: &Q) -> Option<T>
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);
Sourcepub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, T)>
pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, T)>
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);
Sourcepub fn remove_full<Q>(&mut self, key: &Q) -> Option<(Ptr, RemovedEntry<K, T>)>
pub fn remove_full<Q>(&mut self, key: &Q) -> Option<(Ptr, RemovedEntry<K, T>)>
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, wherePtr
is the pointer to the removed entry andRemovedEntry<K , T>
contains the key, value, and neighboring pointersNone
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);
Sourcepub fn get_ptr<Q>(&self, key: &Q) -> Option<Ptr>
pub fn get_ptr<Q>(&self, key: &Q) -> Option<Ptr>
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 mapNone
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);
Sourcepub fn get<Q>(&self, key: &Q) -> Option<&T>
pub fn get<Q>(&self, key: &Q) -> Option<&T>
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);
Sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut T>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut T>
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"));
Sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
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>
impl<K: Hash + Eq + Clone, T: Clone, S: BuildHasher + Clone> Clone for LinkedHashMap<K, T, S>
Source§impl<K, T, S: BuildHasher + Default> Default for LinkedHashMap<K, T, S>
impl<K, T, S: BuildHasher + Default> Default for LinkedHashMap<K, T, S>
Source§impl<'a, K, T, S> Extend<(&'a K, &'a T)> for LinkedHashMap<K, T, S>
impl<'a, K, T, S> Extend<(&'a K, &'a T)> for LinkedHashMap<K, T, S>
Source§fn extend<I: IntoIterator<Item = (&'a K, &'a T)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (&'a K, &'a T)>>(&mut self, iter: I)
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, T, S> Extend<(K, T)> for LinkedHashMap<K, T, S>
impl<K, T, S> Extend<(K, T)> for LinkedHashMap<K, T, S>
Source§fn extend<I: IntoIterator<Item = (K, T)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (K, T)>>(&mut self, iter: I)
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
)