/// Create a new unbounded `LruCache` that does not automatically evict entries. /// /// A simple convenience method that is equivalent to `LruCache::new(usize::MAX)` #[inline] pubfn new_unbounded() -> Self {
LruCache::new(usize::MAX)
}
}
/// Insert a new value into the `LruCache`. /// /// If necessary, will remove the value at the front of the LRU list to make room. #[inline] pubfn insert(&mutself, k: K, v: V) -> Option<V> { let old_val = self.map.insert(k, v); ifself.len() > self.capacity() { self.remove_lru();
}
old_val
}
/// Get the value for the given key, *without* marking the value as recently used and moving it /// to the back of the LRU list. #[inline] pubfn peek<Q>(&self, k: &Q) -> Option<&V> where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{ self.map.get(k)
}
/// Get the value for the given key mutably, *without* marking the value as recently used and /// moving it to the back of the LRU list. #[inline] pubfn peek_mut<Q>(&mutself, k: &Q) -> Option<&mut V> where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{ self.map.get_mut(k)
}
/// Retrieve the given key, marking it as recently used and moving it to the back of the LRU /// list. #[inline] pubfn get<Q>(&mutself, k: &Q) -> Option<&V> where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{ self.get_mut(k).map(|v| &*v)
}
/// Retrieve the given key, marking it as recently used and moving it to the back of the LRU /// list. #[inline] pubfn get_mut<Q>(&mutself, k: &Q) -> Option<&mut V> where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{ matchself.map.raw_entry_mut().from_key(k) {
linked_hash_map::RawEntryMut::Occupied(mut occupied) => {
occupied.to_back();
Some(occupied.into_mut())
}
linked_hash_map::RawEntryMut::Vacant(_) => None,
}
}
/// If the returned entry is vacant, it will always have room to insert a single value. By /// using the entry API, you can exceed the configured capacity by 1. /// /// The returned entry is not automatically moved to the back of the LRU list. By calling /// `Entry::to_back` / `Entry::to_front` you can manually control the position of this entry in /// the LRU list. #[inline] pubfn entry(&mutself, key: K) -> Entry<'_, K, V, S> { ifself.len() > self.capacity() { self.remove_lru();
} self.map.entry(key)
}
/// The constructed raw entry is never automatically moved to the back of the LRU list. By /// calling `Entry::to_back` / `Entry::to_front` you can manually control the position of this /// entry in the LRU list. #[inline] pubfn raw_entry(&self) -> RawEntryBuilder<'_, K, V, S> { self.map.raw_entry()
}
/// If the constructed raw entry is vacant, it will always have room to insert a single value. /// By using the raw entry API, you can exceed the configured capacity by 1. /// /// The constructed raw entry is never automatically moved to the back of the LRU list. By /// calling `Entry::to_back` / `Entry::to_front` you can manually control the position of this /// entry in the LRU list. #[inline] pubfn raw_entry_mut(&mutself) -> RawEntryBuilderMut<'_, K, V, S> { ifself.len() > self.capacity() { self.remove_lru();
} self.map.raw_entry_mut()
}
/// Set the new cache capacity for the `LruCache`. /// /// If there are more entries in the `LruCache` than the new capacity will allow, they are /// removed. #[inline] pubfn set_capacity(&mutself, capacity: usize) { for _ in capacity..self.len() { self.remove_lru();
} self.max_size = capacity;
}
/// Remove the least recently used entry and return it. /// /// If the `LruCache` is empty this will return None. #[inline] pubfn remove_lru(&mutself) -> Option<(K, V)> { self.map.pop_front()
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.