### LinkedHashSet Get or Insert Element Source: https://context7.com/djc/hashlink/llms.txt Shows how to retrieve an existing element or insert a new one into a LinkedHashSet, returning a reference to the stored value. If the element already exists, a reference to the existing element is returned without duplication. ```rust use hashlink::LinkedHashSet; let mut set: LinkedHashSet = LinkedHashSet::new(); let val = set.get_or_insert("hello".to_string()); assert_eq!(val, "hello"); // Already present — returns ref to existing, does not duplicate let val2 = set.get_or_insert("hello".to_string()); assert_eq!(val2, "hello"); assert_eq!(set.len(), 1); ``` -------------------------------- ### LruCache Get and Get Mut Source: https://context7.com/djc/hashlink/llms.txt Illustrates retrieving a value from an LruCache using `get` or `get_mut`, which marks the accessed element as recently used. This operation moves the element to the back of the LRU list. ```rust use hashlink::LruCache; let mut cache: LruCache = LruCache::new(3); cache.insert(1, "one"); cache.insert(2, "two"); cache.insert(3, "three"); // Access key 1 — it is now most-recently-used let _ = cache.get(&1); cache.insert(4, "four"); // evicts LRU (key 2, not key 1) assert!(!cache.contains_key(&2)); assert!(cache.contains_key(&1)); // 1 was recently used, survives ``` -------------------------------- ### Get or check key existence in LinkedHashMap Source: https://context7.com/djc/hashlink/llms.txt Standard `get`, `get_mut`, and `contains_key` operations perform lookups without affecting the entry's position in the list. `get_mut` allows in-place modification of the value. ```rust use hashlink::LinkedHashMap; let mut map = LinkedHashMap::new(); map.insert("hello", 42u32); assert_eq!(map.get("hello"), Some(&42)); assert!(map.contains_key("hello")); if let Some(v) = map.get_mut("hello") { *v += 1; } assert_eq!(map["hello"], 43); ``` -------------------------------- ### Remove entry from LinkedHashMap by key Source: https://context7.com/djc/hashlink/llms.txt Use `remove` to delete an entry by its key and retrieve its value, or `remove_entry` to get the full key-value pair. Both operations remove the entry from the map and the linked list. ```rust use hashlink::LinkedHashMap; let mut map: LinkedHashMap = LinkedHashMap::new(); map.insert("keep".into(), 1); map.insert("drop".into(), 2); let val = map.remove("drop"); assert_eq!(val, Some(2)); assert!(!map.contains_key("drop")); let pair = map.remove_entry("keep"); assert_eq!(pair, Some(("keep".into(), 1))); assert!(map.is_empty()); ``` -------------------------------- ### LruCache Creation Source: https://context7.com/djc/hashlink/llms.txt Demonstrates the creation of a capacity-bounded LRU cache using `LruCache::new` and an unbounded cache using `new_unbounded`. The bounded cache evicts the least-recently-used entry when capacity is exceeded. ```rust use hashlink::LruCache; let mut cache: LruCache<&str, u32> = LruCache::new(3); cache.insert("a", 1); cache.insert("b", 2); cache.insert("c", 3); cache.insert("d", 4); // evicts "a" (LRU) assert!(!cache.contains_key("a")); assert_eq!(cache.len(), 3); ``` -------------------------------- ### LinkedHashMap Construction Source: https://context7.com/djc/hashlink/llms.txt Demonstrates how to construct a new LinkedHashMap using `new`, `with_capacity`, or `with_hasher`. It also shows that iteration follows insertion order. ```APIDOC ## LinkedHashMap Construction ### Description Constructs a new ordered map. `new` uses the default hasher; `with_capacity` pre-allocates; `with_hasher` accepts any `BuildHasher` implementation. ### Method `LinkedHashMap::new`, `LinkedHashMap::with_capacity`, `LinkedHashMap::with_hasher` ### Example ```rust use hashlink::LinkedHashMap; let mut map: LinkedHashMap<&str, i32> = LinkedHashMap::new(); map.insert("one", 1); map.insert("two", 2); map.insert("three", 3); // Iteration is always in insertion order let keys: Vec<&str> = map.keys().collect(); assert_eq!(keys, ["one", "two", "three"]); ``` ``` -------------------------------- ### LinkedHashMap with Custom Hasher Source: https://context7.com/djc/hashlink/llms.txt Shows how to create a LinkedHashMap with a custom hasher, such as FxBuildHasher from the rustc-hash crate, for performance. ```rust use hashlink::LinkedHashMap; use rustc_hash::FxBuildHasher; let mut map: LinkedHashMap = LinkedHashMap::with_hasher(FxBuildHasher::default()); map.insert(1, "one"); map.insert(2, "two"); assert_eq!(map[&1], "one"); assert_eq!(map.len(), 2); ``` -------------------------------- ### LRU Cache Entry Manipulation with LinkedHashMap Source: https://github.com/djc/hashlink/blob/main/README.md Demonstrates how to interact with an LRU cache using `LinkedHashMap`. It shows how to retrieve an entry, move it to the back on a cache hit, or insert a new entry if it's a cache miss. This approach avoids redundant key hashing and lookups. ```rust let mut lru_cache = LinkedHashMap::new(); let key = "key".to_owned(); // Try to find my expensive to construct and hash key let _cached_val = match lru_cache.raw_entry_mut().from_key(&key) { RawEntryMut::Occupied(mut occupied) => { // Cache hit, move entry to the back. occupied.to_back(); occupied.into_mut() } RawEntryMut::Vacant(vacant) => { // Insert expensive to construct key and expensive to compute value, // automatically inserted at the back. vacant.insert(key.clone(), 42).1 } }; ``` -------------------------------- ### Construct LinkedHashMap with new, with_capacity, or with_hasher Source: https://context7.com/djc/hashlink/llms.txt Use `new` for the default hasher, `with_capacity` to pre-allocate, or `with_hasher` to provide a custom `BuildHasher`. Iteration order is guaranteed to be insertion order. ```rust use hashlink::LinkedHashMap; let mut map: LinkedHashMap<&str, i32> = LinkedHashMap::new(); map.insert("one", 1); map.insert("two", 2); map.insert("three", 3); // Iteration is always in insertion order let keys: Vec<&str> = map.keys().collect(); assert_eq!(keys, ["one", "two", "three"]); ``` -------------------------------- ### LruCache Remove LRU and Set Capacity Source: https://context7.com/djc/hashlink/llms.txt Shows how to manually evict the least-recently-used entry using `remove_lru` and how to resize the cache capacity with `set_capacity`, which may evict excess LRU entries. ```rust use hashlink::LruCache; let mut cache: LruCache = LruCache::new(10); for i in 0..5 { cache.insert(i, "v"); } // Manually remove the least-recently-used entry let lru = cache.remove_lru(); assert_eq!(lru, Some((0, "v"))); // Shrink capacity to 2, evicting excess LRU entries cache.set_capacity(2); assert_eq!(cache.len(), 2); ``` -------------------------------- ### LruCache Entry and Raw Entry Mut Source: https://context7.com/djc/hashlink/llms.txt Demonstrates using `entry` and `raw_entry_mut` for efficient LRU updates, avoiding double-hashing expensive keys. This allows for updating existing entries or inserting new ones while controlling LRU position. ```rust use hashlink::{LruCache, lru_cache::RawEntryMut}; let mut cache: LruCache = LruCache::new(128); cache.insert("foo".into(), 0); let key = "foo".to_owned(); // Single lookup: hit → move to back and update; miss → compute and insert match cache.raw_entry_mut().from_key(&key) { RawEntryMut::Occupied(mut e) => { e.to_back(); *e.get_mut() += 1; } RawEntryMut::Vacant(e) => { e.insert(key.clone(), 1); // expensive computation elided } } assert_eq!(cache.peek(&key), Some(&1)); assert_eq!(cache.back().map(|(k, _)| k.as_str()), Some("foo")); ``` -------------------------------- ### LruCache Peek and Peek Mut Source: https://context7.com/djc/hashlink/llms.txt Shows how to read a value from an LruCache using `peek` or `peek_mut` without promoting it in the LRU order. This is useful when you need to inspect a value without affecting its recency. ```rust use hashlink::LruCache; let mut cache: LruCache = LruCache::new(2); cache.insert(1, "one"); cache.insert(2, "two"); // peek does not change LRU position of key 1 let val = cache.peek(&1); assert_eq!(val, Some(&"one")); cache.insert(3, "three"); // evicts LRU — key 1, because peek didn't promote it assert!(!cache.contains_key(&1)); ``` -------------------------------- ### Simplified LRU Cache Insertion with LinkedHashMap Source: https://github.com/djc/hashlink/blob/main/README.md Provides a more concise method for handling LRU cache entries using `LinkedHashMap`. The `or_insert_with` method simplifies the process of inserting a new entry if the key is not found, automatically placing it at the back. ```rust let mut lru_cache = LinkedHashMap::new(); let key = "key".to_owned(); let _cached_val = lru_cache .raw_entry_mut() .from_key(&key) .or_insert_with(|| (key.clone(), 42)); ``` -------------------------------- ### LinkedHashSet: to_front, to_back, pop_front, pop_back for reordering and consumption Source: https://context7.com/djc/hashlink/llms.txt Reorder set elements or consume them from either end using `to_front`, `to_back`, `pop_front`, and `pop_back`. ```rust use hashlink::LinkedHashSet; let mut set: LinkedHashSet = [1, 2, 3, 4, 5].iter().copied().collect(); set.to_front(&5); set.to_back(&1); let order: Vec = set.iter().copied().collect(); assert_eq!(order, [5, 2, 3, 4, 1]); assert_eq!(set.pop_front(), Some(5)); assert_eq!(set.pop_back(), Some(1)); ``` -------------------------------- ### LinkedHashSet::to_front / to_back / pop_front / pop_back Source: https://context7.com/djc/hashlink/llms.txt These methods allow for reordering set elements or consuming them from either the front or the back of the set. ```APIDOC ## LinkedHashSet::to_front / to_back / pop_front / pop_back ### Description Reorder set elements or consume from either end. ### Usage Example ```rust use hashlink::LinkedHashSet; let mut set: LinkedHashSet = [1, 2, 3, 4, 5].iter().copied().collect(); set.to_front(&5); set.to_back(&1); let order: Vec = set.iter().copied().collect(); assert_eq!(order, [5, 2, 3, 4, 1]); assert_eq!(set.pop_front(), Some(5)); assert_eq!(set.pop_back(), Some(1)); ``` ``` -------------------------------- ### LruCache::new / new_unbounded Source: https://context7.com/djc/hashlink/llms.txt Constructs a new LruCache with a specified capacity or an unbounded cache that never evicts entries. ```APIDOC ## LruCache::new / new_unbounded Capacity-bounded LRU cache. `new(n)` evicts the least-recently-used entry when the `n+1`th unique key is inserted. `new_unbounded()` never evicts. ### Example ```rust use hashlink::LruCache; let mut cache: LruCache<&str, u32> = LruCache::new(3); cache.insert("a", 1); cache.insert("b", 2); cache.insert("c", 3); cache.insert("d", 4); // evicts "a" (LRU) assert!(!cache.contains_key("a")); assert_eq!(cache.len(), 3); ``` ``` -------------------------------- ### Pop or peek at front/back of LinkedHashMap Source: https://context7.com/djc/hashlink/llms.txt Use `pop_front` and `pop_back` to remove and return the first or last entries, respectively. `front` and `back` provide immutable references to these entries without removing them. ```rust use hashlink::LinkedHashMap; let mut map = LinkedHashMap::new(); map.insert("a", 1); map.insert("b", 2); map.insert("c", 3); assert_eq!(map.front(), Some((&"a", &1))); assert_eq!(map.back(), Some((&"c", &3))); let first = map.pop_front(); assert_eq!(first, Some(("a", 1))); assert_eq!(map.front(), Some((&"b", &2))); ``` -------------------------------- ### LinkedHashMap::raw_entry_mut API Source: https://context7.com/djc/hashlink/llms.txt The `raw_entry_mut` API offers low-level entry manipulation, avoiding double-hashing for performance-critical keys or custom hash strategies. This pattern is fundamental for efficient LRU cache access. ```APIDOC ## LinkedHashMap::raw_entry_mut API ### Description Low-level entry API that avoids double-hashing for expensive keys or custom hash strategies. The pattern is the basis for efficient LRU access. ### Usage Example ```rust use hashlink::{LinkedHashMap, linked_hash_map::RawEntryMut}; let mut cache: LinkedHashMap = LinkedHashMap::new(); cache.insert("alpha".into(), 100); cache.insert("beta".into(), 200); let key = "alpha".to_owned(); let value = match cache.raw_entry_mut().from_key(&key) { RawEntryMut::Occupied(mut e) => { e.to_back(); // mark as recently used *e.get() } RawEntryMut::Vacant(e) => { // Insert expensive-to-compute value at back in one shot *e.insert(key.clone(), 42).1 } }; assert_eq!(value, 100); assert_eq!(cache.back().map(|(k, _)| k.as_str()), Some("alpha")); ``` ``` -------------------------------- ### LruCache::entry and raw_entry_mut Source: https://context7.com/djc/hashlink/llms.txt Provides efficient LRU updates by avoiding double-hashing expensive keys and allowing control over LRU position. ```APIDOC ## LruCache::entry and raw_entry_mut for Efficient LRU Updates Avoid double-hashing expensive keys while still controlling LRU position. ### Example ```rust use hashlink::{LruCache, lru_cache::RawEntryMut}; let mut cache: LruCache = LruCache::new(128); cache.insert("foo".into(), 0); let key = "foo".to_owned(); // Single lookup: hit → move to back and update; miss → compute and insert match cache.raw_entry_mut().from_key(&key) { RawEntryMut::Occupied(mut e) => { e.to_back(); *e.get_mut() += 1; } RawEntryMut::Vacant(e) => { e.insert(key.clone(), 1); // expensive computation elided } } assert_eq!(cache.peek(&key), Some(&1)); assert_eq!(cache.back().map(|(k, _)| k.as_str()), Some("foo")); ``` ``` -------------------------------- ### LinkedHashMap::raw_entry_mut API for low-level access Source: https://context7.com/djc/hashlink/llms.txt Utilize `raw_entry_mut` for efficient LRU access, especially with expensive keys or custom hash strategies. It avoids double-hashing and allows direct manipulation of entries. ```rust use hashlink::{LinkedHashMap, linked_hash_map::RawEntryMut}; let mut cache: LinkedHashMap = LinkedHashMap::new(); cache.insert("alpha".into(), 100); cache.insert("beta".into(), 200); let key = "alpha".to_owned(); let value = match cache.raw_entry_mut().from_key(&key) { RawEntryMut::Occupied(mut e) => { e.to_back(); // mark as recently used *e.get() } RawEntryMut::Vacant(e) => { // Insert expensive-to-compute value at back in one shot *e.insert(key.clone(), 42).1 } }; assert_eq!(value, 100); assert_eq!(cache.back().map(|(k, _)| k.as_str()), Some("alpha")); ``` -------------------------------- ### LinkedHashMap and LinkedHashSet Serde Round-trip Source: https://context7.com/djc/hashlink/llms.txt Demonstrates serializing and deserializing LinkedHashMap and LinkedHashSet with serde_json, preserving insertion order for maps and element order for sets. ```rust use hashlink::{LinkedHashMap, LinkedHashSet}; use serde_json; // LinkedHashMap round-trip — insertion order preserved let mut map: LinkedHashMap<&str, i32> = LinkedHashMap::new(); map.insert("first", 1); map.insert("second", 2); map.insert("third", 3); let json = serde_json::to_string(&map).unwrap(); assert_eq!(json, r#"{"first":1,"second":2,"third":3}"#); let restored: LinkedHashMap = serde_json::from_str(&json).unwrap(); let keys: Vec<&str> = restored.keys().map(String::as_str).collect(); assert_eq!(keys, ["first", "second", "third"]); // LinkedHashSet round-trip — serialized as a JSON array let mut set: LinkedHashSet<&str> = LinkedHashSet::new(); set.insert("alpha"); set.insert("beta"); let json_set = serde_json::to_string(&set).unwrap(); assert_eq!(json_set, r#"["alpha","beta"]"#); let restored_set: LinkedHashSet = serde_json::from_str(&json_set).unwrap(); assert!(restored_set.contains("alpha")); ``` -------------------------------- ### Serde Support for LinkedHashMap and LinkedHashSet Source: https://context7.com/djc/hashlink/llms.txt Enables serialization and deserialization for LinkedHashMap and LinkedHashSet, preserving insertion order. ```APIDOC ## Serde Support ### Serialize and Deserialize `LinkedHashMap` and `LinkedHashSet` Enable with `features = ["serde_impl"]` in `Cargo.toml`. Insertion order is preserved through the serialization round-trip. ### Example ```toml [dependencies] hashlink = { version = "...", features = ["serde_impl"] } serde = { version = "...", features = ["derive"] } ``` ``` -------------------------------- ### LinkedHashMap::pop_front, pop_back, front, back Source: https://context7.com/djc/hashlink/llms.txt Provides methods to peek at or remove the first (`front`, `pop_front`) or last (`back`, `pop_back`) entry in the linked list. ```APIDOC ## LinkedHashMap::pop_front / pop_back / front / back ### Description Peek at or remove the first/last entry in the list. ### Method `LinkedHashMap::pop_front`, `LinkedHashMap::pop_back`, `LinkedHashMap::front`, `LinkedHashMap::back` ### Example ```rust use hashlink::LinkedHashMap; let mut map = LinkedHashMap::new(); map.insert("a", 1); map.insert("b", 2); map.insert("c", 3); assert_eq!(map.front(), Some((&"a", &1))); assert_eq!(map.back(), Some((&"c", &3))); let first = map.pop_front(); assert_eq!(first, Some(("a", 1))); assert_eq!(map.front(), Some((&"b", &2))); ``` ``` -------------------------------- ### LinkedHashMap::entry API for combined lookup-or-insert Source: https://context7.com/djc/hashlink/llms.txt Use the entry API for efficient lookups and insertions. `or_insert` and `or_insert_with` move occupied entries to the back, preserving LRU semantics. ```rust use hashlink::LinkedHashMap; let mut word_count: LinkedHashMap<&str, usize> = LinkedHashMap::new(); let sentence = "the quick brown fox jumps over the lazy fox"; for word in sentence.split_whitespace() { *word_count.entry(word).or_insert(0) += 1; } // "the" and "fox" were re-inserted (moved to back on second encounter) assert_eq!(word_count["the"], 2); assert_eq!(word_count["fox"], 2); // last unique word seen is last in iteration order assert_eq!(word_count.back().map(|(k, _)| *k), Some("fox")); ``` -------------------------------- ### Move LinkedHashMap entry to front or back Source: https://context7.com/djc/hashlink/llms.txt The `to_front` and `to_back` methods allow reordering of existing entries within the linked list without modifying their associated values. ```rust use hashlink::LinkedHashMap; let mut map = LinkedHashMap::new(); map.insert(1, "a"); map.insert(2, "b"); map.insert(3, "c"); map.to_front(&3); // move 3 to front map.to_back(&1); // move 1 to back let order: Vec = map.keys().copied().collect(); assert_eq!(order, [3, 2, 1]); ``` -------------------------------- ### LruCache::remove_lru / set_capacity Source: https://context7.com/djc/hashlink/llms.txt Allows manual eviction of the least-recently-used entry or resizing the cache, evicting excess entries as needed. ```APIDOC ## LruCache::remove_lru / set_capacity Manually evict the LRU entry or resize the cache (evicting as needed). ### Example ```rust use hashlink::LruCache; let mut cache: LruCache = LruCache::new(10); for i in 0..5 { cache.insert(i, "v"); } // Manually remove the least-recently-used entry let lru = cache.remove_lru(); assert_eq!(lru, Some((0, "v"))); // Shrink capacity to 2, evicting excess LRU entries cache.set_capacity(2); assert_eq!(cache.len(), 2); ``` ``` -------------------------------- ### LinkedHashSet Set Operations Source: https://context7.com/djc/hashlink/llms.txt Demonstrates set operations like union, intersection, difference, and symmetric difference using operator overloads for LinkedHashSet. Note that equality checks on LinkedHashSet are ordered. ```rust use hashlink::LinkedHashSet; let a: LinkedHashSet = [1, 2, 3, 4].iter().copied().collect(); let b: LinkedHashSet = [3, 4, 5, 6].iter().copied().collect(); let union_op: LinkedHashSet = &a | &b; let inter_op: LinkedHashSet = &a & &b; let diff_op: LinkedHashSet = &a - &b; let sym_diff: LinkedHashSet = &a ^ &b; // Note: equality on LinkedHashSet is *ordered* let union_items: Vec = union_op.iter().copied().collect(); let inter_items: Vec = inter_op.iter().copied().collect(); let diff_items: Vec = diff_op.iter().copied().collect(); assert_eq!(inter_items, [3, 4]); assert_eq!(diff_items, [1, 2]); println!("union: {:?}", union_items); // [1, 2, 3, 4, 5, 6] ``` -------------------------------- ### LruCache::get / get_mut Source: https://context7.com/djc/hashlink/llms.txt Retrieves a value from the LruCache and marks it as recently used, moving it to the back of the LRU list. ```APIDOC ## LruCache::get / get_mut Retrieve a value and mark it as **recently used** (moves it to the back of the LRU list). ### Example ```rust use hashlink::LruCache; let mut cache: LruCache = LruCache::new(3); cache.insert(1, "one"); cache.insert(2, "two"); cache.insert(3, "three"); // Access key 1 — it is now most-recently-used let _ = cache.get(&1); cache.insert(4, "four"); // evicts LRU (key 2, not key 1) assert!(!cache.contains_key(&2)); assert!(cache.contains_key(&1)); // 1 was recently used, survives ``` ``` -------------------------------- ### LinkedHashSet Set Operations Source: https://context7.com/djc/hashlink/llms.txt Demonstrates the set operations like union, intersection, difference, and symmetric difference supported by LinkedHashSet, including operator overloads. ```APIDOC ## LinkedHashSet Set Operations Supports `difference`, `intersection`, `union`, `symmetric_difference`, plus operator overloads `|`, `&`, `^`, `-`. ### Example ```rust use hashlink::LinkedHashSet; let a: LinkedHashSet = [1, 2, 3, 4].iter().copied().collect(); let b: LinkedHashSet = [3, 4, 5, 6].iter().copied().collect(); let union_op: LinkedHashSet = &a | &b; let inter_op: LinkedHashSet = &a & &b; let diff_op: LinkedHashSet = &a - &b; let sym_diff: LinkedHashSet = &a ^ &b; // Note: equality on LinkedHashSet is *ordered* let union_items: Vec = union_op.iter().copied().collect(); let inter_items: Vec = inter_op.iter().copied().collect(); let diff_items: Vec = diff_op.iter().copied().collect(); assert_eq!(inter_items, [3, 4]); assert_eq!(diff_items, [1, 2]); println!("union: {{:?}}", union_items); // [1, 2, 3, 4, 5, 6] ``` ``` -------------------------------- ### LinkedHashMap::cursor_front_mut / cursor_back_mut Source: https://context7.com/djc/hashlink/llms.txt Provides a mutable cursor API for detailed traversal and positional insertion or modification of map elements. Cursors can be moved from the front or back. ```APIDOC ## LinkedHashMap::cursor_front_mut / cursor_back_mut ### Description Mutable cursor API for fine-grained traversal and positional insertion/modification. ### Usage Example ```rust use hashlink::LinkedHashMap; let mut map: LinkedHashMap = LinkedHashMap::new(); map.insert(1, "one"); map.insert(2, "two"); map.insert(3, "three"); let mut cursor = map.cursor_front_mut(); // Inspect current element at front assert_eq!(cursor.current(), Some((&1, &mut "one"))); // Insert a new entry immediately before the cursor position cursor.insert_before(0, "zero"); // Peek ahead without advancing assert_eq!(cursor.peek_next(), Some((&2, &mut "two"))); // Advance and check cursor.move_next(); assert_eq!(cursor.current(), Some((&2, &mut "two"))); ``` ``` -------------------------------- ### Insert into LinkedHashMap, moving entry to back Source: https://context7.com/djc/hashlink/llms.txt The `insert` method adds a new key-value pair or replaces an existing one, always moving the entry to the back of the internal list. It returns the old value if the key was already present. ```rust use hashlink::LinkedHashMap; let mut map = LinkedHashMap::new(); map.insert("a", 1); map.insert("b", 2); let old = map.insert("a", 99); // "a" is moved to back, old value returned assert_eq!(old, Some(1)); let order: Vec<(&str, i32)> = map.iter().map(|(&k, &v)| (k, v)).collect(); assert_eq!(order, [("b", 2), ("a", 99)]); // "a" is now last ``` -------------------------------- ### LinkedHashSet::get_or_insert / get_or_insert_with Source: https://context7.com/djc/hashlink/llms.txt Retrieves an existing element from a LinkedHashSet or inserts a new one if it doesn't exist, returning a reference to the stored value. ```APIDOC ## LinkedHashSet::get_or_insert / get_or_insert_with Retrieve an existing element or insert a new one, returning a reference to the stored value. ### Example ```rust use hashlink::LinkedHashSet; let mut set: LinkedHashSet = LinkedHashSet::new(); let val = set.get_or_insert("hello".to_string()); assert_eq!(val, "hello"); // Already present — returns ref to existing, does not duplicate let val2 = set.get_or_insert("hello".to_string()); assert_eq!(val2, "hello"); assert_eq!(set.len(), 1); ``` ``` -------------------------------- ### LinkedHashMap::cursor_front_mut for mutable traversal and insertion Source: https://context7.com/djc/hashlink/llms.txt Employ the mutable cursor API for fine-grained traversal and positional insertion or modification. `cursor_front_mut` allows operations from the front of the map. ```rust use hashlink::LinkedHashMap; let mut map: LinkedHashMap = LinkedHashMap::new(); map.insert(1, "one"); map.insert(2, "two"); map.insert(3, "three"); let mut cursor = map.cursor_front_mut(); // Inspect current element at front assert_eq!(cursor.current(), Some((&1, &mut "one"))); // Insert a new entry immediately before the cursor position cursor.insert_before(0, "zero"); // Peek ahead without advancing assert_eq!(cursor.peek_next(), Some((&2, &mut "two"))); // Advance and check cursor.move_next(); assert_eq!(cursor.current(), Some((&2, &mut "two"))); ``` -------------------------------- ### LinkedHashMap::entry API Source: https://context7.com/djc/hashlink/llms.txt The `Entry` API allows for combined lookup-or-insert operations. Methods like `or_insert` and `or_insert_with` move an occupied entry to the back of the map, preserving LRU semantics. ```APIDOC ## LinkedHashMap::entry API ### Description The `Entry` API provides combined lookup-or-insert, preserving LRU semantics: `or_insert` / `or_insert_with` move an occupied entry to the **back**. ### Usage Example ```rust use hashlink::LinkedHashMap; let mut word_count: LinkedHashMap<&str, usize> = LinkedHashMap::new(); let sentence = "the quick brown fox jumps over the lazy fox"; for word in sentence.split_whitespace() { *word_count.entry(word).or_insert(0) += 1; } // "the" and "fox" were re-inserted (moved to back on second encounter) assert_eq!(word_count["the"], 2); assert_eq!(word_count["fox"], 2); // last unique word seen is last in iteration order assert_eq!(word_count.back().map(|(k, _)| *k), Some("fox")); ``` ``` -------------------------------- ### LruCache::peek / peek_mut Source: https://context7.com/djc/hashlink/llms.txt Reads a value from the LruCache without changing its position in the LRU order. ```APIDOC ## LruCache::peek / peek_mut Read a value **without** promoting it in the LRU order. ### Example ```rust use hashlink::LruCache; let mut cache: LruCache = LruCache::new(2); cache.insert(1, "one"); cache.insert(2, "two"); // peek does not change LRU position of key 1 let val = cache.peek(&1); assert_eq!(val, Some(&"one")); cache.insert(3, "three"); // evicts LRU — key 1, because peek didn't promote it assert!(!cache.contains_key(&1)); ``` ``` -------------------------------- ### LinkedHashMap::to_front, to_back Source: https://context7.com/djc/hashlink/llms.txt Moves an existing entry to either the front or the back of the linked list without altering its associated value. ```APIDOC ## LinkedHashMap::to_front / to_back ### Description Moves an existing entry to the front or back of the list without changing its value. ### Method `LinkedHashMap::to_front`, `LinkedHashMap::to_back` ### Example ```rust use hashlink::LinkedHashMap; let mut map = LinkedHashMap::new(); map.insert(1, "a"); map.insert(2, "b"); map.insert(3, "c"); map.to_front(&3); // move 3 to front map.to_back(&1); // move 1 to back let order: Vec = map.keys().copied().collect(); assert_eq!(order, [3, 2, 1]); ``` ``` -------------------------------- ### LinkedHashSet: new, insert, contains, remove operations Source: https://context7.com/djc/hashlink/llms.txt An ordered hash set backed by `LinkedHashMap`. Insertion order is preserved, and `insert` moves existing values to the back. ```rust use hashlink::LinkedHashSet; let mut set: LinkedHashSet<&str> = LinkedHashSet::new(); set.insert("apple"); set.insert("banana"); set.insert("cherry"); set.insert("apple"); // moves "apple" to back assert!(set.contains("banana")); assert_eq!(set.front(), Some(&"banana")); assert_eq!(set.back(), Some(&"apple")); // apple was re-inserted set.remove("banana"); let items: Vec<&str> = set.iter().copied().collect(); assert_eq!(items, ["cherry", "apple"]); ``` -------------------------------- ### LinkedHashMap::get, get_mut, contains_key Source: https://context7.com/djc/hashlink/llms.txt Provides standard key lookup methods. These operations do not affect the entry's position in the linked list. ```APIDOC ## LinkedHashMap::get / get_mut / contains_key ### Description Standard key lookup — does **not** affect the entry's position in the list. ### Method `LinkedHashMap::get`, `LinkedHashMap::get_mut`, `LinkedHashMap::contains_key` ### Example ```rust use hashlink::LinkedHashMap; let mut map = LinkedHashMap::new(); map.insert("hello", 42u32); assert_eq!(map.get("hello"), Some(&42)); assert!(map.contains_key("hello")); if let Some(v) = map.get_mut("hello") { *v += 1; } assert_eq!(map["hello"], 43); ``` ``` -------------------------------- ### LinkedHashMap::retain / retain_with_order Source: https://context7.com/djc/hashlink/llms.txt The `retain` method removes entries that do not satisfy a given predicate, operating in hash-table order. `retain_with_order` iterates in linked-list order, which is beneficial when the order of removal is significant. ```APIDOC ## LinkedHashMap::retain / retain_with_order ### Description `retain` removes entries not satisfying the predicate (hash-table order). `retain_with_order` iterates in linked-list order, which is useful when removal order matters. ### Usage Example ```rust use hashlink::LinkedHashMap; let mut map: LinkedHashMap = (0..10).map(|i| (i, i * 2)).collect(); // Keep only even keys, iterating in insertion order map.retain_with_order(|k, _v| k % 2 == 0); let keys: Vec = map.keys().copied().collect(); assert_eq!(keys, [0, 2, 4, 6, 8]); ``` ``` -------------------------------- ### LinkedHashSet::new / insert / contains / remove Source: https://context7.com/djc/hashlink/llms.txt The `LinkedHashSet` is an ordered hash set built upon `LinkedHashMap`. It preserves insertion order, and the `insert` operation moves existing elements to the back. ```APIDOC ## LinkedHashSet::new / insert / contains / remove ### Description An ordered hash set backed by `LinkedHashMap`. Insertion order is preserved; `insert` moves existing values to the back. ### Usage Example ```rust use hashlink::LinkedHashSet; let mut set: LinkedHashSet<&str> = LinkedHashSet::new(); set.insert("apple"); set.insert("banana"); set.insert("cherry"); set.insert("apple"); // moves "apple" to back assert!(set.contains("banana")); assert_eq!(set.front(), Some(&"banana")); assert_eq!(set.back(), Some(&"apple")); // apple was re-inserted set.remove("banana"); let items: Vec<&str> = set.iter().copied().collect(); assert_eq!(items, ["cherry", "apple"]); ``` ``` -------------------------------- ### LinkedHashMap::insert Source: https://context7.com/djc/hashlink/llms.txt Inserts or replaces a key-value pair at the back of the list. If the key already exists, its old value is returned, and the entry is moved to the back. ```APIDOC ## LinkedHashMap::insert ### Description Inserts (or replaces) a key-value pair at the **back** of the list. If the key was already present the old value is returned and the entry is moved to the back. ### Method `LinkedHashMap::insert` ### Request Body Example ```rust use hashlink::LinkedHashMap; let mut map = LinkedHashMap::new(); map.insert("a", 1); map.insert("b", 2); let old = map.insert("a", 99); // "a" is moved to back, old value returned assert_eq!(old, Some(1)); let order: Vec<(&str, i32)> = map.iter().map(|(&k, &v)| (k, v)).collect(); assert_eq!(order, [("b", 2), ("a", 99)]); // "a" is now last ``` ``` -------------------------------- ### LinkedHashMap::replace Source: https://context7.com/djc/hashlink/llms.txt Replaces a value without changing the entry's position in the list. Returns the previous value if the key existed; otherwise, it inserts the new entry at the back. ```APIDOC ## LinkedHashMap::replace ### Description Replaces a value **without** moving the entry in the list. Returns the previous value if the key existed; inserts at back if not. ### Method `LinkedHashMap::replace` ### Request Body Example ```rust use hashlink::LinkedHashMap; let mut map = LinkedHashMap::new(); map.insert("x", 10); map.insert("y", 20); let old = map.replace("x", 99); // position of "x" unchanged assert_eq!(old, Some(10)); let order: Vec<_> = map.keys().copied().collect(); assert_eq!(order, ["x", "y"]); // "x" still first ``` ``` -------------------------------- ### LinkedHashMap::retain_with_order for conditional removal Source: https://context7.com/djc/hashlink/llms.txt Use `retain_with_order` to remove entries that do not satisfy a predicate, iterating in linked-list order. This is useful when the order of removal matters. ```rust use hashlink::LinkedHashMap; let mut map: LinkedHashMap = (0..10).map(|i| (i, i * 2)).collect(); // Keep only even keys, iterating in insertion order map.retain_with_order(|k, _v| k % 2 == 0); let keys: Vec = map.keys().copied().collect(); assert_eq!(keys, [0, 2, 4, 6, 8]); ``` -------------------------------- ### LinkedHashMap Iterators: Forward, Reverse, and Drain Source: https://context7.com/djc/hashlink/llms.txt Iterators for `LinkedHashMap` implement `DoubleEndedIterator` and `ExactSizeIterator`. The `drain` method removes all entries, recycling internal nodes. ```rust use hashlink::LinkedHashMap; let mut map: LinkedHashMap<&str, i32> = [("a", 1), ("b", 2), ("c", 3)].into_iter().collect(); // Forward iteration let fwd: Vec<_> = map.iter().map(|(&k, &v)| (k, v)).collect(); assert_eq!(fwd, [("a", 1), ("b", 2), ("c", 3)]); // Reverse iteration let rev: Vec<_> = map.iter().rev().map(|(&k, &v)| (k, v)).collect(); assert_eq!(rev, [("c", 3), ("b", 2), ("a", 1)]); // Drain (empties the map, returns owned pairs) let drained: Vec<_> = map.drain().collect(); assert_eq!(drained, [("a", 1), ("b", 2), ("c", 3)]); assert!(map.is_empty()); ``` -------------------------------- ### Replace value in LinkedHashMap without changing order Source: https://context7.com/djc/hashlink/llms.txt Use `replace` to update a value associated with a key without altering the entry's position in the linked list. If the key does not exist, it is inserted at the back. ```rust use hashlink::LinkedHashMap; let mut map = LinkedHashMap::new(); map.insert("x", 10); map.insert("y", 20); let old = map.replace("x", 99); // position of "x" unchanged assert_eq!(old, Some(10)); let order: Vec<_> = map.keys().copied().collect(); assert_eq!(order, ["x", "y"]); // "x" still first ``` -------------------------------- ### LinkedHashMap Iterators Source: https://context7.com/djc/hashlink/llms.txt All iterators for `LinkedHashMap` implement `DoubleEndedIterator` and `ExactSizeIterator`. The `drain` method removes all entries while recycling internal nodes. ```APIDOC ## LinkedHashMap Iterators ### Description All iterators implement `DoubleEndedIterator` and `ExactSizeIterator`. `drain` removes all entries, recycling internal nodes. ### Usage Example ```rust use hashlink::LinkedHashMap; let mut map: LinkedHashMap<&str, i32> = [("a", 1), ("b", 2), ("c", 3)].into_iter().collect(); // Forward iteration let fwd: Vec<_> = map.iter().map(|(&k, &v)| (k, v)).collect(); assert_eq!(fwd, [("a", 1), ("b", 2), ("c", 3)]); // Reverse iteration let rev: Vec<_> = map.iter().rev().map(|(&k, &v)| (k, v)).collect(); assert_eq!(rev, [("c", 3), ("b", 2), ("a", 1)]); // Drain (empties the map, returns owned pairs) let drained: Vec<_> = map.drain().collect(); assert_eq!(drained, [("a", 1), ("b", 2), ("c", 3)]); assert!(map.is_empty()); ``` ``` -------------------------------- ### LinkedHashMap::remove, remove_entry Source: https://context7.com/djc/hashlink/llms.txt Removes an entry from the map by its key. `remove` returns the value, while `remove_entry` returns the entire key-value pair. ```APIDOC ## LinkedHashMap::remove / remove_entry ### Description Removes an entry by key and returns the value or the full key-value pair. ### Method `LinkedHashMap::remove`, `LinkedHashMap::remove_entry` ### Example ```rust use hashlink::LinkedHashMap; let mut map: LinkedHashMap = LinkedHashMap::new(); map.insert("keep".into(), 1); map.insert("drop".into(), 2); let val = map.remove("drop"); assert_eq!(val, Some(2)); assert!(!map.contains_key("drop")); let pair = map.remove_entry("keep"); assert_eq!(pair, Some(("keep".into(), 1))); assert!(map.is_empty()); ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.