-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLRU.h
244 lines (202 loc) · 4.02 KB
/
LRU.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
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
#ifndef _LRU_H_
#define _LRU_H_
#include "Terrain.h"
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <map>
#include <iostream>
#include <exception>
using namespace std;
namespace tessterrain {
class TessTerrain;
// a double linked list node
struct Node {
Node* prev;
Node* next;
string key;
TessTerrain* value;
Node(const string& keyObj, TessTerrain* valueObj): prev(0), next(0), key(keyObj) {
value = valueObj;
}
virtual ~Node() {
cleanup();
}
void cleanup() {
if (next) {
delete next;
}
next = 0;
prev = 0;
}
void unlink() {
if (next) {
next->prev = prev;
}
if (prev) {
prev->next = next;
}
next = 0;
prev = 0;
}
template<class Visitor>
void walk(Visitor& visitorFunc) {
visitorFunc(*this);
if (this->next) {
this->next->walk(visitorFunc);
}
}
};
// a doubly linked list class
struct List {
Node* head;
Node* tail;
size_t size;
List() :
head(0), tail(0), size(0) {
}
virtual ~List() {
clear();
}
void clear() {
if (head) {
delete head;
}
head = 0;
tail = 0;
size = 0;
}
Node* pop() {
if (!head) {
return 0;
} else {
Node* newHead = head->next;
head->unlink();
Node* oldHead = head;
head = newHead;
size--;
if (size == 0) {
tail = 0;
}
return oldHead;
}
}
Node* remove(Node* node) {
if (node == head) {
head = node->next;
}
if (node == tail) {
tail = node->prev;
}
node->unlink();
size--;
return node;
}
void push(Node* node) {
node->unlink();
if (!head) {
head = node;
} else if (head == tail) {
head->next = node;
node->prev = head;
} else {
tail->next = node;
node->prev = tail;
}
tail = node;
size++;
}
};
#define MapType map<string, Node*>
class LRUCache {
public:
class KeyNotFound: public std::exception {
public:
const char* what() const throw () {
return "KeyNotFound";
}
};
// -- methods
LRUCache(size_t maxSize = 64, size_t elasticity = 10) :
m_maxSize(maxSize), m_elasticity(elasticity) {
}
virtual ~LRUCache() {
}
void clear() {
m_cache.clear();
m_keys.clear();
}
void insert(const string& key, TessTerrain* value) {
MapType::iterator iter = m_cache.find(key);
if (iter != m_cache.end()) {
iter->second->value = value;
m_keys.remove(iter->second);
m_keys.push(iter->second);
} else {
Node* n = new Node(key, value);
m_cache[key] = n;
m_keys.push(n);
prune();
}
}
bool tryGet(const string& key, TessTerrain*& value) {
MapType::iterator iter = m_cache.find(key);
if (iter == m_cache.end()) {
return false;
} else {
m_keys.remove(iter->second);
m_keys.push(iter->second);
value = iter->second->value;
return true;
}
}
const TessTerrain* get(const string& key) {
MapType::iterator iter = m_cache.find(key);
if (iter == m_cache.end()) {
throw KeyNotFound();
}
m_keys.remove(iter->second);
m_keys.push(iter->second);
return iter->second->value;
}
void remove(const string& key) {
MapType::iterator iter = m_cache.find(key);
if (iter != m_cache.end()) {
m_keys.remove(iter->second);
m_cache.erase(iter);
}
}
bool contains(const string& key) {
return m_cache.find(key) != m_cache.end();
}
static void printVisitor(const Node& node) {
std::cout << "{" << node.key << ":" << node.value << "}" << std::endl;
}
void dumpDebug() const {
std::cout << "LRUCache Size : " << m_cache.size() << " (max:" << m_maxSize
<< ") (elasticity: " << m_elasticity << ")" << std::endl;
}
int size() {
return m_cache.size();
}
void dumpDebug(std::ostream& os) const {
std::cout << "LRUCache Size : " << m_cache.size() << " (max:" << m_maxSize
<< ") (elasticity: " << m_elasticity << ")" << std::endl;
for (Node* node = m_keys.head; node != NULL; node = node->next) {
printVisitor(*node);
}
}
protected:
size_t prune();
private:
MapType m_cache;
List m_keys;
size_t m_maxSize;
size_t m_elasticity;
private:
LRUCache(const LRUCache&);
const LRUCache& operator =(const LRUCache&);
};
}; //namespace gigapoint
#endif