Created
March 21, 2022 03:36
-
-
Save jayhuang75/36d202bfa428a2403a57bea7560cb1ee to your computer and use it in GitHub Desktop.
lfu_update
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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