-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlrucache0x.h
More file actions
200 lines (157 loc) · 5.22 KB
/
Copy pathlrucache0x.h
File metadata and controls
200 lines (157 loc) · 5.22 KB
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
//
// Copyright Erik Dubbelboer and other contributors. All rights reserved.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
// Based loosely on http://timday.bitbucket.org/lru.html by Tim Day.
//
// Possible improvements:
// - return an interator instead of a value.
//
#ifndef SRC_LRUCACHE_H_
#define SRC_LRUCACHE_H_
#include <list>
#include <assert.h>
/**
* Simple Least Recently Used (LRU) cache.
*
* This cache will discards the least recently used item when space is needed.
*
* Both KeyType and ItemType can be any type.
* Keep in mind that internally an std::map is used to when using strings as keys use std::string.
* When ItemType is a pointer the destructor will be called on eviction.
*/
template <class KeyType, class ItemType, template<typename...> class MapType = std::map>
class LRUCache {
private:
typedef std::list<KeyType> KeyTypeList;
typedef MapType<KeyType, std::pair<ItemType, typename KeyTypeList::iterator> > KeyItemMap;
KeyTypeList lru; // List of recently accessed items. The last item is the most recent.
KeyItemMap cache; // Map of cached items.
size_t capacity;
/**
* These two classes are helper classes. They help make sure destructors are
* called when ItemType is a pointer type.
* The compiler will select the correct class to use based on ItemType.
*/
template<typename T>
class Pointer {
public:
static void Delete(const T v) {
// Not a pointer so do nothing.
}
};
template<typename T>
class Pointer<T*> {
public:
static void Delete(T* v) {
delete v;
}
};
/**
* Remove the least recently accessed element.
*/
void Evict() {
const auto i = cache.find(lru.front());
assert(i != cache.end());
cache.erase(i);
lru.pop_front();
// Make sure the destructor is called.
Pointer<ItemType>::Delete(i->second.first);
}
public:
explicit LRUCache(size_t capacity) : capacity(capacity) {
}
size_t Capacity() {
return capacity;
}
/**
* Resize the cache.
*/
void Resize(size_t capacity) {
while (cache.size() > capacity) {
Evict();
}
this->capacity = capacity;
}
/**
* Get the entry at with the specified key.
*
* If the key was not set the default value for ItemType is returned.
* For example for pointers this will be a NULL pointer. For integers
* it will be 0.
*/
ItemType Get(const KeyType& key) {
const auto i = cache.find(key);
if (i == cache.end()) {
return ItemType(); // Return the default value for ItemType.
} else {
// Move the key back to the end of the lru (making it the most recently visited).
lru.splice(lru.end(), lru, i->second.second);
return i->second.first;
}
}
/**
* Set the entry at key to value.
* Removing the least recently accessed element if needed.
*/
void Set(const KeyType& key, const ItemType value) {
// Setting the capacity to 0 will disable the cache.
if (capacity == 0) {
return;
}
const auto i = cache.find(key);
if (i == cache.end()) {
// Make sure we have room.
if (cache.size() == capacity) {
Evict();
}
// Insert the key into the back of the lru list (making it the most recently visited).
// We need a pointer to the entry for the map.
const auto j = lru.insert(lru.end(), key);
// Insert the value into the map.
cache.insert(std::make_pair(key, std::make_pair(value, j)));
} else {
// Make sure the destructor is called.
Pointer<ItemType>::Delete(i->second.first);
i->second.first = value;
// Move the key back to the end of the lru (making it the most recently visited).
lru.splice(lru.end(), lru, i->second.second);
}
}
/**
* Remove the element with the specified key.
*/
void Remove(const KeyType& key) {
const auto i = cache.find(key);
if (i == cache.end()) {
return;
}
cache.erase(i);
lru.erase(i->second.second);
// Make sure the destructor is called.
Pointer<ItemType>::Delete(i->second.first);
}
/**
* Return a list of keys in the order of most to least recently used.
*/
KeyTypeList List() {
return KeyTypeList(lru.rbegin(), lru.rend());
}
};
#endif // SRC_LRUCACHE_H_