-
Notifications
You must be signed in to change notification settings - Fork 2
/
lru.hpp
371 lines (284 loc) · 8.02 KB
/
lru.hpp
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* Implementation of an LRU cache with a maximum size.
*
* See http://code.google.com/p/lru-cache-cpp/ for usage and limitations.
*
* Licensed under the GNU LGPL: http://www.gnu.org/copyleft/lesser.html
*
* Pierre-Luc Brunelle, 2011
*
*/
#include <hashtable.h>
#include <sstream>
#include <cassert>
namespace {
//-------------------------------------------------------------
// Bucket
//-------------------------------------------------------------
template<class K, class V>
struct LRUCacheH4Value
{
typedef std::pair<const K, LRUCacheH4Value<K, V> > Val;
LRUCacheH4Value()
: _v(), _older(NULL), _newer(NULL) { }
LRUCacheH4Value(const V & v, Val * older, Val * newer)
: _v(v), _older(older), _newer(newer) { }
V _v;
Val * _older;
Val * _newer;
};
//-------------------------------------------------------------
// Const Iterator
//-------------------------------------------------------------
template<class K, class V>
class LRUCacheH4ConstIterator
{
public:
typedef std::pair<const K, LRUCacheH4Value<K, V> > Val;
typedef LRUCacheH4ConstIterator<K, V> const_iterator;
typedef Val & reference;
typedef Val * pointer;
enum DIRECTION {
MRU_TO_LRU = 0,
LRU_TO_MRU
};
LRUCacheH4ConstIterator(const Val * ptr = NULL, DIRECTION dir = MRU_TO_LRU);
const_iterator & operator++();
const_iterator operator++(int);
bool operator==(const const_iterator & other);
bool operator!=(const const_iterator & other);
const K & key() const;
const V & value() const;
private:
const Val * _ptr;
DIRECTION _dir;
};
template<class K, class V>
LRUCacheH4ConstIterator<K, V>::LRUCacheH4ConstIterator(
const LRUCacheH4ConstIterator<K, V>::Val * ptr,
LRUCacheH4ConstIterator<K, V>::DIRECTION dir)
: _ptr(ptr), _dir(dir)
{
}
template<class K, class V>
LRUCacheH4ConstIterator<K, V> & LRUCacheH4ConstIterator<K, V>::operator++()
{
assert(_ptr);
_ptr = (_dir == LRUCacheH4ConstIterator<K, V>::MRU_TO_LRU ? _ptr->second._older : _ptr->second._newer);
return *this;
}
template<class K, class V>
LRUCacheH4ConstIterator<K, V> LRUCacheH4ConstIterator<K, V>::operator++(int)
{
const_iterator ret = *this;
++*this;
return ret;
}
template<class K, class V>
bool LRUCacheH4ConstIterator<K, V>::operator==(const const_iterator & other)
{
return _ptr == other._ptr;
}
template<class K, class V>
bool LRUCacheH4ConstIterator<K, V>::operator!=(const const_iterator & other)
{
return _ptr != other._ptr;
}
template<class K, class V>
const K & LRUCacheH4ConstIterator<K, V>::key() const
{
assert(_ptr);
return _ptr->first;
}
template<class K, class V>
const V & LRUCacheH4ConstIterator<K, V>::value() const
{
assert(_ptr);
return _ptr->second._v;
}
} // file scope
namespace plb {
//-------------------------------------------------------------
// LRU Cache
//-------------------------------------------------------------
template<class K, class V>
class LRUCacheH4
{
public:
typedef LRUCacheH4ConstIterator<K, V> const_iterator;
public:
LRUCacheH4(int maxsize); // Pre-condition: maxsize >= 1
LRUCacheH4(const LRUCacheH4 & other);
V & operator[](const K & key);
void insert(const K & key, const V & value);
int size() const;
int maxsize() const;
bool empty() const;
const_iterator find(const K & key); // updates the MRU
const_iterator find(const K & key) const; // does not update the MRU
const_iterator mru_begin() const; // from MRU to LRU
const_iterator lru_begin() const; // from LRU to MRU
const_iterator end() const;
void dump_mru_to_lru(std::ostream & os) const;
private:
typedef std::pair<const K, LRUCacheH4Value<K, V> > Val;
typedef __gnu_cxx::hashtable<Val, K, __gnu_cxx::hash<K>, std::_Select1st<Val>, std::equal_to<K> > MAP_TYPE;
private:
Val * _update_or_insert(const K & key);
Val * _update(typename MAP_TYPE::iterator it);
Val * _insert(const K & key);
private:
MAP_TYPE _map;
Val * _mru;
Val * _lru;
int _maxsize;
};
// Reserve enough space to avoid resizing later on and thus invalidate iterators
template<class K, class V>
LRUCacheH4<K, V>::LRUCacheH4(int maxsize)
: _map(maxsize, __gnu_cxx::hash<K>(), std::equal_to<K>()),
_mru(NULL),
_lru(NULL),
_maxsize(maxsize)
{
if (_maxsize <= 0)
throw "LRUCacheH4: expecting cache size >= 1";
}
template<class K, class V>
LRUCacheH4<K, V>::LRUCacheH4(const LRUCacheH4<K, V> & other)
: _map(other._map.bucket_count(), __gnu_cxx::hash<K>(), std::equal_to<K>()),
_maxsize(other._maxsize),
_mru(NULL),
_lru(NULL)
{
for (const_iterator it = other.lru_begin(); it != other.end(); ++it)
this->insert(it.key(), it.value());
}
template<class K, class V>
V & LRUCacheH4<K, V>::operator[](const K & key)
{
return _update_or_insert(key)->second._v;
}
template<class K, class V>
void LRUCacheH4<K, V>::insert(const K & key, const V & value)
{
_update_or_insert(key)->second._v = value;
}
template<class K, class V>
int LRUCacheH4<K, V>::size() const
{
return _map.size();
}
template<class K, class V>
int LRUCacheH4<K, V>::maxsize() const
{
return _maxsize;
}
template<class K, class V>
bool LRUCacheH4<K, V>::empty() const
{
return size() > 0;
}
// updates MRU
template<class K, class V>
typename LRUCacheH4<K, V>::const_iterator LRUCacheH4<K, V>::find(const K & key)
{
typename MAP_TYPE::iterator it = _map.find(key);
if (it != _map.end())
return const_iterator(_update(it), const_iterator::MRU_TO_LRU);
else
return end();
}
// does not update MRU
template<class K, class V>
typename LRUCacheH4<K, V>::const_iterator LRUCacheH4<K, V>::find(const K & key) const
{
typename MAP_TYPE::iterator it = _map.find(key);
if (it != _map.end())
return const_iterator(&*it, const_iterator::MRU_TO_LRU);
else
return end();
}
template<class K, class V>
void LRUCacheH4<K, V>::dump_mru_to_lru(std::ostream & os) const
{
os << "LRUCacheH4(" << size() << "/" << maxsize() << "): MRU --> LRU: " << std::endl;
for (const_iterator it = mru_begin(); it != end(); ++it)
os << it.key() << ": " << it.value() << std::endl;
}
template<class K, class V>
typename LRUCacheH4<K, V>::const_iterator LRUCacheH4<K, V>::mru_begin() const
{
return const_iterator(_mru, const_iterator::MRU_TO_LRU);
}
template<class K, class V>
typename LRUCacheH4<K, V>::const_iterator LRUCacheH4<K, V>::lru_begin() const
{
return const_iterator(_lru, const_iterator::LRU_TO_MRU);
}
template<class K, class V>
typename LRUCacheH4<K, V>::const_iterator LRUCacheH4<K, V>::end() const
{
return const_iterator();
}
template<class K, class V>
typename LRUCacheH4<K, V>::Val * LRUCacheH4<K, V>::_update_or_insert(const K & key)
{
typename MAP_TYPE::iterator it = _map.find(key);
if (it != _map.end())
return _update(it);
else
return _insert(key);
}
template<class K, class V>
typename LRUCacheH4<K, V>::Val * LRUCacheH4<K, V>::_update(typename MAP_TYPE::iterator it)
{
LRUCacheH4Value<K, V> & v = it->second;
Val * older = v._older;
Val * newer = v._newer;
Val * moved = &*it;
// possibly update the LRU
if (moved == _lru && _lru->second._newer)
_lru = _lru->second._newer;
if (moved != _mru) {
// "remove" key from current position
if (older)
older->second._newer = newer;
if (newer)
newer->second._older = older;
// "insert" key to MRU position
v._older = _mru;
v._newer = NULL;
_mru->second._newer = moved;
_mru = moved;
}
return moved;
}
template<class K, class V>
typename LRUCacheH4<K, V>::Val * LRUCacheH4<K, V>::_insert(const K & key)
{
// if we have grown too large, remove LRU
if (_map.size() >= _maxsize) {
Val * old_lru = _lru;
if (_lru->second._newer) {
_lru = _lru->second._newer;
_lru->second._older = NULL;
}
_map.erase(old_lru->first);
}
// insert key to MRU position
std::pair<typename MAP_TYPE::iterator, bool> ret
= _map.insert_unique(Val(key, LRUCacheH4Value<K, V>(V(), _mru, NULL)));
Val * inserted = &*ret.first;
if (_mru)
_mru->second._newer = inserted;
_mru = inserted;
// possibly update the LRU
if (!_lru)
_lru = _mru;
else if (!_lru->second._newer)
_lru->second._newer = _mru;
return inserted;
}
} // namespace plb