-
Notifications
You must be signed in to change notification settings - Fork 1
/
LRUCache.cpp
140 lines (114 loc) · 3.16 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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream>
#include <sstream>
#include <unordered_map>
class Node{
public:
Node(int k, int v): key(k), val(v) {
prev = next = nullptr;
}
std::string toString() {
std::stringstream ss;
ss << "{ " << key << ": " << val << "}";
return ss.str();
}
int key;
int val;
Node* prev;
Node* next;
};
// could only store 0 and positive numbers
class LRUCache {
public:
LRUCache(int capacity): cap_(capacity) {
head_ = new Node(-1, -1);
tail_ = new Node(-1, -1);
head_->next = tail_;
tail_->prev = head_;
}
~LRUCache() {
release(head_);
}
int get(int key) {
std::cout << "try to get " << key << " = ";
auto iter = index_.find(key);
if (iter == index_.end()) {
return -1;
}
auto* node = iter->second;
attach(head_, detach(node));
return node->val;
}
void put(int key, int value) {
std::cout << "try to put " << key << " : " << value << std::endl;
auto iter = index_.find(key);
if (iter != index_.end()) {
iter->second->val = value;
attach(head_, detach(iter->second));
return;
}
Node* node = new Node(key, value);
index_[key] = attach(head_, node);
if (index_.size() > cap_) {
auto toDel = detach(tail_->prev);
index_.erase(toDel->key);
std::cout << "evict item: " << toDel->toString() << std::endl;
delete toDel;
}
}
public: // for test
size_t size() {
return index_.size();
}
void echo() {
Node* iter = head_->next;
while (iter != tail_) {
std::cout << iter->toString() << "; ";
iter = iter->next;
}
std::cout << std::endl;
}
private:
void release(Node* head) {
while(head) {
Node* curr = head;
head = head->next;
std::cout << "release " << curr->toString() << std::endl;
delete curr;
}
}
Node* detach(Node* node) {
node->next->prev = node->prev;
node->prev->next = node->next;
return node;
}
Node* attach(Node* head, Node* node) {
node->next = head->next;
node->prev = head;
head->next->prev = node;
head->next = node;
return node;
}
private:
int cap_;
Node* head_;
Node* tail_;
std::unordered_map<int, Node*> index_;
};
int main() {
LRUCache cache(3);
cache.put(1, 10);
cache.put(2, 20);
cache.put(3, 30);
cache.put(4, 40);
cache.put(5, 50);
cache.echo();
cache.put(4, 400);
cache.echo();
std::cout << cache.get(1) << std::endl;
cache.echo();
std::cout << cache.get(3) << std::endl;
cache.echo();
cache.put(2, 200);
cache.echo();
return 0;
}