-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
LRUCache.cpp
253 lines (207 loc) · 5.98 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Source : https://oj.leetcode.com/problems/lru-cache/
// Author : Hao Chen
// Date : 2014-10-12
/**********************************************************************************
*
* Design and implement a data structure for Least Recently Used (LRU) cache.
* It should support the following operations: get and set.
*
* get(key) - Get the value (will always be positive) of the key if the key exists
* in the cache, otherwise return -1.
*
* set(key, value) - Set or insert the value if the key is not already present.
* When the cache reached its capacity, it should invalidate
* the least recently used item before inserting a new item.
*
**********************************************************************************/
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <map>
using namespace std;
// The idea here is quite simple:
// 1) A Map to index the key. O(1) key search time-complexity.
// 2) A List to sort the cache data by accessed time.
//
// Considering there are too many insert/delete opreations for the List,
// The ouble linked list is the good data structure to performance it.
class Node {
public:
int key;
int value;
Node *next, *prev;
Node(int k, int v) { key=k; value=v; next = prev = NULL; }
//Node(int k, int v, Node* n=NULL, Node* p=NULL): key(k), value(v), next(n), prev(p) {}
};
// the following double linked list seems a bit commplicated.
class DoubleLinkedList {
private:
Node *pHead, *pTail;
int size;
public:
DoubleLinkedList(){
pHead = pTail = NULL;
size = 0;
}
~DoubleLinkedList() {
while(pHead!=NULL){
Node*p = pHead;
pHead = pHead->next;
delete p;
}
}
int Size() const {
return size;
}
Node* NewAtBegin(int key, int value) {
Node *n = new Node(key, value);
return AddAtBegin(n);
}
Node* NewAtEnd(int key, int value) {
Node *n = new Node(key, value);
return AddAtEnd(n);
}
Node* AddAtBegin(Node* n){
size++;
if (pHead==NULL) {
pHead = pTail = n;
return n;
}
n->next = pHead;
n->prev = NULL;
pHead->prev = n;
pHead = n;
return n;
}
Node* AddAtEnd(Node* n) {
size++;
if (pHead==NULL) {
pHead = pTail = n;
return n;
}
pTail->next = n;
n->prev = pTail;
n->next = NULL;
pTail = n;
}
void Unlink(Node* n){
Node* before = n->prev;
Node* after = n->next;
if (before){
before->next = after;
}
if (after){
after->prev = before;
}
if(pHead == n){
pHead = pHead->next;
}else if(pTail == n) {
pTail = pTail->prev;
}
size--;
}
void Delete(Node* n){
Unlink(n);
delete n;
}
void TakeToBegin(Node* n){
Unlink(n);
AddAtBegin(n);
}
Node* GetTailNode() {
return pTail;
}
void DeleteLast() {
Delete(pTail);
}
void Print(){
Node* p = pHead;
while(p!=NULL) {
cout << "(" << p->key << "," << p->value << ") ";
p = p->next;
}
cout << endl;
}
};
class LRUCache{
private:
//cacheList - store the date
DoubleLinkedList cacheList;
//cacheMap - index the date for searching
map<int, Node*> cacheMap;
//the max capcity of cache
int capacity;
public:
LRUCache(int capacity) {
this->capacity = capacity;
}
void print(){
cacheList.Print();
}
int get(int key) {
// The accessed node must be up-to-time -- take to the front
if (cacheMap.find(key) != cacheMap.end() ){
cacheList.TakeToBegin(cacheMap[key]);
return cacheMap[key]->value;
}
return -1;
}
void set(int key, int value) {
// key found, update the data, and take to the front
if (cacheMap.find(key) != cacheMap.end() ){
Node *p = cacheMap[key];
p->value = value;
cacheList.TakeToBegin(cacheMap[key]);
}else{
// key not found, new a node to store data
cacheMap[key] = cacheList.NewAtBegin(key, value);
// if the capacity exceed, remove the last one.
if( cacheList.Size() > capacity) {
int key = cacheList.GetTailNode()->key;
cacheMap.erase(key);
cacheList.DeleteLast();
}
}
}
};
int main(int argc, char** argv)
{
/*
LRUCache c(2);
c.set(2,1);
c.print();
c.set(2,2);
c.print();
c.get(2);
c.print();
c.set(1,1);
c.print();
c.set(4,1);
c.print();
c.get(2);
c.print();
cout << "---------" << endl;
*/
srand(time(0));
int capacity = 5;
int test_loop_times = 10;
if (argc>1){
capacity = atoi(argv[1]);
}
if (argc>2){
test_loop_times = atoi(argv[1]);
}
LRUCache cache(capacity);
int v;
for(int i=0; i<test_loop_times; i++) {
v = i;//rand() % capacity;
cout << "set " << v << ": ";
cache.set(v, v);
cache.print();
v = rand() % capacity;
cout << "get " << v << ": " << cache.get(v);
cache.print();
cout << endl;
}
return 0;
}