Last active
March 21, 2022 03:32
-
-
Save jayhuang75/4ac6f4d4c86c516f3ac3c7d5e3db81ad to your computer and use it in GitHub Desktop.
lfu_struct
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
| #[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