pub struct CommonCache<K, V, R: Rng = StdRng> { /* private fields */ }
Expand description
A collection which keeps and prioritizes the most recently and commonly used items.
See the module level documentation for details.
Implementations§
Source§impl<K, V> CommonCache<K, V>
impl<K, V> CommonCache<K, V>
Sourcepub fn new(base: usize, max_size: Option<usize>) -> Self
pub fn new(base: usize, max_size: Option<usize>) -> Self
Create a new CommonCache
with a specific base and Rng
generated from some entropy.
Takes a base which must be >1
and optionally a max_size which must be >= 2
.
Sourcepub fn set_max_size(&mut self, max_size: usize)
pub fn set_max_size(&mut self, max_size: usize)
Set the max size. Note that if this might cause many elements to be removed.
PRE: max_size >= 2
Runs in linear time if max_size < self.size(), constant time otherwise.
If the new max_size is less than the previous, all indexes to this cache will be invalidated. This is because some elements might be removed randomly from the cache and we don’t want some index lookup to randomly work/fail.
Source§impl<K, V, R: Rng> CommonCache<K, V, R>
impl<K, V, R: Rng> CommonCache<K, V, R>
Sourcepub fn new_with_rng(base: usize, max_size: Option<usize>, rng: R) -> Self
pub fn new_with_rng(base: usize, max_size: Option<usize>, rng: R) -> Self
Create a new CommonCache
with a given random generator. This can be
useful if you have a psuedo random generator and want deterministic
and reproduceable behaviour.
Also takes in a base which must be >1 and optionally a max_size which must be >=2.
Source§impl<K, V, R> CommonCache<K, V, R>
impl<K, V, R> CommonCache<K, V, R>
Sourcepub fn insert(&mut self, key: K, value: V) -> Entry<'_, K, V, R>
pub fn insert(&mut self, key: K, value: V) -> Entry<'_, K, V, R>
Insert a value into the cache.
If the value is new, it will be inserted at the second lowest level. So if self.base = 2
,
then it will be inserted in the second quarter of the cache.
If the value exists in the cache already though, it will be updated with the new key and value and be moved to one level above its previous position.
IMPORTANT: All Index
to elements in this cache will be invalidated
This is because some random elements will be moved between levels in
the cache, and we don’t want indexes to be invalidated randomly. It
might cause some erroneous tests to pass undeterministicly.
§Returns
Returns the entry for the newly inserted item.
§Examples
use common_cache::CommonCache;
let mut cache = CommonCache::new(2, None);
let mut entry = cache.insert(4, "Hello");
assert!(matches!(*entry.get_value(), "Hello"));
Sourcepub fn entry<Q>(&mut self, key: &Q) -> Option<Entry<'_, K, V, R>>
pub fn entry<Q>(&mut self, key: &Q) -> Option<Entry<'_, K, V, R>>
Get a handle to an entry in the cache.
Runs in O(log[base](n))
time.
Sourcepub fn iter(&self) -> impl DoubleEndedIterator<Item = (&K, &V)> + '_
pub fn iter(&self) -> impl DoubleEndedIterator<Item = (&K, &V)> + '_
Iterate over the elements in the cache so that all items on any level will come before any item on any lower level.
This does not alter the cache in any way. So no items are promoted to higher levels in the cache when iterated over.
Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)>
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)>
Iterate over mutable references to the elements in the cache. All items on any level will come before any item on any lower level.
This does not alter the structure of the cache. So no items are promoted to higher levels in the cache when iterated over.
Sourcepub fn iter_indices(
&self,
) -> impl DoubleEndedIterator<Item = Index<K, V, R>> + '_
pub fn iter_indices( &self, ) -> impl DoubleEndedIterator<Item = Index<K, V, R>> + '_
Iterate over indices to the elements in the cache so that all items on any level will come before any item on any lower level.
This does not alter the cache in any way. So no items are promoted to higher levels in the cache when iterated over.
Sourcepub fn find_first(
&mut self,
pred: impl FnMut(&K, &V) -> bool,
) -> Option<Entry<'_, K, V, R>>
pub fn find_first( &mut self, pred: impl FnMut(&K, &V) -> bool, ) -> Option<Entry<'_, K, V, R>>
Find the first item in the cache matching a predicate.
The advantage of using this method over self.iter().find()
is that you
get an Entry
from this which can be used to promote or remove the
item with.