-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlru_cache.h
68 lines (58 loc) · 1.36 KB
/
lru_cache.h
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
#ifndef LRU_CACHE_H
#define LRU_CACHE_H
#include <list>
#include <unordered_map>
namespace xsf_data_structures {
class LRUCache {
public:
LRUCache(int capacity) { cap_ = capacity; }
int get(int key) {
if (key2node_.contains(key)) {
// 找到 key
makeRecently(key);
return key2node_[key]->val;
} else {
// 未找到 key
return -1;
}
}
void put(int key, int value) {
if (key2node_.contains(key)) {
// 找到 key,更新
key2node_[key]->val = value;
makeRecently(key);
} else {
// 未找到 key,加入
if (list_.size() + 1 > cap_) {
// 加入后超出容量,需逐出
popLeastRecently();
}
list_.emplace_back(key, value);
key2node_[key] = --list_.end();
}
}
private:
void makeRecently(int key) {
// 移至链表后端
list_.push_back(*(key2node_[key]));
list_.erase(key2node_[key]);
key2node_[key] = --list_.end();
}
void popLeastRecently() {
// 记录将逐出的 key
int key = list_.front().key;
// 逐出链表最前端元素
list_.pop_front();
// 删除对应映射
key2node_.erase(key);
}
struct Node {
int key;
int val;
};
int cap_;
std::list<Node> list_;
std::unordered_map<int, std::list<Node>::iterator> key2node_;
};
} // namespace xsf_data_structures
#endif // LRU_CACHE_H