Struct lru_cache::LruCache
[−]
[src]
pub struct LruCache<K: Eq + Hash, V, S: BuildHasher = RandomState> { /* fields omitted */ }
An LRU cache.
Methods
impl<K: Eq + Hash, V> LruCache<K, V>
[src]
pub fn new(capacity: usize) -> Self
[src]
Creates an empty cache that can hold at most capacity
items.
Examples
use lru_cache::LruCache; let mut cache: LruCache<i32, &str> = LruCache::new(10);
impl<K: Eq + Hash, V, S: BuildHasher> LruCache<K, V, S>
[src]
pub fn with_hasher(capacity: usize, hash_builder: S) -> Self
[src]
Creates an empty cache that can hold at most capacity
items with the given hash builder.
pub fn contains_key<Q: ?Sized>(&mut self, key: &Q) -> bool where
K: Borrow<Q>,
Q: Hash + Eq,
[src]
K: Borrow<Q>,
Q: Hash + Eq,
Checks if the map contains the given key.
Examples
use lru_cache::LruCache; let mut cache = LruCache::new(1); cache.insert(1, "a"); assert_eq!(cache.contains_key(&1), true);
pub fn insert(&mut self, k: K, v: V) -> Option<V>
[src]
Inserts a key-value pair into the cache. If the key already existed, the old value is returned.
Examples
use lru_cache::LruCache; let mut cache = LruCache::new(2); cache.insert(1, "a"); cache.insert(2, "b"); assert_eq!(cache.get_mut(&1), Some(&mut "a")); assert_eq!(cache.get_mut(&2), Some(&mut "b"));
pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V> where
K: Borrow<Q>,
Q: Hash + Eq,
[src]
K: Borrow<Q>,
Q: Hash + Eq,
Returns a mutable reference to the value corresponding to the given key in the cache, if any.
Examples
use lru_cache::LruCache; let mut cache = LruCache::new(2); cache.insert(1, "a"); cache.insert(2, "b"); cache.insert(2, "c"); cache.insert(3, "d"); assert_eq!(cache.get_mut(&1), None); assert_eq!(cache.get_mut(&2), Some(&mut "c"));
pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V> where
K: Borrow<Q>,
Q: Hash + Eq,
[src]
K: Borrow<Q>,
Q: Hash + Eq,
Removes the given key from the cache and returns its corresponding value.
Examples
use lru_cache::LruCache; let mut cache = LruCache::new(2); cache.insert(2, "a"); assert_eq!(cache.remove(&1), None); assert_eq!(cache.remove(&2), Some("a")); assert_eq!(cache.remove(&2), None); assert_eq!(cache.len(), 0);
pub fn capacity(&self) -> usize
[src]
Returns the maximum number of key-value pairs the cache can hold.
Examples
use lru_cache::LruCache; let mut cache: LruCache<i32, &str> = LruCache::new(2); assert_eq!(cache.capacity(), 2);
pub fn set_capacity(&mut self, capacity: usize)
[src]
Sets the number of key-value pairs the cache can hold. Removes least-recently-used key-value pairs if necessary.
Examples
use lru_cache::LruCache; let mut cache = LruCache::new(2); cache.insert(1, "a"); cache.insert(2, "b"); cache.insert(3, "c"); assert_eq!(cache.get_mut(&1), None); assert_eq!(cache.get_mut(&2), Some(&mut "b")); assert_eq!(cache.get_mut(&3), Some(&mut "c")); cache.set_capacity(3); cache.insert(1, "a"); cache.insert(2, "b"); assert_eq!(cache.get_mut(&1), Some(&mut "a")); assert_eq!(cache.get_mut(&2), Some(&mut "b")); assert_eq!(cache.get_mut(&3), Some(&mut "c")); cache.set_capacity(1); assert_eq!(cache.get_mut(&1), None); assert_eq!(cache.get_mut(&2), None); assert_eq!(cache.get_mut(&3), Some(&mut "c"));
pub fn remove_lru(&mut self) -> Option<(K, V)>
[src]
Removes and returns the least recently used key-value pair as a tuple.
Examples
use lru_cache::LruCache; let mut cache = LruCache::new(2); cache.insert(1, "a"); cache.insert(2, "b"); assert_eq!(cache.remove_lru(), Some((1, "a"))); assert_eq!(cache.len(), 1);
pub fn len(&self) -> usize
[src]
Returns the number of key-value pairs in the cache.
pub fn is_empty(&self) -> bool
[src]
Returns true
if the cache contains no key-value pairs.
pub fn clear(&mut self)
[src]
Removes all key-value pairs from the cache.
ⓘImportant traits for Iter<'a, K, V>pub fn iter(&self) -> Iter<K, V>
[src]
Returns an iterator over the cache's key-value pairs in least- to most-recently-used order.
Accessing the cache through the iterator does not affect the cache's LRU state.
Examples
use lru_cache::LruCache; let mut cache = LruCache::new(2); cache.insert(1, 10); cache.insert(2, 20); cache.insert(3, 30); let kvs: Vec<_> = cache.iter().collect(); assert_eq!(kvs, [(&2, &20), (&3, &30)]);
ⓘImportant traits for IterMut<'a, K, V>pub fn iter_mut(&mut self) -> IterMut<K, V>
[src]
Returns an iterator over the cache's key-value pairs in least- to most-recently-used order, with mutable references to the values.
Accessing the cache through the iterator does not affect the cache's LRU state.
Examples
use lru_cache::LruCache; let mut cache = LruCache::new(2); cache.insert(1, 10); cache.insert(2, 20); cache.insert(3, 30); let mut n = 2; for (k, v) in cache.iter_mut() { assert_eq!(*k, n); assert_eq!(*v, n * 10); *v *= 10; n += 1; } assert_eq!(n, 4); assert_eq!(cache.get_mut(&2), Some(&mut 200)); assert_eq!(cache.get_mut(&3), Some(&mut 300));
Trait Implementations
impl<K: Clone + Eq + Hash, V: Clone, S: Clone + BuildHasher> Clone for LruCache<K, V, S>
[src]
fn clone(&self) -> LruCache<K, V, S>
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<K: Eq + Hash, V, S: BuildHasher> Extend<(K, V)> for LruCache<K, V, S>
[src]
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
[src]
Extends a collection with the contents of an iterator. Read more
impl<K: Debug + Eq + Hash, V: Debug, S: BuildHasher> Debug for LruCache<K, V, S>
[src]
fn fmt(&self, f: &mut Formatter) -> Result
[src]
Formats the value using the given formatter. Read more
impl<K: Eq + Hash, V, S: BuildHasher> IntoIterator for LruCache<K, V, S>
[src]
type Item = (K, V)
The type of the elements being iterated over.
type IntoIter = IntoIter<K, V>
Which kind of iterator are we turning this into?
ⓘImportant traits for IntoIter<K, V>fn into_iter(self) -> IntoIter<K, V>
[src]
Creates an iterator from a value. Read more
impl<'a, K: Eq + Hash, V, S: BuildHasher> IntoIterator for &'a LruCache<K, V, S>
[src]
type Item = (&'a K, &'a V)
The type of the elements being iterated over.
type IntoIter = Iter<'a, K, V>
Which kind of iterator are we turning this into?
ⓘImportant traits for Iter<'a, K, V>fn into_iter(self) -> Iter<'a, K, V>
[src]
Creates an iterator from a value. Read more