-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLRUCache.cpp
More file actions
84 lines (70 loc) · 1.8 KB
/
LRUCache.cpp
File metadata and controls
84 lines (70 loc) · 1.8 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <unordered_map>
#include <iostream>
using namespace std;
class LRUCache {
public:
LRUCache(int capacity) {
cap = capacity;
}
int get(int key) {
auto item = cache.find(key);
if (item == cache.end())
return -1;
else {
update(item);
return item->second.first;
}
}
void put(int key, int value) {
auto item = cache.find(key);
//if find
if (item != cache.end()) {
//update lru
update(item);
cache[key] = { value, lru.begin() };
}
//if not find
else {
//if reach capacity
//evict lru
if (cache.size() == cap) {
cache.erase(lru.back());
lru.pop_back();
}
// push_front
lru.push_front(key);
cache.insert({ key, {value, lru.begin()} });
}
}
//if find, move to head
void update(unordered_map<int, pair<int, list<int>::iterator>>::iterator& it) {
lru.erase(it->second.second);
lru.push_front(it->first);
it->second.second = lru.begin();
}
private:
int cap;
// unordered_map<key, <value, key iterator> >
unordered_map<int, pair<int, list<int>::iterator>> cache;
//head is MRU tail is LRU
list<int> lru;
};
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache* obj = new LRUCache(capacity);
* int param_1 = obj->get(key);
* obj->put(key,value);
*/
int main() {
LRUCache * cache = new LRUCache(2 /* capacity */);
cache->put(1, 1);
cache->put(2, 2);
cout << cache->get(1) << endl; // returns 1
cache->put(3, 3); // evicts key 2
cache->get(2); // returns -1 (not found)
cache->put(4, 4); // evicts key 1
cout << cache->get(1) << endl; // returns -1 (not found)
cout << cache->get(3) << endl; // returns 3
cout << cache->get(4) << endl; // returns 4
cin.get();
}