|
1 |
| -// Copyright 2024 TIER IV, Inc. |
| 1 | +// Copyright 2025 The Autoware Contributors |
2 | 2 | //
|
3 | 3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
4 | 4 | // you may not use this file except in compliance with the License.
|
|
11 | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 | 12 | // See the License for the specific language governing permissions and
|
13 | 13 | // limitations under the License.
|
| 14 | + |
14 | 15 | #ifndef AUTOWARE_UTILS__SYSTEM__LRU_CACHE_HPP_
|
15 | 16 | #define AUTOWARE_UTILS__SYSTEM__LRU_CACHE_HPP_
|
16 | 17 |
|
17 |
| -#include <cstddef> |
18 |
| -#include <list> |
19 |
| -#include <optional> |
20 |
| -#include <unordered_map> |
21 |
| -#include <utility> |
22 |
| - |
23 |
| -namespace autoware_utils |
24 |
| -{ |
25 |
| - |
26 |
| -/** |
27 |
| - * @brief A template class for LRU (Least Recently Used) Cache. |
28 |
| - * |
29 |
| - * This class implements a simple LRU cache using a combination of a list and a hash map. |
30 |
| - * |
31 |
| - * @tparam Key The type of keys. |
32 |
| - * @tparam Value The type of values. |
33 |
| - * @tparam Map The type of underlying map, defaulted to std::unordered_map. |
34 |
| - */ |
35 |
| -template <typename Key, typename Value, template <typename...> class Map = std::unordered_map> |
36 |
| -class LRUCache |
37 |
| -{ |
38 |
| -private: |
39 |
| - size_t capacity_; ///< The maximum capacity of the cache. |
40 |
| - std::list<std::pair<Key, Value>> cache_list_; ///< List to maintain the order of elements. |
41 |
| - Map<Key, typename std::list<std::pair<Key, Value>>::iterator> |
42 |
| - cache_map_; ///< Map for fast access to elements. |
43 |
| - |
44 |
| -public: |
45 |
| - /** |
46 |
| - * @brief Construct a new LRUCache object. |
47 |
| - * |
48 |
| - * @param size The capacity of the cache. |
49 |
| - */ |
50 |
| - explicit LRUCache(size_t size) : capacity_(size) {} |
51 |
| - |
52 |
| - /** |
53 |
| - * @brief Get the capacity of the cache. |
54 |
| - * |
55 |
| - * @return The capacity of the cache. |
56 |
| - */ |
57 |
| - [[nodiscard]] size_t capacity() const { return capacity_; } |
58 |
| - |
59 |
| - /** |
60 |
| - * @brief Insert a key-value pair into the cache. |
61 |
| - * |
62 |
| - * If the key already exists, its value is updated and it is moved to the front. |
63 |
| - * If the cache exceeds its capacity, the least recently used element is removed. |
64 |
| - * |
65 |
| - * @param key The key to insert. |
66 |
| - * @param value The value to insert. |
67 |
| - */ |
68 |
| - void put(const Key & key, const Value & value) |
69 |
| - { |
70 |
| - auto it = cache_map_.find(key); |
71 |
| - if (it != cache_map_.end()) { |
72 |
| - cache_list_.erase(it->second); |
73 |
| - } |
74 |
| - cache_list_.push_front({key, value}); |
75 |
| - cache_map_[key] = cache_list_.begin(); |
76 |
| - |
77 |
| - if (cache_map_.size() > capacity_) { |
78 |
| - auto last = cache_list_.back(); |
79 |
| - cache_map_.erase(last.first); |
80 |
| - cache_list_.pop_back(); |
81 |
| - } |
82 |
| - } |
83 |
| - |
84 |
| - /** |
85 |
| - * @brief Retrieve a value from the cache. |
86 |
| - * |
87 |
| - * If the key does not exist in the cache, std::nullopt is returned. |
88 |
| - * If the key exists, the value is returned and the element is moved to the front. |
89 |
| - * |
90 |
| - * @param key The key to retrieve. |
91 |
| - * @return The value associated with the key, or std::nullopt if the key does not exist. |
92 |
| - */ |
93 |
| - std::optional<Value> get(const Key & key) |
94 |
| - { |
95 |
| - auto it = cache_map_.find(key); |
96 |
| - if (it == cache_map_.end()) { |
97 |
| - return std::nullopt; |
98 |
| - } |
99 |
| - cache_list_.splice(cache_list_.begin(), cache_list_, it->second); |
100 |
| - return it->second->second; |
101 |
| - } |
102 |
| - |
103 |
| - /** |
104 |
| - * @brief Clear the cache. |
105 |
| - * |
106 |
| - * This removes all elements from the cache. |
107 |
| - */ |
108 |
| - void clear() |
109 |
| - { |
110 |
| - cache_list_.clear(); |
111 |
| - cache_map_.clear(); |
112 |
| - } |
113 |
| - |
114 |
| - /** |
115 |
| - * @brief Get the current size of the cache. |
116 |
| - * |
117 |
| - * @return The number of elements in the cache. |
118 |
| - */ |
119 |
| - [[nodiscard]] size_t size() const { return cache_map_.size(); } |
120 |
| - |
121 |
| - /** |
122 |
| - * @brief Check if the cache is empty. |
123 |
| - * |
124 |
| - * @return True if the cache is empty, false otherwise. |
125 |
| - */ |
126 |
| - [[nodiscard]] bool empty() const { return cache_map_.empty(); } |
| 18 | +// NOLINTBEGIN(build/namespaces, whitespace/line_length) |
| 19 | +// clang-format off |
127 | 20 |
|
128 |
| - /** |
129 |
| - * @brief Check if a key exists in the cache. |
130 |
| - * |
131 |
| - * @param key The key to check. |
132 |
| - * @return True if the key exists, false otherwise. |
133 |
| - */ |
134 |
| - [[nodiscard]] bool contains(const Key & key) const |
135 |
| - { |
136 |
| - return cache_map_.find(key) != cache_map_.end(); |
137 |
| - } |
138 |
| -}; |
| 21 | +#pragma message("#include <autoware_utils/system/lru_cache.hpp> is deprecated. Use #include <autoware_utils_system/lru_cache.hpp> instead.") |
| 22 | +#include <autoware_utils_system/lru_cache.hpp> |
| 23 | +namespace autoware_utils { using namespace autoware_utils_system; } |
139 | 24 |
|
140 |
| -} // namespace autoware_utils |
| 25 | +// clang-format on |
| 26 | +// NOLINTEND |
141 | 27 |
|
142 | 28 | #endif // AUTOWARE_UTILS__SYSTEM__LRU_CACHE_HPP_
|
0 commit comments