Skip to content

Commit

Permalink
fix lfu to hash
Browse files Browse the repository at this point in the history
  • Loading branch information
tickbh committed Jun 17, 2024
1 parent 9e700fb commit 281410b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
6 changes: 6 additions & 0 deletions src/cache/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ pub struct ArcCache<K, V, S> {
cap: usize,
}

impl<K: Hash + Eq, V> Default for ArcCache<K, V, DefaultHasher> {
fn default() -> Self {
ArcCache::new(100 )
}
}

impl<K: Hash + Eq, V> ArcCache<K, V, DefaultHasher> {
/// 因为存在四个数组, 所以实际的容量为这个的4倍
pub fn new(cap: usize) -> Self {
Expand Down
35 changes: 19 additions & 16 deletions src/cache/lfu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::{
ptr::NonNull,
};

use crate::{HashMap, HashSet, DefaultHasher};
use crate::{DefaultHasher, HashMap, LruCache};
use lazy_static::lazy_static;

use super::{KeyRef, KeyWrapper};
Expand Down Expand Up @@ -118,7 +118,7 @@ impl<K, V> LfuEntry<K, V> {
/// ```
pub struct LfuCache<K, V, S> {
map: HashMap<KeyRef<K>, NonNull<LfuEntry<K, V>>, S>,
times_map: HashMap<u8, HashSet<KeyRef<K>>>,
times_map: HashMap<u8, LruCache<KeyRef<K>, (), DefaultHasher>>,
cap: usize,
max_freq: u8,
min_freq: u8,
Expand Down Expand Up @@ -402,7 +402,8 @@ impl<K: Hash + Eq, V, S: BuildHasher> LfuCache<K, V, S> {
self.times_map
.entry(freq)
.or_default()
.insert((*entry).key_ref());
.reserve(1)
.insert((*entry).key_ref(), ());

self.check_reduce();
}
Expand All @@ -427,7 +428,8 @@ impl<K: Hash + Eq, V, S: BuildHasher> LfuCache<K, V, S> {
self.times_map
.entry(next)
.or_default()
.insert((*node).key_ref());
.reserve(1)
.insert((*node).key_ref(), ());
}
}
}
Expand Down Expand Up @@ -833,14 +835,15 @@ impl<K: Hash + Eq, V, S: BuildHasher> LfuCache<K, V, S> {
}
}

fn _pop_one(keys: &mut HashSet<KeyRef<K>>) -> Option<KeyRef<K>> {
let k = if let Some(k) = keys.iter().next() {
KeyRef { k: k.k }
} else {
return None;
};
keys.remove(&k);
Some(k)
fn _pop_one(keys: &mut LruCache<KeyRef<K>, (), DefaultHasher>) -> Option<KeyRef<K>> {
keys.pop().map(|(k, _)| k)
// let k = if let Some(k) = keys.iter().next() {
// KeyRef { k: k.k }
// } else {
// return None;
// };
// keys.remove(&k);
// Some(k)
}
}

Expand Down Expand Up @@ -941,7 +944,7 @@ impl<'a, K: Hash + Eq, V, S: BuildHasher> Iterator for Iter<'a, K, V, S> {
if let Some(s) = self.base.times_map.get(&i) {
if s.len() != 0 {
self.now_freq = i.saturating_sub(1);
self.now_keys = Some(s.iter().map(|s| KeyRef { k: s.k }).collect());
self.now_keys = Some(s.iter().map(|s| KeyRef { k: s.0.k }).collect());
break;
}
}
Expand Down Expand Up @@ -980,7 +983,7 @@ impl<'a, K: Hash + Eq, V, S: BuildHasher> DoubleEndedIterator for Iter<'a, K, V,
if let Some(s) = self.base.times_map.get(&i) {
if s.len() != 0 {
self.now_freq = i.saturating_sub(1);
self.now_keys = Some(s.iter().map(|s| KeyRef { k: s.k }).collect());
self.now_keys = Some(s.iter().map(|s| KeyRef { k: s.0.k }).collect());
break;
}
}
Expand Down Expand Up @@ -1035,7 +1038,7 @@ impl<'a, K: Hash + Eq, V, S: BuildHasher> Iterator for IterMut<'a, K, V, S> {
if let Some(s) = self.base.times_map.get(&i) {
if s.len() != 0 {
self.now_freq = i.saturating_sub(1);
self.now_keys = Some(s.iter().map(|s| KeyRef { k: s.k }).collect());
self.now_keys = Some(s.iter().map(|s| KeyRef { k: s.0.k }).collect());
break;
}
}
Expand Down Expand Up @@ -1074,7 +1077,7 @@ impl<'a, K: Hash + Eq, V, S: BuildHasher> DoubleEndedIterator for IterMut<'a, K,
if let Some(s) = self.base.times_map.get(&i) {
if s.len() != 0 {
self.now_freq = i.saturating_sub(1);
self.now_keys = Some(s.iter().map(|s| KeyRef { k: s.k }).collect());
self.now_keys = Some(s.iter().map(|s| KeyRef { k: s.0.k }).collect());
break;
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/cache/lru.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ pub struct LruCache<K, V, S> {
tail: *mut LruEntry<K, V>,
}

impl<K: Hash + Eq, V> Default for LruCache<K, V, DefaultHasher> {
fn default() -> Self {
LruCache::new(100 )
}
}

impl<K: Hash + Eq, V> LruCache<K, V, DefaultHasher> {
pub fn new(cap: usize) -> Self {
LruCache::with_hasher(cap, DefaultHasher::default())
Expand Down Expand Up @@ -167,8 +173,9 @@ impl<K, V, S> LruCache<K, V, S> {
}

/// 扩展当前容量
pub fn reserve(&mut self, additional: usize) {
pub fn reserve(&mut self, additional: usize) -> &mut Self {
self.cap += additional;
self
}

/// 遍历当前的所有值
Expand Down
6 changes: 6 additions & 0 deletions src/cache/lruk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ pub struct LruKCache<K, V, S> {
lru_count: usize,
}

impl<K: Hash + Eq, V> Default for LruKCache<K, V, DefaultHasher> {
fn default() -> Self {
LruKCache::new(100 )
}
}

impl<K: Hash + Eq, V> LruKCache<K, V, DefaultHasher> {
pub fn new(cap: usize) -> Self {
LruKCache::with_hasher(cap, DEFAULT_TIMESK, DefaultHasher::default())
Expand Down

0 comments on commit 281410b

Please sign in to comment.