pub struct LruKCache<K, V, S> { /* private fields */ }
Expand description
一个 LRU-K 缓存的实现, 接口参照Hashmap保持一致 当一个元素访问次数达到K次后, 将移入到新列表中, 防止被析构 设置容量之后将最大保持该容量大小的数据 后进的数据将会淘汰最久没有被访问的数据
§Examples
use algorithm::LruKCache;
fn main() {
let mut lru = LruKCache::with_times(3, 3);
lru.insert("this", "lru");
for _ in 0..3 {
let _ = lru.get("this");
}
lru.insert("hello", "algorithm");
lru.insert("auth", "tickbh");
assert!(lru.len() == 3);
lru.insert("auth1", "tickbh");
assert_eq!(lru.get("this"), Some(&"lru"));
assert_eq!(lru.get("hello"), None);
assert!(lru.len() == 3);
}
Implementations§
source§impl<K, V, S> LruKCache<K, V, S>
impl<K, V, S> LruKCache<K, V, S>
sourcepub fn with_hasher(
cap: usize,
times: usize,
hash_builder: S,
) -> LruKCache<K, V, S>
pub fn with_hasher( cap: usize, times: usize, hash_builder: S, ) -> LruKCache<K, V, S>
提供hash函数
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
清理当前数据
§Examples
use algorithm::LruKCache;
fn main() {
let mut lru = LruKCache::new(3);
lru.insert("now", "ok");
lru.insert("hello", "algorithm");
lru.insert("this", "lru");
assert!(lru.len() == 3);
lru.clear();
assert!(lru.len() == 0);
}
pub fn is_empty(&self) -> bool
sourcepub fn iter(&self) -> Iter<'_, K, V>
pub fn iter(&self) -> Iter<'_, K, V>
遍历当前的所有值
use algorithm::LruKCache;
fn main() {
let mut lru = LruKCache::new(3);
lru.insert("this", "lru");
for _ in 0..3 {
let _ = lru.get("this");
}
lru.insert("hello", "algorithm");
for (k, v) in lru.iter() {
assert!(k == &"hello" || k == &"this");
assert!(v == &"algorithm" || v == &"lru");
}
assert!(lru.len() == 2);
}
sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V>
pub fn iter_mut(&mut self) -> IterMut<'_, K, V>
遍历当前的所有值, 可变
use algorithm::LruKCache;
fn main() {
let mut lru = LruKCache::new(3);
lru.insert("hello", "algorithm".to_string());
lru.insert("this", "lru".to_string());
for (k, v) in lru.iter_mut() {
v.push_str(" ok");
}
assert!(lru.len() == 2);
assert!(lru.get(&"this") == Some(&"lru ok".to_string()));
assert!(lru.get(&"hello") == Some(&"algorithm ok".to_string()));
}
sourcepub fn keys(&self) -> Keys<'_, K, V>
pub fn keys(&self) -> Keys<'_, K, V>
遍历当前的key值
use algorithm::LruKCache;
fn main() {
let mut lru = LruKCache::with_times(3, 3);
lru.insert("this", "lru");
for _ in 0..3 {
let _ = lru.get("this");
}
lru.insert("hello", "algorithm");
let mut keys = lru.keys();
assert!(keys.next()==Some(&"this"));
assert!(keys.next()==Some(&"hello"));
assert!(keys.next() == None);
}
sourcepub fn values(&self) -> Values<'_, K, V>
pub fn values(&self) -> Values<'_, K, V>
遍历当前的valus值
use algorithm::LruKCache;
fn main() {
let mut lru = LruKCache::with_times(3, 3);
lru.insert("this", "lru");
for _ in 0..3 {
let _ = lru.get("this");
}
lru.insert("hello", "algorithm");
let mut values = lru.values();
assert!(values.next()==Some(&"lru"));
assert!(values.next()==Some(&"algorithm"));
assert!(values.next() == None);
}
sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V>
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V>
遍历当前的valus值
use algorithm::LruKCache;
fn main() {
let mut lru = LruKCache::new(3);
lru.insert("hello", "algorithm".to_string());
lru.insert("this", "lru".to_string());
{
let mut values = lru.values_mut();
values.next().unwrap().push_str(" ok");
values.next().unwrap().push_str(" ok");
assert!(values.next() == None);
}
assert_eq!(lru.get(&"this"), Some(&"lru ok".to_string()))
}
pub fn hasher(&self) -> &S
source§impl<K: Hash + Eq, V, S: BuildHasher> LruKCache<K, V, S>
impl<K: Hash + Eq, V, S: BuildHasher> LruKCache<K, V, S>
sourcepub fn drain(&mut self) -> Drain<'_, K, V, S>
pub fn drain(&mut self) -> Drain<'_, K, V, S>
排出当前数据
use algorithm::LruKCache;
fn main() {
let mut lru = LruKCache::new(3);
lru.insert("hello", "algorithm");
lru.insert("this", "lru");
{
let mut drain = lru.drain();
assert!(drain.next()==Some(("hello", "algorithm")));
}
assert!(lru.len() == 0);
}
sourcepub fn pop_usual(&mut self) -> Option<(K, V)>
pub fn pop_usual(&mut self) -> Option<(K, V)>
弹出栈顶上的数据, 最常使用的数据
use algorithm::LruKCache;
fn main() {
let mut lru = LruKCache::new(3);
lru.insert("hello", "algorithm");
lru.insert("this", "lru");
assert!(lru.pop_usual()==Some(("this", "lru")));
assert!(lru.len() == 1);
}
sourcepub fn pop_unusual(&mut self) -> Option<(K, V)>
pub fn pop_unusual(&mut self) -> Option<(K, V)>
弹出栈尾上的数据, 最不常使用的数据
use algorithm::LruKCache;
fn main() {
let mut lru = LruKCache::new(3);
lru.insert("hello", "algorithm");
lru.insert("this", "lru");
assert!(lru.pop_unusual()==Some(("hello", "algorithm")));
assert!(lru.len() == 1);
}
sourcepub fn peek_usual(&mut self) -> Option<(&K, &V)>
pub fn peek_usual(&mut self) -> Option<(&K, &V)>
取出栈顶上的数据, 最常使用的数据
use algorithm::LruKCache;
fn main() {
let mut lru = LruKCache::new(3);
lru.insert("hello", "algorithm");
lru.insert("this", "lru");
assert!(lru.peek_usual()==Some((&"this", &"lru")));
assert!(lru.len() == 2);
}
sourcepub fn peek_unusual(&mut self) -> Option<(&K, &V)>
pub fn peek_unusual(&mut self) -> Option<(&K, &V)>
取出栈尾上的数据, 最不常使用的数据
use algorithm::LruKCache;
fn main() {
let mut lru = LruKCache::new(3);
lru.insert("hello", "algorithm");
lru.insert("this", "lru");
assert!(lru.peek_unusual()==Some((&"hello", &"algorithm")));
assert!(lru.len() == 2);
}
pub fn contains_key<Q>(&mut self, k: &Q) -> bool
sourcepub fn raw_get<Q>(&self, k: &Q) -> Option<&V>
pub fn raw_get<Q>(&self, k: &Q) -> Option<&V>
获取key值相对应的value值, 根据hash判定
use algorithm::LruKCache;
fn main() {
let mut lru = LruKCache::new(3);
lru.insert("hello", "algorithm");
lru.insert("this", "lru");
assert!(lru.raw_get(&"this") == Some(&"lru"));
}
sourcepub fn get<Q>(&mut self, k: &Q) -> Option<&V>
pub fn get<Q>(&mut self, k: &Q) -> Option<&V>
获取key值相对应的value值, 根据hash判定
use algorithm::LruKCache;
fn main() {
let mut lru = LruKCache::new(3);
lru.insert("hello", "algorithm");
lru.insert("this", "lru");
assert!(lru.get(&"this") == Some(&"lru"));
}
sourcepub fn get_key_value<Q>(&mut self, k: &Q) -> Option<(&K, &V)>
pub fn get_key_value<Q>(&mut self, k: &Q) -> Option<(&K, &V)>
获取key值相对应的key和value值, 根据hash判定
use algorithm::LruKCache;
fn main() {
let mut lru = LruKCache::new(3);
lru.insert("hello", "algorithm");
lru.insert("this", "lru");
assert!(lru.get_key_value(&"this") == Some((&"this", &"lru")));
}
sourcepub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
获取key值相对应的value值, 根据hash判定, 可编辑被改变
use algorithm::LruKCache;
fn main() {
let mut lru = LruKCache::new(3);
lru.insert("hello", "algorithm".to_string());
lru.insert("this", "lru".to_string());
lru.get_mut(&"this").unwrap().insert_str(3, " good");
assert!(lru.get_key_value(&"this") == Some((&"this", &"lru good".to_string())));
}
sourcepub fn insert(&mut self, k: K, v: V) -> Option<V>
pub fn insert(&mut self, k: K, v: V) -> Option<V>
插入值, 如果值重复将返回原来的数据
use algorithm::LruKCache;
fn main() {
let mut lru = LruKCache::new(3);
lru.insert("hello", "algorithm");
lru.insert("this", "lru");
assert!(lru.insert("this", "lru good") == Some(&"lru"));
}
pub fn capture_insert(&mut self, k: K, v: V) -> Option<(K, V, bool)>
pub fn get_or_insert<F>(&mut self, k: K, f: F) -> &Vwhere
F: FnOnce() -> V,
pub fn get_or_insert_mut<F>(&mut self, k: K, f: F) -> &mut Vwhere
F: FnOnce() -> V,
sourcepub fn remove<Q>(&mut self, k: &Q) -> Option<(K, V)>
pub fn remove<Q>(&mut self, k: &Q) -> Option<(K, V)>
移除元素
use algorithm::LruKCache;
fn main() {
let mut lru = LruKCache::new(3);
lru.insert("hello", "algorithm");
lru.insert("this", "lru");
assert!(lru.remove("this") == Some(("this", "lru")));
assert!(lru.len() == 1);
}
sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
根据保留当前的元素, 返回false则表示抛弃元素
use algorithm::LruKCache;
fn main() {
let mut lru = LruKCache::new(3);
lru.insert("hello", "algorithm");
lru.insert("this", "lru");
lru.insert("year", "2024");
lru.retain(|_, v| *v == "2024" || *v == "lru");
assert!(lru.len() == 2);
assert!(lru.get("this") == Some(&"lru"));
}
source§impl<K: Hash + Eq, V: Default, S: BuildHasher> LruKCache<K, V, S>
impl<K: Hash + Eq, V: Default, S: BuildHasher> LruKCache<K, V, S>
pub fn get_or_insert_default(&mut self, k: K) -> &V
pub fn get_or_insert_default_mut(&mut self, k: K) -> &mut V
Trait Implementations§
source§impl<K: Hash + Eq, V> Extend<(K, V)> for LruKCache<K, V, DefaultHasher>
impl<K: Hash + Eq, V> Extend<(K, V)> for LruKCache<K, V, DefaultHasher>
source§fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
Extends a collection with the contents of an iterator. Read more
source§fn extend_one(&mut self, item: A)
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)
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<K: Hash + Eq, V> FromIterator<(K, V)> for LruKCache<K, V, DefaultHasher>
impl<K: Hash + Eq, V> FromIterator<(K, V)> for LruKCache<K, V, DefaultHasher>
source§fn from_iter<T: IntoIterator<Item = (K, V)>>(
iter: T,
) -> LruKCache<K, V, DefaultHasher>
fn from_iter<T: IntoIterator<Item = (K, V)>>( iter: T, ) -> LruKCache<K, V, DefaultHasher>
Creates a value from an iterator. Read more
source§impl<K: Hash + Eq, V, S: BuildHasher> IntoIterator for LruKCache<K, V, S>
impl<K: Hash + Eq, V, S: BuildHasher> IntoIterator for LruKCache<K, V, S>
source§impl<K, V, S> PartialEq for LruKCache<K, V, S>
impl<K, V, S> PartialEq for LruKCache<K, V, S>
impl<K, V, S> Eq for LruKCache<K, V, S>
impl<K: Send, V: Send, S: Send> Send for LruKCache<K, V, S>
impl<K: Sync, V: Sync, S: Sync> Sync for LruKCache<K, V, S>
Auto Trait Implementations§
impl<K, V, S> Freeze for LruKCache<K, V, S>where
S: Freeze,
impl<K, V, S> RefUnwindSafe for LruKCache<K, V, S>
impl<K, V, S> Unpin for LruKCache<K, V, S>where
S: Unpin,
impl<K, V, S> UnwindSafe for LruKCache<K, V, S>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more