-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathlru-cache.cpp
53 lines (50 loc) · 1.6 KB
/
lru-cache.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
49
50
51
52
53
struct node {
int value, count;
node(int v, int c) : value(v), count(c) {}
};
class LRUCache{//a way to improve effeiciency is to use list rather than map to store count numbers. and use hashtable
public:
int count, cap, sz;
map<int, int> ck;
map<int, node> kv;
LRUCache(int capacity) : count(0x80000000), cap(capacity), sz(0){
}
int get(int key) {
if(cap <= 0)
return -1;
map<int, node>::iterator p = kv.find(key);
if(p != kv.end()) {
++count;
ck.erase(p->second.count);
p->second.count = count;
ck.insert(pair<int, int>(count, key));
return p->second.value;
} else {
return -1;
}
}
void set(int key, int value) {
if(cap <= 0)
return;
++count;
map<int, node>::iterator p = kv.find(key);
if(p != kv.end()) {
ck.erase(p->second.count);
p->second.count = count;
ck.insert(pair<int, int>(count, key));
p->second.value = value;
} else {
if(sz < cap) {
kv.insert(pair<int, node>(key, node(value, count)));
ck.insert(pair<int, int>(count ,key));
++sz;
} else {
int tmpKey = ck.begin()->second;
kv.erase(tmpKey);
ck.erase(ck.begin());
kv.insert(pair<int, node>(key, node(value, count)));
ck.insert(pair<int, int>(count ,key));
}
}
}
};