Skip to content

Instantly share code, notes, and snippets.

@jayhuang75
Last active March 21, 2022 03:32
Show Gist options
  • Select an option

  • Save jayhuang75/4ac6f4d4c86c516f3ac3c7d5e3db81ad to your computer and use it in GitHub Desktop.

Select an option

Save jayhuang75/4ac6f4d4c86c516f3ac3c7d5e3db81ad to your computer and use it in GitHub Desktop.
lfu_struct
#[derive(Debug, PartialEq, Clone)]
pub struct Entry<T> {
val: T,
count: usize,
}
impl<T> Entry<T> {
fn inc(&mut self) {
self.count += 1;
}
}
pub struct LFUCache<'a, T, const N: usize>{
by_key: HashMap<&'a str, Entry<T>>,
freq_node: HashMap<usize, LinkedHashSet<&'a str>>,
capacity: usize,
least_freq_node: usize,
}
impl <'a, T, const N: usize> Default for LFUCache<'a, T, N> {
fn default() -> Self {
if N <= 0 {
panic!("Unable to create cache: capacity is {:?}", N);
}
LFUCache {
by_key: HashMap::new(),
freq_node: HashMap::new(),
capacity: N,
least_freq_node: 0,
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment