25 namespace localization {
28 template <
class Key,
class Element>
32 template <
class Key,
class Element,
class MapLRUCache = LRUCache<Key, Element>>
37 value->SetIsReserved(
false);
41 return !(value->GetIsReserved());
47 : destroy_func_(destroy_func), lru_map_nodes_(capacity) {}
52 bool Get(
const Key& key, Element**
value);
56 bool GetSilent(
const Key& key, Element** value);
59 Element*
Put(
const Key& key, Element* value);
62 Element*
Remove(
const Key& key);
74 return lru_map_nodes_.ChangeCapacity(capacity);
77 unsigned int Size() {
return lru_map_nodes_.size(); }
79 unsigned Capacity() {
return lru_map_nodes_.capacity(); }
87 MapLRUCache lru_map_nodes_;
90 template <
class Key,
class Element,
class MapLRUCache>
93 auto value_ptr = lru_map_nodes_.Get(key);
101 template <
class Key,
class Element,
class MapLRUCache>
104 auto value_ptr = lru_map_nodes_.GetSilently(key);
112 template <
class Key,
class Element,
class MapLRUCache>
115 if (value ==
nullptr) {
116 AINFO <<
"LRUCache Warning: put a NULL";
120 auto* value_ptr = lru_map_nodes_.Get(key);
121 Element* node_remove =
nullptr;
123 node_remove = *value_ptr;
124 if (destroy_func_(node_remove)) {
132 if (lru_map_nodes_.size() >= lru_map_nodes_.capacity()) {
133 auto* node = lru_map_nodes_.Last();
134 node_remove = node->val;
136 lru_map_nodes_.PutAndGetObsolete(key, &value, &key_tmp);
140 lru_map_nodes_.Put(key, std::move(value));
144 template <
class Key,
class Element,
class MapLRUCache>
146 auto* node_remove = lru_map_nodes_.GetSilently(key);
147 if (node_remove && lru_map_nodes_.Remove(key)) {
154 template <
class Key,
class Element,
class MapLRUCache>
156 auto* node_remove = lru_map_nodes_.Last();
160 while (node_remove != lru_map_nodes_.First()) {
161 if (destroy_func_(node_remove->val)) {
162 lru_map_nodes_.Remove(node_remove->key);
163 return node_remove->val;
165 node_remove = node_remove->prev;
167 if (node_remove == lru_map_nodes_.First() &&
168 destroy_func_(node_remove->val)) {
169 lru_map_nodes_.Remove(node_remove->key);
170 return node_remove->val;
175 template <
class Key,
class Element,
class MapLRUCache>
177 return lru_map_nodes_.Prioritize(key);
bool IsExist(const Key &key)
Find element for key in the cache. If it exists, move it to the head of queue.
Definition: base_map_cache.h:176
The data structure of the LRUCache.
Definition: base_map_cache.h:33
Definition: lru_cache.h:43
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
unsigned int Size()
return cache's in use.
Definition: base_map_cache.h:77
bool GetSilent(const Key &key, Element **value)
Find element for key if it exists in the cache. If not exist, return false. This function is thread s...
Definition: base_map_cache.h:102
~MapNodeCache()
The destructor.
Definition: base_map_cache.h:49
static bool CacheL1Destroy(Element *value)
Definition: base_map_cache.h:36
MapNodeCache(unsigned int capacity, const DestroyFunc &destroy_func)
The constructor.
Definition: base_map_cache.h:46
bool ChangeCapacity(int capacity)
Change cache's max capacity. New capacity must be larger than size in use.
Definition: base_map_cache.h:73
std::function< bool(apollo::localization::msf::pyramid_map::BaseMapNode *)> DestroyFunc
Definition: base_map_cache.h:35
Element * Put(const Key &key, Element *value)
Caches element for key. If cache is full, return the removed element, otherwise return null...
Definition: base_map_cache.h:113
unsigned Capacity()
return cache's max capacity.
Definition: base_map_cache.h:79
static bool CacheL2Destroy(Element *value)
Definition: base_map_cache.h:40
bool Get(const Key &key, Element **value)
Find element for key if it exists in the cache. If not exist, return false.
Definition: base_map_cache.h:91
apollo::cyber::base::std value
Element * Remove(const Key &key)
Remove element for key. if it exist in the cache, return the element, otherwise return null...
Definition: base_map_cache.h:145
#define AINFO
Definition: log.h:42
Element * ClearOne()
Remove the Least Recently Used element in the cache. return the removed element or null...
Definition: base_map_cache.h:155