-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLRUCache.cpp
48 lines (43 loc) · 1.21 KB
/
LRUCache.cpp
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
class LRUCache{
public:
struct CacheEntry{
int key;
int val;
CacheEntry(int k, int v): key(k), val(v) {}
};
int n_capacity;
unordered_map<int, list<CacheEntry>::iterator> table;
list<CacheEntry> cache;
// basic requirement
// 1) based on the key, get the value and update the position on the list
// 2) based on the cacheEntry from the cache line, you can find the key to delete
// the oldest datum.
void MoveToFront(int key) {
auto entry = *table[key];
cache.erase(table[key]);
cache.push_front(entry);
table[key]=cache.begin();
}
LRUCache(int capacity) {
n_capacity = capacity;
}
int get(int key) {
if(table.find(key)==table.end()) return -1;
MoveToFront(key);
return table[key]->val;
}
void set(int key, int value) {
if(get(key)!=-1) {
table[key]->val = value;
return;
}
if(cache.size()>=n_capacity) {
int r_key = cache.back().key;
table.erase(r_key);
cache.pop_back();
}
CacheEntry en(key, value);
cache.push_front(en);
table[key] = cache.begin();
}
};