Apollo 10.0
自动驾驶开放平台
base_map_cache.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2020 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16#pragma once
17
18#include <functional>
19#include <utility>
20#include "cyber/common/log.h"
21
23
24namespace apollo {
25namespace localization {
26namespace msf {
27
28template <class Key, class Element>
30
32template <class Key, class Element, class MapLRUCache = LRUCache<Key, Element>>
34 public:
35 using DestroyFunc = std::function<bool(Element*)>;
36 static bool CacheL1Destroy(Element* value) {
37 value->SetIsReserved(false);
38 return true;
39 }
40 static bool CacheL2Destroy(Element* value) {
41 return !(value->GetIsReserved());
42 }
43
44 public:
46 MapNodeCache(unsigned int capacity, const DestroyFunc& destroy_func)
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);
65 Element* ClearOne();
66 // bool clear();
69 bool IsExist(const Key& key);
70
73 bool ChangeCapacity(int capacity) {
74 return lru_map_nodes_.ChangeCapacity(capacity);
75 }
77 unsigned int Size() { return lru_map_nodes_.size(); }
79 unsigned Capacity() { return lru_map_nodes_.capacity(); }
80
81 private:
86 const DestroyFunc destroy_func_;
87 MapLRUCache lru_map_nodes_;
88};
89
90template <class Key, class Element, class MapLRUCache>
92 Element** value) {
93 auto value_ptr = lru_map_nodes_.Get(key);
94 if (!value_ptr) {
95 return false;
96 }
97 *value = *value_ptr;
98 return true;
99}
100
101template <class Key, class Element, class MapLRUCache>
103 Element** value) {
104 auto value_ptr = lru_map_nodes_.GetSilently(key);
105 if (!value_ptr) {
106 return false;
107 }
108 *value = *value_ptr;
109 return true;
110}
111
112template <class Key, class Element, class MapLRUCache>
114 Element* value) {
115 if (value == nullptr) {
116 AINFO << "LRUCache Warning: put a NULL";
117 return nullptr;
118 }
119
120 auto* value_ptr = lru_map_nodes_.Get(key);
121 Element* node_remove = nullptr;
122 if (value_ptr) {
123 node_remove = *value_ptr;
124 if (destroy_func_(node_remove)) {
125 *value_ptr = value;
126 } else {
127 node_remove = value;
128 }
129 return node_remove;
130 }
131
132 if (lru_map_nodes_.size() >= lru_map_nodes_.capacity()) {
133 auto* node = lru_map_nodes_.Last();
134 node_remove = node->val;
135 Key key_tmp;
136 lru_map_nodes_.PutAndGetObsolete(key, &value, &key_tmp);
137 return node_remove;
138 }
139
140 lru_map_nodes_.Put(key, std::move(value));
141 return node_remove;
142}
143
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)) {
148 return *node_remove;
149 }
150
151 return nullptr;
152}
153
154template <class Key, class Element, class MapLRUCache>
156 auto* node_remove = lru_map_nodes_.Last();
157 if (!node_remove) {
158 return nullptr;
159 }
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;
164 }
165 node_remove = node_remove->prev;
166 }
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;
171 }
172 return nullptr;
173}
174
175template <class Key, class Element, class MapLRUCache>
177 return lru_map_nodes_.Prioritize(key);
178}
179
180} // namespace msf
181} // namespace localization
182} // namespace apollo
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.
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.
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37