LfuCache

Struct LfuCache 

Source
pub struct LfuCache<Key: Hash + Eq, Value> { /* private fields */ }
Expand description

A collection that if limited to a certain capacity will evict based on the least recently used value.

Implementations§

Source§

impl<Key: Hash + Eq, Value> LfuCache<Key, Value>

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a LFU cache with at least the specified capacity.

When the capacity is reached, then the least frequently used item is evicted. If there are multiple least frequently used items in this collection, the most recently added item is evicted.

let mut cache = LfuCache::with_capacity(2);

// Fill up the cache.
cache.insert("foo", 3);
cache.insert("bar", 4);

// Insert returns the evicted value, if a value was evicted.
let maybe_evicted = cache.insert("baz", 5);

// In the case of a tie, the most recently added value is evicted.
assert!(cache.get(&"bar").is_none());
assert_eq!(maybe_evicted, Some(4));

cache.get(&"baz");
// Otherwise, the least frequently value is evicted.
assert_eq!(cache.pop_lfu(), Some(3));
Source

pub fn unbounded() -> Self

Creates a LFU cache with no bound.

This effectively turns the LFU cache into a very expensive HashMap if the least frequently used item is never removed. This is useful if you want to have fine-grain control over when the LFU cache should evict. If a LFU cache was constructed using this function, users should call pop_lfu to remove the least frequently used item.

Construction of this cache will not heap allocate.

Source

pub fn set_capacity(&mut self, new_capacity: usize)

Sets the new capacity.

If the provided capacity is zero, then this will turn the cache into an unbounded cache. If the new capacity is less than the current capacity, the least frequently used items are evicted until the number of items is equal to the capacity.

If the cache becomes unbounded, then users must manually call pop_lfu to remove the least frequently used item.

If the cache was previously unbounded and is provided a non-zero capacity, then the cache now is bounded and will automatically remove the least frequently used item when the capacity is reached.

Source

pub fn insert(&mut self, key: Key, value: Value) -> Option<Value>

Inserts a key-value pair into the cache.

If the key already exists, the value is updated without updating the key and the previous value is returned. The frequency of this item is reset.

If the key did not already exist, then what is returned depends on the capacity of the cache. If an entry was evicted, the old value is returned. If the cache did not need to evict an entry or was unbounded, this returns None.

Source

pub fn get(&mut self, key: &Key) -> Option<&Value>

Gets a value and increments the internal frequency counter of that value, if it exists.

Source

pub fn get_mut(&mut self, key: &Key) -> Option<&mut Value>

Gets a mutable value and increments the internal frequency counter of that value, if it exists.

Source

pub fn remove(&mut self, key: &Key) -> Option<Value>

Removes a value from the cache by key, if it exists.

Source

pub fn entry(&mut self, key: Key) -> Entry<'_, Key, Value>

Gets the given key’s corresponding entry in the LFU cache for in-place manipulation. If the key refers to an occupied entry, that entry then is immediately considered accessed, even if no reading or writing is performed. This behavior is a limitation of the Entry API.

Source

pub fn pop_lfu(&mut self) -> Option<Value>

Evicts the least frequently used value and returns it. If the cache is empty, then this returns None. If there are multiple items that have an equal access count, then the most recently added value is evicted.

Source

pub fn pop_lfu_key_value(&mut self) -> Option<(Key, Value)>

Evicts the least frequently used key-value pair and returns it. If the cache is empty, then this returns None. If there are multiple items that have an equal access count, then the most recently added key-value pair is evicted.

Source

pub fn pop_lfu_key_value_frequency(&mut self) -> Option<(Key, Value, usize)>

Evicts the least frequently used value and returns it, the key it was inserted under, and the frequency it had. If the cache is empty, then this returns None. If there are multiple items that have an equal access count, then the most recently added key-value pair is evicted.

Source

pub fn clear(&mut self) -> LfuCacheIter<Key, Value>

Clears the cache, returning the iterator of the previous cached values.

Source

pub fn peek_lfu(&self) -> Option<&Value>

Peeks at the next value to be evicted, if there is one. This will not increment the access counter for that value.

Source

pub fn peek_lfu_key(&self) -> Option<&Key>

Peeks at the key for the next value to be evicted, if there is one. This will not increment the access counter for that value.

Source

pub const fn capacity(&self) -> Option<NonZeroUsize>

Returns the current capacity of the cache.

Source

pub const fn len(&self) -> usize

Returns the current number of items in the cache. This is a constant time operation.

Source

pub const fn is_empty(&self) -> bool

Returns if the cache contains no elements.

Source

pub const fn is_unbounded(&self) -> bool

Returns if the cache is unbounded.

Source

pub fn frequencies(&self) -> Vec<usize>

Returns the frequencies that this cache has. This is a linear time operation.

Source

pub fn shrink_to_fit(&mut self)

Sets the capacity to the amount of objects currently in the cache. If no items were in the cache, the cache becomes unbounded.

Source

pub fn keys(&self) -> impl Iterator<Item = &Key> + FusedIterator + '_

Returns an iterator over the keys of the LFU cache in any order.

Source

pub fn peek_values(&self) -> impl Iterator<Item = &Value> + FusedIterator + '_

Returns an iterator over the values of the LFU cache in any order. Note that this does not increment the count for any of the values.

Source

pub fn peek_iter( &self, ) -> impl Iterator<Item = (&Key, &Value)> + FusedIterator + '_

Returns an iterator over the keys and values of the LFU cache in any order. Note that this does not increment the count for any of the values.

Trait Implementations§

Source§

impl<Key: Hash + Eq + Debug, Value> Debug for LfuCache<Key, Value>

Available on non-tarpaulin_include only.
Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Key: Hash + Eq, Value> Extend<(Key, Value)> for LfuCache<Key, Value>

Source§

fn extend<T: IntoIterator<Item = (Key, Value)>>(&mut self, iter: T)

Inserts the items from the iterator into the cache. Note that this may evict items if the number of elements in the iterator plus the number of current items in the cache exceeds the capacity of the cache.

Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<Key: Hash + Eq, Value> FromIterator<(Key, Value)> for LfuCache<Key, Value>

Source§

fn from_iter<T: IntoIterator<Item = (Key, Value)>>(iter: T) -> Self

Constructs a LFU cache with the capacity equal to the number of elements in the iterator.

Source§

impl<Key: Hash + Eq, Value> IntoIterator for LfuCache<Key, Value>

Source§

type Item = (Key, Value)

The type of the elements being iterated over.
Source§

type IntoIter = LfuCacheIter<Key, Value>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<Key: PartialEq + Hash + Eq, Value: PartialEq> PartialEq for LfuCache<Key, Value>

Source§

fn eq(&self, other: &LfuCache<Key, Value>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<Key: Eq + Hash + Eq, Value: Eq> Eq for LfuCache<Key, Value>

Source§

impl<Key: Hash + Eq, Value> Send for LfuCache<Key, Value>

Source§

impl<Key: Hash + Eq, Value> StructuralPartialEq for LfuCache<Key, Value>

Source§

impl<Key: Hash + Eq, Value> Sync for LfuCache<Key, Value>

Auto Trait Implementations§

§

impl<Key, Value> Freeze for LfuCache<Key, Value>

§

impl<Key, Value> RefUnwindSafe for LfuCache<Key, Value>
where Key: RefUnwindSafe, Value: RefUnwindSafe,

§

impl<Key, Value> Unpin for LfuCache<Key, Value>

§

impl<Key, Value> UnwindSafe for LfuCache<Key, Value>
where Key: RefUnwindSafe, Value: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.