Skip to content

Instantly share code, notes, and snippets.

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

  • Save jayhuang75/5af21d5bfeca1b2552bf3142c06b9ca4 to your computer and use it in GitHub Desktop.

Select an option

Save jayhuang75/5af21d5bfeca1b2552bf3142c06b9ca4 to your computer and use it in GitHub Desktop.
lfu_get_evict
// return a ref of the most-recently-used entry
pub fn get(&mut self, key: &'a str) -> Option<&T> where T: Debug{
self.update_freq_node(key);
self.by_key.get(key).map(|e| &e.val)
}
// evict the least freq used
fn evict(&mut self) {
let least_freq_node = self.freq_node.get_mut(&self.least_freq_node).unwrap();
let least_freq_entry= least_freq_node.pop_front().unwrap();
self.by_key.remove(least_freq_entry);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment