Struct algorithm::LruKCache

source ·
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: Hash + Eq, V> LruKCache<K, V, DefaultHasher>

source

pub fn new(cap: usize) -> Self

source

pub fn with_times(cap: usize, times: usize) -> Self

source§

impl<K, V, S> LruKCache<K, V, S>

source

pub fn with_hasher( cap: usize, times: usize, hash_builder: S, ) -> LruKCache<K, V, S>

提供hash函数

source

pub fn capacity(&self) -> usize

获取当前容量

source

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);
}
source

pub fn len(&self) -> usize

获取当前长度

source

pub fn is_empty(&self) -> bool

source

pub fn reserve(&mut self, additional: usize) -> &mut Self

扩展当前容量

source

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);
}
source

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()));
}
source

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);
}
source

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);
}
source

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()))
}
source

pub fn hasher(&self) -> &S

source§

impl<K: Hash + Eq, V, S: BuildHasher> LruKCache<K, V, S>

source

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);
}
source

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);
}
source

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);
}
source

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);
}
source

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);
}
source

pub fn contains_key<Q>(&mut self, k: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

source

pub fn raw_get<Q>(&self, k: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

获取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"));
}
source

pub fn get<Q>(&mut self, k: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

获取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"));
}
source

pub fn get_key_value<Q>(&mut self, k: &Q) -> Option<(&K, &V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

获取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")));
}
source

pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

获取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())));
}
source

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"));
}
source

pub fn capture_insert(&mut self, k: K, v: V) -> Option<(K, V, bool)>

source

pub fn get_or_insert<F>(&mut self, k: K, f: F) -> &V
where F: FnOnce() -> V,

source

pub fn get_or_insert_mut<F>(&mut self, k: K, f: F) -> &mut V
where F: FnOnce() -> V,

source

pub fn remove<Q>(&mut self, k: &Q) -> Option<(K, V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

移除元素

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);
}
source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(&K, &mut V) -> bool,

根据保留当前的元素, 返回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>

source

pub fn get_or_insert_default(&mut self, k: K) -> &V

source

pub fn get_or_insert_default_mut(&mut self, k: K) -> &mut V

Trait Implementations§

source§

impl<K: Clone + Hash + Eq, V: Clone, S: Clone + BuildHasher> Clone for LruKCache<K, V, S>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<K, V, S> Debug for LruKCache<K, V, S>
where K: Ord + Debug, V: Debug,

source§

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

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

impl<K: Hash + Eq, V> Default for LruKCache<K, V, DefaultHasher>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<K, V, S> Drop for LruKCache<K, V, S>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

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)

Extends a collection with the contents of an iterator. Read more
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<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>

Creates a value from an iterator. Read more
source§

impl<'a, K, V, S> Index<&'a K> for LruKCache<K, V, S>
where K: Hash + Eq, S: BuildHasher,

§

type Output = V

The returned type after indexing.
source§

fn index(&self, index: &K) -> &V

Performs the indexing (container[index]) operation. Read more
source§

impl<'a, K, V, S> IndexMut<&'a K> for LruKCache<K, V, S>
where K: Hash + Eq, S: BuildHasher,

source§

fn index_mut(&mut self, index: &K) -> &mut V

Performs the mutable indexing (container[index]) operation. Read more
source§

impl<K: Hash + Eq, V, S: BuildHasher> IntoIterator for LruKCache<K, V, S>

§

type Item = (K, V)

The type of the elements being iterated over.
§

type IntoIter = IntoIter<K, V, S>

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

fn into_iter(self) -> IntoIter<K, V, S>

Creates an iterator from a value. Read more
source§

impl<K, V, S> PartialEq for LruKCache<K, V, S>
where K: Eq + Hash, V: PartialEq, S: BuildHasher,

source§

fn eq(&self, other: &LruKCache<K, V, S>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<K, V, S> Eq for LruKCache<K, V, S>
where K: Eq + Hash, V: PartialEq, S: BuildHasher,

source§

impl<K: Send, V: Send, S: Send> Send for LruKCache<K, V, S>

source§

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> 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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>,

§

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.