25namespace localization {
28template <
class Key,
class Element>
32template <
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);
59 Element*
Put(
const Key& key, Element* value);
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_;
90template <
class Key,
class Element,
class MapLRUCache>
93 auto value_ptr = lru_map_nodes_.Get(key);
101template <
class Key,
class Element,
class MapLRUCache>
104 auto value_ptr = lru_map_nodes_.GetSilently(key);
112template <
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));
144template <
class Key,
class Element,
class MapLRUCache>
146 auto* node_remove = lru_map_nodes_.GetSilently(key);
147 if (node_remove && lru_map_nodes_.Remove(key)) {
154template <
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;
175template <
class Key,
class Element,
class MapLRUCache>
177 return lru_map_nodes_.Prioritize(key);
The data structure of the LRUCache.
Element * ClearOne()
Remove the Least Recently Used element in the cache.
bool IsExist(const Key &key)
Find element for key in the cache.
unsigned int Size()
return cache's in use.
Element * Remove(const Key &key)
Remove element for key.
~MapNodeCache()
The destructor.
static bool CacheL2Destroy(Element *value)
bool GetSilent(const Key &key, Element **value)
Find element for key if it exists in the cache.
unsigned Capacity()
return cache's max capacity.
bool ChangeCapacity(int capacity)
Change cache's max capacity.
static bool CacheL1Destroy(Element *value)
bool Get(const Key &key, Element **value)
Find element for key if it exists in the cache.
Element * Put(const Key &key, Element *value)
Caches element for key.
std::function< bool(Element *)> DestroyFunc
MapNodeCache(unsigned int capacity, const DestroyFunc &destroy_func)
The constructor.