forked from qdrvm/kagome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lru.hpp
149 lines (126 loc) · 3.01 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
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef KAGOME_UTILS_LRU_HPP
#define KAGOME_UTILS_LRU_HPP
#include <boost/assert.hpp>
#include <unordered_map>
namespace kagome {
/**
* `std::unordered_map` with LRU.
*/
template <typename K, typename V>
class Lru {
public:
struct Item;
// TODO(turuslan): remove unique_ptr after deprecating gcc 10
using Map = std::unordered_map<K, std::unique_ptr<Item>>;
using It = typename Map::iterator;
struct Item {
V v;
It more, less;
};
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;
}
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:
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_;
};
} // namespace kagome
#endif // KAGOME_UTILS_LRU_HPP