29template <
class K,
class V>
37 template <
typename VV>
42template <
class K,
class V>
46 : capacity_(
capacity), head_(), tail_() {
53 for (
auto it = map_.begin(); it != map_.end(); ++it) {
54 cache->emplace(it->first, it->second.val);
61 GetObsolete(&obsolete);
70 for (
auto it = map_.begin(); it != map_.end(); ++it) {
71 ret->push_back(&it->second.val);
78 template <
typename VV>
79 bool Put(
const K& key, VV&& val) {
81 return Update(key, std::forward<VV>(val), &tmp,
false,
false);
87 template <
typename VV>
88 bool Update(
const K& key, VV&& val) {
93 return Update(key, std::forward<VV>(val), &tmp,
true,
false);
99 template <
typename VV>
105 return Update(key, std::forward<VV>(*val), &tmp,
true,
true);
111 template <
typename VV>
112 bool Add(
const K& key, VV* val) {
114 return Update(key, std::forward<VV>(*val), &tmp,
true,
false);
117 template <
typename VV>
119 return Update(key, std::forward<VV>(*val), obs,
false,
false);
122 template <
typename VV>
124 return Update(key, std::forward<VV>(*val), obs,
true,
false);
129 V*
Get(
const K& key) {
return Get(key,
false); }
132 return GetCopy(key, val,
true);
137 size_t size() {
return size_; }
159 bool Contains(
const K& key) {
return map_.find(key) != map_.end(); }
163 auto* node = &map_[key];
180 auto* node = &map_[key];
194 static constexpr size_t kDefaultCapacity = 10;
196 const size_t capacity_;
198 std::map<K, Node<K, V>> map_;
203 head_.
prev =
nullptr;
206 tail_.
next =
nullptr;
212 if (node->prev !=
nullptr) {
213 node->prev->next = node->next;
215 if (node->next !=
nullptr) {
216 node->next->prev = node->prev;
218 node->prev =
nullptr;
219 node->next =
nullptr;
225 node->next = head_.
next;
227 if (node->next !=
nullptr) {
228 node->next->prev = node;
233 template <
typename VV>
234 bool Update(
const K& key, VV&& val, K* obs,
bool add_only,
235 bool silent_update) {
236 if (obs ==
nullptr) {
241 map_[key].val = std::forward<VV>(val);
242 if (!silent_update) {
243 auto* node = &map_[key];
251 if (
Full() && !GetObsolete(obs)) {
255 map_.emplace(key,
Node<K, V>(key, std::forward<VV>(val)));
261 V*
Get(
const K& key,
bool silent) {
263 auto* node = &map_[key];
273 bool GetCopy(
const K& key, V*
const val,
bool silent) {
275 auto* node = &map_[key];
286 bool GetObsolete(K* key) {
288 auto* node = tail_.
prev;
291 map_.erase(node->key);
bool UpdateSilently(const K &key, VV *val)
bool ChangeCapacity(const size_t capacity)
bool Put(const K &key, VV &&val)
V & operator[](const K &key)
bool GetCopySilently(const K &key, V *const val)
bool Add(const K &key, VV *val)
bool GetCopy(const K &key, V *const val)
void GetCache(std::map< K, V > *cache)
bool AddAndGetObsolete(const K &key, VV *val, K *obs)
bool Update(const K &key, VV &&val)
void GetAllSilently(std::vector< V * > *ret)
bool Contains(const K &key)
bool Remove(const K &key)
V * GetSilently(const K &key)
bool Prioritize(const K &key)
bool PutAndGetObsolete(const K &key, VV *val, K *obs)
LRUCache(const size_t capacity=kDefaultCapacity)
Node(const K &key, VV &&val)