3#include <boost/multi_index/hashed_index.hpp>
4#include <boost/multi_index/member.hpp>
5#include <boost/multi_index/sequenced_index.hpp>
6#include <boost/multi_index_container.hpp>
7#include <boost/optional.hpp>
11namespace liarsdice::data_structures {
13 namespace mi = boost::multi_index;
16
17
18
19
20
21
22
23
24
25
26 template <
typename Key,
typename Value,
typename Hash =
std::
hash<
Key>,
57
58
59
63 throw std::invalid_argument(
"LRUCache: max_size must be > 0");
68
69
70
71
72
73 bool put(
const Key& key,
const Value& value) {
74 auto& key_index = cache_.
template get<by_key>();
75 auto it = key_index.find(key);
77 if (it != key_index.end()) {
79 key_index.modify(it, [&value](CacheEntry& entry) {
81 entry.timestamp = std::chrono::steady_clock::now();
86 auto& seq_index = cache_.
template get<by_sequence>();
87 seq_index.relocate(seq_index.begin(), cache_.
template project<by_sequence>(it));
93 if (cache_.size() >= max_size_) {
95 auto& seq_index = cache_.
template get<by_sequence>();
100 cache_.push_front(CacheEntry(key, value));
105
106
107
108
110 auto& key_index = cache_.
template get<by_key>();
111 auto it = key_index.find(key);
113 if (it == key_index.end()) {
121 key_index.modify(it, [](CacheEntry& entry) {
122 entry.timestamp = std::chrono::steady_clock::now();
123 entry.access_count++;
127 auto& seq_index = cache_.
template get<by_sequence>();
128 seq_index.relocate(seq_index.begin(), cache_.
template project<by_sequence>(it));
134
135
136
137
139 const auto& key_index = cache_.
template get<by_key>();
140 return key_index.find(key) != key_index.end();
144
145
146
147
149 auto& key_index = cache_.
template get<by_key>();
150 auto it = key_index.find(key);
152 if (it == key_index.end()) {
161
162
171
172
173
177
178
179
183
184
185
189
190
191
195
196
197
200 double hit_rate = total > 0 ?
static_cast<
double>(
hits_) / total : 0.0;
201 return {hits_, misses_, evictions_, hit_rate};
205
206
207
209 std::vector<Key> keys;
210 keys.reserve(cache_.size());
212 const auto& seq_index = cache_.
template get<by_sequence>();
213 for (
const auto& entry : seq_index) {
214 keys.push_back(entry.key);
221
222
223
224
225 template <
typename Func>
void for_each(Func func)
const {
226 const auto& seq_index = cache_.
template get<by_sequence>();
227 for (
const auto& entry : seq_index) {
228 func(entry.key, entry.value, entry.access_count);
233
234
235
236
237
240 throw std::invalid_argument(
"LRUCache: new_size must be > 0");
243 max_size_ = new_size;
246 while (cache_.size() > max_size_) {
247 auto& seq_index = cache_.
template get<by_sequence>();
248 seq_index.pop_back();
262
263
264
265
266 template <
typename StateKey,
typename GameState>
using GameStateCache
267 = LRUCache<StateKey, GameState>;
270
271
272
274 template <
typename PatternKey>
using PatternCache = LRUCache<PatternKey, std::vector<
double>>;
High-performance LRU cache using boost::multi_index.
Definition lru_cache.hpp:28
void for_each(Func func) const
Apply function to all entries in LRU order.
Definition lru_cache.hpp:225
size_type size() const
Get current number of entries.
Definition lru_cache.hpp:174
void resize(size_type new_size)
Resize cache capacity.
Definition lru_cache.hpp:238
size_t hits_
Definition lru_cache.hpp:256
size_type capacity() const
Get maximum capacity.
Definition lru_cache.hpp:192
std::vector< Key > get_keys() const
Get all cached keys in LRU order.
Definition lru_cache.hpp:208
bool erase(const Key &key)
Remove entry by key.
Definition lru_cache.hpp:148
cache_container cache_
Definition lru_cache.hpp:254
void clear()
Clear all entries.
Definition lru_cache.hpp:163
bool put(const Key &key, const Value &value)
Insert or update a key-value pair.
Definition lru_cache.hpp:73
std::tuple< size_t, size_t, size_t, double > get_stats() const
Get cache statistics.
Definition lru_cache.hpp:198
size_t evictions_
Definition lru_cache.hpp:258
size_t misses_
Definition lru_cache.hpp:257
bool contains(const Key &key) const
Check if key exists without updating LRU order.
Definition lru_cache.hpp:138
size_type max_size_
Definition lru_cache.hpp:255
boost::optional< Value > get(const Key &key)
Get value by key.
Definition lru_cache.hpp:109
LRUCache(size_type max_size)
Construct LRU cache with maximum capacity.
Definition lru_cache.hpp:60
bool empty() const
Check if cache is empty.
Definition lru_cache.hpp:180
bool full() const
Check if cache is full.
Definition lru_cache.hpp:186
Definition lru_cache.hpp:30
Key key
Definition lru_cache.hpp:31
Value value
Definition lru_cache.hpp:32
std::chrono::steady_clock::time_point timestamp
Definition lru_cache.hpp:33
size_t access_count
Definition lru_cache.hpp:34
CacheEntry(const Key &k, const Value &v)
Definition lru_cache.hpp:36
Definition lru_cache.hpp:42
Definition lru_cache.hpp:41