14template<
typename Key,
typename Value>
25 using Data = std::map<Key, std::pair<LRUIterator, Value>>;
26 using LRU = std::list<typename Data::iterator>;
28 struct LRUIterator {
typename LRU::iterator it; };
35 LRUCache(
size_t capacity) : capacity(capacity) { }
42 if (capacity == 0)
return;
46 if (data.size() >= capacity) {
50 auto oldest = lru.begin();
55 auto res = data.emplace(
key, std::make_pair(LRUIterator(),
value));
59 auto j = lru.insert(lru.end(),
i);
61 i->second.first.it =
j;
64 bool erase(
const Key &
key)
67 if (
i ==
data.end())
return false;
68 lru.erase(
i->second.first.it);
77 std::optional<Value>
get(
const Key &
key)
79 auto i = data.find(
key);
80 if (
i == data.end())
return {};
85 lru.erase(
i->second.first.it);
86 auto j = lru.insert(lru.end(),
i);
87 i->second.first.it =
j;
89 return i->second.second;
void upsert(const Key &key, const Value &value)
Definition lru-cache.hh:40
std::optional< Value > get(const Key &key)
Definition lru-cache.hh:77
const T::key_type & key
Definition lexer.l:2763
auto i
Definition lexer.l:2745
Strings res
Definition lexer.l:2566
std::variant< std::string, std::string_view > data
Definition lexer.l:177
ExprAttrs::AttrDefs::iterator j
Definition lexer.l:8572
const T & value
Definition lexer.l:492