-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLFU_Cache.cpp
More file actions
53 lines (49 loc) · 1.33 KB
/
LFU_Cache.cpp
File metadata and controls
53 lines (49 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class LFUCache {
public:
typedef unordered_map<int, list<int>> FreqList;
typedef unordered_map<int, tuple<int,int,list<int>::iterator>> Cache;
FreqList freq;
Cache cache;
int minFreq, maxSize;
LFUCache(int capacity) {
maxSize = capacity;
}
void update(int key){
auto& f = ::get<1>(cache[key]);
auto& i = ::get<2>(cache[key]);
freq[f].erase(i);
if(freq[f].empty() && minFreq == f)
minFreq++;
f += 1;
i = freq[f].insert(freq[f].begin(), key);
}
int get(int key) {
if(!cache.count(key))
return -1;
update(key);
return ::get<0>(cache[key]);
}
void put(int key, int value) {
if(maxSize<=0)
return;
if(cache.count(key)){
update(key);
::get<0>(cache[key]) = value;
return;
}
if(cache.size()==maxSize){
cache.erase(freq[minFreq].back());
freq[minFreq].pop_back();
}
minFreq = 1;
freq[minFreq].push_front(key);
cache[key] = make_tuple(value, minFreq, freq[minFreq].begin());
return;
}
};
/**
* Your LFUCache object will be instantiated and called as such:
* LFUCache* obj = new LFUCache(capacity);
* int param_1 = obj->get(key);
* obj->put(key,value);
*/