Skip to content

Instantly share code, notes, and snippets.

@jayhuang75
Created March 21, 2022 03:36
Show Gist options
  • Select an option

  • Save jayhuang75/36d202bfa428a2403a57bea7560cb1ee to your computer and use it in GitHub Desktop.

Select an option

Save jayhuang75/36d202bfa428a2403a57bea7560cb1ee to your computer and use it in GitHub Desktop.
lfu_update
fn update_freq_node(&mut self, key: &'a str) {
let entry = self.by_key.get_mut(key).unwrap();
// get the freq node
let freq_node = self.freq_node.get_mut(&entry.count).unwrap();
// once it have been access
// we need to remove the key in this current node
freq_node.remove(key);
if entry.count == self.least_freq_node && freq_node.is_empty() {
self.least_freq_node += 1;
}
// add to the next freq node
self.freq_node.entry(entry.count+1).or_default().insert(key);
// increase the access account
entry.inc();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment