29 template <
class K,
class V>
35 Node() : prev(nullptr), next(nullptr) {}
37 template <
typename VV>
38 Node(
const K& key, VV&& val)
39 : key(key), val(
std::forward<VV>(val)), prev(nullptr), next(nullptr) {}
42 template <
class K,
class V>
45 explicit LRUCache(
const size_t capacity = kDefaultCapacity)
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>
81 return Update(key, std::forward<VV>(
val), &tmp,
false,
false);
87 template <
typename VV>
93 return Update(key, std::forward<VV>(
val), &tmp,
true,
false);
99 template <
typename VV>
101 if (!Contains(key)) {
105 return Update(key, std::forward<VV>(*val), &tmp,
true,
true);
111 template <
typename VV>
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);
132 return GetCopy(key, val,
true);
137 size_t size() {
return size_; }
139 bool Full() {
return size() > 0 && size() >= capacity_; }
141 bool Empty() {
return size() == 0; }
159 bool Contains(
const K&
key) {
return map_.find(key) != map_.end(); }
163 auto* node = &map_[
key];
177 if (!Contains(key)) {
180 auto* node = &map_[
key];
186 if (size() > capacity) {
189 capacity_ = capacity;
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) {
215 if (node->
next !=
nullptr) {
218 node->
prev =
nullptr;
219 node->
next =
nullptr;
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) {
242 if (!silent_update) {
243 auto* node = &map_[
key];
251 if (Full() && !GetObsolete(obs)) {
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);
void GetAllSilently(std::vector< V *> *ret)
Definition: lru_cache.h:69
bool Prioritize(const K &key)
Definition: lru_cache.h:161
Definition: lru_cache.h:43
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
LRUCache(const size_t capacity=kDefaultCapacity)
Definition: lru_cache.h:45
K key
Definition: lru_cache.h:31
bool Add(const K &key, VV *val)
Definition: lru_cache.h:112
bool GetCopySilently(const K &key, V *const val)
Definition: lru_cache.h:131
Node()
Definition: lru_cache.h:35
size_t size()
Definition: lru_cache.h:137
void GetCache(std::map< K, V > *cache)
Definition: lru_cache.h:52
bool Update(const K &key, VV &&val)
Definition: lru_cache.h:88
size_t capacity()
Definition: lru_cache.h:143
V * GetSilently(const K &key)
Definition: lru_cache.h:127
bool ChangeCapacity(const size_t capacity)
Definition: lru_cache.h:185
Node(const K &key, VV &&val)
Definition: lru_cache.h:38
bool Empty()
Definition: lru_cache.h:141
bool UpdateSilently(const K &key, VV *val)
Definition: lru_cache.h:100
bool PutAndGetObsolete(const K &key, VV *val, K *obs)
Definition: lru_cache.h:118
bool AddAndGetObsolete(const K &key, VV *val, K *obs)
Definition: lru_cache.h:123
bool Full()
Definition: lru_cache.h:139
bool Init(const char *binary_name)
Node< K, V > * First()
Definition: lru_cache.h:145
V val
Definition: lru_cache.h:32
Definition: lru_cache.h:30
bool Remove(const K &key)
Definition: lru_cache.h:176
V & operator[](const K &key)
Definition: lru_cache.h:58
bool GetCopy(const K &key, V *const val)
Definition: lru_cache.h:135
Node * prev
Definition: lru_cache.h:33
bool Contains(const K &key)
Definition: lru_cache.h:159
bool Put(const K &key, VV &&val)
Definition: lru_cache.h:79
Node< K, V > * Last()
Definition: lru_cache.h:152
~LRUCache()
Definition: lru_cache.h:50
void Clear()
Definition: lru_cache.h:171
V * Get(const K &key)
Definition: lru_cache.h:129
Node * next
Definition: lru_cache.h:34