1 #ifndef AETHER_LRUCACHE_HPP 2 #define AETHER_LRUCACHE_HPP 6 #include <unordered_map> 13 template <
typename Key,
typename Value>
17 typedef typename std::pair<Key, Value> KeyValuePair;
18 typedef typename std::list<KeyValuePair>::iterator ListIterator;
22 std::list<KeyValuePair> items;
25 std::unordered_map<Key, ListIterator> keyMap;
28 std::function<void(const Key &, const Value &)> callback;
38 this->callback =
nullptr;
48 LRUCache(
const unsigned int size,
const std::function<
void(
const Key &,
const Value &)> & func =
nullptr) {
49 this->callback = func;
59 void addData(
const Key & key,
const Value & data) {
61 typename std::unordered_map<Key, ListIterator>::iterator it = this->keyMap.find(key);
62 this->items.push_front(KeyValuePair(key, data));
63 if (it != this->keyMap.end()) {
65 this->items.erase(it->second);
66 this->keyMap.erase(it);
68 this->keyMap[key] = this->items.begin();
71 if (this->keyMap.size() > this->maxSize) {
73 typename std::list<KeyValuePair>::iterator it2 = this->items.end();
77 if (this->callback !=
nullptr) {
78 this->callback(it2->first, it2->second);
82 this->keyMap.erase(it2->first);
83 this->items.pop_back();
96 typename std::unordered_map<Key, ListIterator>::iterator it = this->keyMap.find(key);
97 if (it != this->keyMap.end()) {
99 this->items.splice(this->items.begin(), this->items, it->second);
103 return this->items.begin()->second;
113 return (this->keyMap.find(key) != this->keyMap.end());
122 return this->keyMap.size();
129 if (this->callback !=
nullptr) {
130 for (
const KeyValuePair & pair : this->items) {
131 this->callback(pair.first, pair.second);
Value getData(const Key &key)
Returns the cached value associated with the given key.
Definition: LRUCache.hpp:95
void addData(const Key &key, const Value &data)
Add data to the cache using the given key.
Definition: LRUCache.hpp:59
A basic LRU (least recently used) cache implementation. Also supports a custom callback for when an i...
Definition: LRUCache.hpp:14
~LRUCache()
Calls callback function on each stored item when deleted.
Definition: LRUCache.hpp:128
Base namespace for all Aether related classes and functions.
bool hasKey(const Key &key)
Returns if the given key has a cached value.
Definition: LRUCache.hpp:112
LRUCache(const unsigned int size, const std::function< void(const Key &, const Value &)> &func=nullptr)
Create a LRU Cache with the given size.
Definition: LRUCache.hpp:48
unsigned int size()
Returns the number of items in the cache.
Definition: LRUCache.hpp:121
LRUCache()
Default constructor initializes a cache of size 100 and no callback.
Definition: LRUCache.hpp:37