-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathlru.hpp
More file actions
221 lines (191 loc) · 4.5 KB
/
lru.hpp
File metadata and controls
221 lines (191 loc) · 4.5 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <boost/assert.hpp>
#include <functional>
#include <memory>
#include <optional>
#include <stdexcept>
#include <unordered_map>
namespace kagome {
/**
* `std::unordered_map` with LRU.
*/
template <typename K, typename V, typename H = std::hash<K>>
class Lru {
public:
struct Item;
// TODO(turuslan): remove unique_ptr after deprecating gcc 10
using Map = std::unordered_map<K, std::unique_ptr<Item>, H>;
using It = typename Map::iterator;
struct Item {
V v;
It more, less;
};
explicit Lru(size_t capacity) : capacity_{capacity} {
if (capacity_ == 0) {
throw std::length_error{"Lru(capacity=0)"};
}
}
Lru(const Lru &) = delete;
void operator=(const Lru &) = delete;
size_t capacity() const {
return capacity_;
}
size_t size() const {
return map_.size();
}
std::optional<std::reference_wrapper<V>> get(const K &k) {
auto it = map_.find(k);
if (it == map_.end()) {
return std::nullopt;
}
lru_use(it);
return std::ref(it->second->v);
}
V &put(const K &k, V v) {
return put2(k, std::move(v)).first->second->v;
}
void erase(const K &k) {
auto it = map_.find(k);
if (it == map_.end()) {
return;
}
lru_extract(*it->second);
map_.erase(it);
}
void forEach(const auto &f) {
for (auto &p : map_) {
f(p.first, p.second->v);
}
}
template <typename F>
void erase_if(const F &f) {
for (auto it = map_.begin(); it != map_.end();) {
if (f(it->first, it->second->v)) {
++it;
} else {
lru_extract(*it->second);
it = map_.erase(it);
}
}
}
private:
static auto empty(const It &it) {
return it == It{};
}
std::pair<It, bool> put2(const K &k, V &&v) {
auto it = map_.find(k);
if (it == map_.end()) {
if (map_.size() >= capacity_) {
lru_pop();
}
it = map_.emplace(k, std::make_unique<Item>(Item{std::move(v), {}, {}}))
.first;
lru_push(it);
return {it, true};
}
it->second->v = std::move(v);
lru_use(it);
return {it, false};
}
void lru_use(It it) {
if (it == most_) {
return;
}
lru_extract(*it->second);
lru_push(it);
}
void lru_push(It it) {
BOOST_ASSERT(empty(it->second->less));
BOOST_ASSERT(empty(it->second->more));
it->second->less = most_;
if (not empty(most_)) {
most_->second->more = it;
}
most_ = it;
if (empty(least_)) {
least_ = most_;
}
}
void lru_extract(Item &v) {
if (not empty(v.more)) {
v.more->second->less = v.less;
} else {
most_ = v.less;
}
if (not empty(v.less)) {
v.less->second->more = v.more;
} else {
least_ = v.more;
}
v.more = It{};
v.less = It{};
}
void lru_pop() {
auto it = least_;
lru_extract(*it->second);
map_.erase(it);
}
Map map_;
size_t capacity_;
It most_, least_;
template <typename>
friend class LruSet;
};
template <typename K>
class LruSet {
public:
explicit LruSet(size_t capacity) : lru_{capacity} {}
bool has(const K &k) {
return lru_.get(k).has_value();
}
bool add(const K &k) {
return lru_.put2(k, {}).second;
}
private:
struct V {};
Lru<K, V> lru_;
};
/**
* Wraps `map<K, LruSet<V>>` with `capacity` for `LruSet<V>`.
*/
template <typename K, typename V>
class MapLruSet {
public:
explicit MapLruSet(size_t capacity) : capacity_{capacity} {}
/**
* @returns true if `K` was added.
*/
bool add(const K &k) {
auto it = map_.find(k);
if (it != map_.end()) {
return false;
}
it = map_.emplace(k, capacity_).first;
return true;
}
/**
* @returns true if `(K, V)` was added.
*/
bool add(const K &k, const V &v) {
auto it = map_.find(k);
if (it == map_.end()) {
it = map_.emplace(k, capacity_).first;
}
return it->second.add(v);
}
/**
* Remove `K` and all corresponding `V`.
*/
void remove(const K &k) {
map_.erase(k);
}
private:
size_t capacity_;
std::unordered_map<K, LruSet<V>> map_{};
};
} // namespace kagome