pub struct LRUCache<K: Ord + Clone, V, const MAX_SIZE: usize> {
inner: BTreeMap<K, Node<K, V>>,
head: Option<K>,
tail: Option<K>,
}Expand description
An Least Recently Used Cache with capacity MAX_SIZE.
Fields§
§inner: BTreeMap<K, Node<K, V>>§head: Option<K>§tail: Option<K>Implementations§
Source§impl<K: Ord + Clone, V, const MAX_SIZE: usize> LRUCache<K, V, MAX_SIZE>
impl<K: Ord + Clone, V, const MAX_SIZE: usize> LRUCache<K, V, MAX_SIZE>
fn attach(&mut self, k: K) -> &mut Node<K, V>
fn detach(&mut self, prev: Option<K>, next: Option<K>)
Sourcepub fn get(&mut self, k: K) -> Option<&mut V>
pub fn get(&mut self, k: K) -> Option<&mut V>
Returns a mutable reference to the value corresponding to the key and update the last access time.
Sourcepub fn get_or_insert_with<E>(
&mut self,
k: K,
f: impl FnOnce() -> Result<V, E>,
) -> Result<&mut V, E>
pub fn get_or_insert_with<E>( &mut self, k: K, f: impl FnOnce() -> Result<V, E>, ) -> Result<&mut V, E>
Inserts the value computed with f into the LRUCache if it is not
present, then returns a reference to the value in the LRUCache.
fn __put(&mut self, k: K, v: V) -> &mut Node<K, V>
Sourcepub fn put(&mut self, k: K, v: V)
pub fn put(&mut self, k: K, v: V)
Inserts a key-value pair into the LRUCache.
If the map did have this key present, the value is updated. The key is not updated, though; this matters for types that can be == without being identical.
If the cache size is overflowed after insertion, evict the oldest accessed entry.
Sourcepub fn remove(&mut self, k: &K) -> Option<V>
pub fn remove(&mut self, k: &K) -> Option<V>
Removes a key from the LRUCache, returning the stored value if the key was previously in the LRUCache.
The key may be any borrowed form of the LRUCache’s key type, but the ordering on the borrowed form must match the ordering on the key type.