| 20 | typename KeyEqual = EqualTo<Key>, |
| 21 | int INLINED_COUNT = FASTLED_HASHMAP_INLINED_COUNT> |
| 22 | class HashMapLru { |
| 23 | private: |
| 24 | // Wrapper for values that includes access time tracking |
| 25 | struct ValueWithTimestamp { |
| 26 | T value; |
| 27 | u32 last_access_time; |
| 28 | |
| 29 | ValueWithTimestamp() FL_NOEXCEPT : last_access_time(0) {} |
| 30 | ValueWithTimestamp(const T &v, u32 time) |
| 31 | : value(v), last_access_time(time) {} |
| 32 | }; |
| 33 | |
| 34 | public: |
| 35 | HashMapLru(fl::size max_size) : mMaxSize(max_size), mCurrentTime(0) { |
| 36 | // Ensure max size is at least 1 |
| 37 | if (mMaxSize < 1) |
| 38 | mMaxSize = 1; |
| 39 | } |
| 40 | |
| 41 | HashMapLru(fl::size max_size, memory_resource* resource) |
| 42 | : mMap(resource), mMaxSize(max_size), mCurrentTime(0) { |
| 43 | if (mMaxSize < 1) |
| 44 | mMaxSize = 1; |
| 45 | } |
| 46 | |
| 47 | void setMaxSize(fl::size max_size) { |
| 48 | while (mMaxSize < max_size) { |
| 49 | // Evict oldest items until we reach the new max size |
| 50 | evictOldest(); |
| 51 | } |
| 52 | mMaxSize = max_size; |
| 53 | } |
| 54 | |
| 55 | void swap(HashMapLru &other) { |
| 56 | fl::swap(mMap, other.mMap); |
| 57 | fl::swap(mMaxSize, other.mMaxSize); |
| 58 | fl::swap(mCurrentTime, other.mCurrentTime); |
| 59 | } |
| 60 | |
| 61 | // Insert or update a key-value pair |
| 62 | void insert(const Key &key, const T &value) { |
| 63 | // Only evict if we're at capacity AND this is a new key |
| 64 | const ValueWithTimestamp *existing = mMap.find_value(key); |
| 65 | |
| 66 | auto curr = mCurrentTime++; |
| 67 | |
| 68 | if (existing) { |
| 69 | // Update the value and access time |
| 70 | ValueWithTimestamp &vwt = |
| 71 | const_cast<ValueWithTimestamp &>(*existing); |
| 72 | vwt.value = value; |
| 73 | vwt.last_access_time = curr; |
| 74 | return; |
| 75 | } |
| 76 | if (mMap.size() >= mMaxSize) { |
| 77 | evictOldest(); |
| 78 | } |
| 79 |
nothing calls this directly
no test coverage detected