|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <cstddef> |
| 4 | +#include <functional> |
| 5 | +#include <iostream> |
| 6 | +#include <list> |
| 7 | +#include <optional> |
| 8 | +#include <stdexcept> |
| 9 | +#include <unordered_map> |
| 10 | + |
| 11 | +namespace infinicore::common { |
| 12 | +template <typename Key, typename Value> |
| 13 | +class LRUCache { |
| 14 | +public: |
| 15 | + using KeyValuePair = std::pair<Key, Value>; |
| 16 | + using ListIt = typename std::list<KeyValuePair>::iterator; |
| 17 | + using Destructor = std::function<void(Value &)>; |
| 18 | + |
| 19 | + explicit LRUCache(size_t capacity = 100, Destructor destructor = nullptr) |
| 20 | + : capacity_(capacity), destructor_(destructor) { |
| 21 | + if (capacity == 0) { |
| 22 | + capacity_ = UINT64_MAX; // effectively unbounded |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + ~LRUCache() { |
| 27 | + cleanup(); |
| 28 | + } |
| 29 | + |
| 30 | + bool contains(const Key &key) const { |
| 31 | + return map_.find(key) != map_.end(); |
| 32 | + } |
| 33 | + |
| 34 | + void put(const Key &key, const Value &value) { |
| 35 | + auto it = map_.find(key); |
| 36 | + if (it != map_.end()) { |
| 37 | + if (destructor_) { |
| 38 | + destructor_(it->second->second); |
| 39 | + } |
| 40 | + it->second->second = value; |
| 41 | + touch(it); |
| 42 | + } else { |
| 43 | + // insert new |
| 44 | + if (list_.size() >= capacity_) { |
| 45 | + evictLRU(); |
| 46 | + } |
| 47 | + list_.emplace_front(key, value); |
| 48 | + map_[key] = list_.begin(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + std::optional<Value> get(const Key &key) { |
| 53 | + auto it = map_.find(key); |
| 54 | + if (it == map_.end()) { |
| 55 | + return std::nullopt; |
| 56 | + } |
| 57 | + touch(it); |
| 58 | + return it->second->second; |
| 59 | + } |
| 60 | + |
| 61 | + std::optional<Value> get(const Key &key) const { |
| 62 | + auto it = map_.find(key); |
| 63 | + if (it == map_.end()) { |
| 64 | + return std::nullopt; |
| 65 | + } |
| 66 | + // Note: can't touch in const context |
| 67 | + return it->second->second; |
| 68 | + } |
| 69 | + |
| 70 | + void setDestructor(Destructor destructor) { |
| 71 | + destructor_ = destructor; |
| 72 | + } |
| 73 | + |
| 74 | + void setCapacity(size_t capacity) { |
| 75 | + capacity_ = capacity; |
| 76 | + while (list_.size() > capacity_) { |
| 77 | + evictLRU(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + void clear() { |
| 82 | + if (destructor_) { |
| 83 | + for (auto &item : list_) { |
| 84 | + safeDestruct(item.second); |
| 85 | + } |
| 86 | + } |
| 87 | + list_.clear(); |
| 88 | + map_.clear(); |
| 89 | + } |
| 90 | + |
| 91 | + const std::list<KeyValuePair> &getAllItems() const { |
| 92 | + return list_; |
| 93 | + } |
| 94 | + |
| 95 | +protected: |
| 96 | + std::list<KeyValuePair> list_; // front = most recent, back = least |
| 97 | + |
| 98 | +private: |
| 99 | + void touch(typename std::unordered_map<Key, ListIt>::iterator it) { |
| 100 | + // move this key to front (most recent) |
| 101 | + list_.splice(list_.begin(), list_, it->second); |
| 102 | + it->second = list_.begin(); |
| 103 | + } |
| 104 | + |
| 105 | + void safeDestruct(Value &value) { |
| 106 | + if (!destructor_) { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + try { |
| 111 | + destructor_(value); |
| 112 | + } catch (const std::exception &e) { |
| 113 | + // Built-in default error handling |
| 114 | + std::cerr << "Cache destructor error (type: " << typeid(Value).name() |
| 115 | + << "): " << e.what() << std::endl; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + void evictLRU() { |
| 120 | + if (!list_.empty()) { |
| 121 | + auto &kv = list_.back(); |
| 122 | + safeDestruct(kv.second); |
| 123 | + map_.erase(kv.first); |
| 124 | + list_.pop_back(); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + void cleanup() { |
| 129 | + clear(); |
| 130 | + } |
| 131 | + |
| 132 | + size_t capacity_; |
| 133 | + std::unordered_map<Key, ListIt> map_; |
| 134 | + Destructor destructor_; |
| 135 | +}; |
| 136 | + |
| 137 | +} // namespace infinicore::common |
0 commit comments