FastLED 3.9.15
Loading...
Searching...
No Matches
hash_map_lru.h
Go to the documentation of this file.
1#pragma once
2
3/*
4LRU (Least Recently Used) HashMap that is optimized for embedded devices.
5This hashmap has a maximum size and will automatically evict the least
6recently used items when it reaches capacity.
7*/
8
9#include "fl/hash_map.h"
10#include "fl/type_traits.h"
11
12namespace fl {
13
14template <typename Key, typename T, typename Hash = Hash<Key>,
15 typename KeyEqual = EqualTo<Key>,
16 int INLINED_COUNT = FASTLED_HASHMAP_INLINED_COUNT>
18 private:
19 // Wrapper for values that includes access time tracking
23
25 ValueWithTimestamp(const T &v, uint32_t time)
26 : value(v), last_access_time(time) {}
27 };
28
29 public:
31 // Ensure max size is at least 1
32 if (mMaxSize < 1)
33 mMaxSize = 1;
34 }
35
36 void setMaxSize(size_t max_size) {
37 while (mMaxSize < max_size) {
38 // Evict oldest items until we reach the new max size
40 }
42 }
43
44 void swap(HashMapLru &other) {
45 fl::swap(mMap, other.mMap);
48 }
49
50 // Insert or update a key-value pair
51 void insert(const Key &key, const T &value) {
52 // Only evict if we're at capacity AND this is a new key
53 const ValueWithTimestamp *existing = mMap.find_value(key);
54
55 auto curr = mCurrentTime++;
56
57 if (existing) {
58 // Update the value and access time
60 const_cast<ValueWithTimestamp &>(*existing);
61 vwt.value = value;
62 vwt.last_access_time = curr;
63 return;
64 }
65 if (mMap.size() >= mMaxSize) {
67 }
68
69 // Insert or update the value with current timestamp
71 mMap.insert(key, vwt);
72 }
73
74 // Get value for key, returns nullptr if not found
75 T *find_value(const Key &key) {
76 ValueWithTimestamp *vwt = mMap.find_value(key);
77 if (vwt) {
78 // Update access time
79 auto curr = mCurrentTime++;
80 vwt->last_access_time = curr;
81 return &vwt->value;
82 }
83 return nullptr;
84 }
85
86 // Get value for key, returns nullptr if not found (const version)
87 const T *find_value(const Key &key) const {
88 const ValueWithTimestamp *vwt = mMap.find_value(key);
89 return vwt ? &vwt->value : nullptr;
90 }
91
92 // Access operator - creates entry if not exists
93 T &operator[](const Key &key) {
94 // If we're at capacity and this is a new key, evict oldest
95 auto curr = mCurrentTime++;
96
97 auto entry = mMap.find_value(key);
98 if (entry) {
99 // Update access time
100 entry->last_access_time = curr;
101 return entry->value;
102 }
103
104 if (mMap.size() >= mMaxSize) {
105 evictOldest();
106 }
107
108 // Get or create entry and update timestamp
109 // mCurrentTime++;
110 ValueWithTimestamp &vwt = mMap[key];
111 vwt.last_access_time = curr;
112 return vwt.value;
113 }
114
115 // Remove a key
116 bool remove(const Key &key) { return mMap.remove(key); }
117
118 // Clear the map
119 void clear() {
120 mMap.clear();
121 mCurrentTime = 0;
122 }
123
124 // Size accessors
125 size_t size() const { return mMap.size(); }
126 bool empty() const { return mMap.empty(); }
127 size_t capacity() const { return mMaxSize; }
128
129 private:
130 // Evict the least recently used item
131 void evictOldest() {
132 if (mMap.empty())
133 return;
134
135 // Find the entry with the oldest timestamp
136 Key oldest_key;
137 uint32_t oldest_time = UINT32_MAX;
138 bool found = false;
139
140 for (auto it = mMap.begin(); it != mMap.end(); ++it) {
141 const auto &entry = *it;
142 const ValueWithTimestamp &vwt = entry.second;
143
144 if (vwt.last_access_time < oldest_time) {
145 oldest_time = vwt.last_access_time;
146 oldest_key = entry.first;
147 found = true;
148 }
149 }
150
151 // Remove the oldest entry
152 if (found) {
153 mMap.remove(oldest_key);
154 }
155 }
156
158 size_t mMaxSize;
159 uint32_t mCurrentTime;
160};
161
162} // namespace fl
void setMaxSize(size_t max_size)
HashMapLru(size_t max_size)
T & operator[](const Key &key)
size_t capacity() const
HashMap< Key, ValueWithTimestamp, Hash, KeyEqual, INLINED_COUNT > mMap
bool empty() const
T * find_value(const Key &key)
size_t size() const
void insert(const Key &key, const T &value)
void swap(HashMapLru &other)
bool remove(const Key &key)
uint32_t mCurrentTime
const T * find_value(const Key &key) const
void swap(array< T, N > &lhs, array< T, N > &rhs) noexcept(noexcept(lhs.swap(rhs)))
Definition array.h:140
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Definition Keyboard.h:22
ValueWithTimestamp(const T &v, uint32_t time)