-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathlru_cache.cpp
More file actions
50 lines (44 loc) · 1.23 KB
/
lru_cache.cpp
File metadata and controls
50 lines (44 loc) · 1.23 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
class LRUCache{
public:
LRUCache(int capacity) {
_capacity = capacity;
}
int get(int key) {
if (cache_map.find(key) != cache_map.end()) {
// 把节点移动到最前,同时更新cache_map[key]的位置。
cache_list.splice(cache_list.begin(), cache_list, cache_map[key]);
cache_map[key] = cache_list.begin();
return cache_map[key]->_value;
}
else {
return -1;
}
}
void set(int key, int value) {
if (cache_map.find(key) == cache_map.end()) {
// 找不到key
if (cache_list.size() == _capacity) {
// 客满,删除尾部节点,访问最少。
cache_map.erase(cache_list.back()._key);
cache_list.pop_back();
}
cache_list.push_front(_Node(key, value));
cache_map[key] = cache_list.begin();
}
else {
// 更新节点值和map对应位置
cache_map[key]->_value = value;
cache_list.splice(cache_list.begin(), cache_list, cache_map[key]);
cache_map[key] = cache_list.begin();
}
}
protected:
struct _Node {
int _key;
int _value;
_Node(int key, int value) : _key(key), _value(value) {}
};
int _capacity;
list<_Node> cache_list;
map<int, list<_Node>::iterator > cache_map;
};