| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "hash_map.h" |
| 34 |
#include "list.h" |
| 35 |
|
| 36 |
template <typename TKey, typename TData, typename Hasher = HashMapHasherDefault, typename Comparator = HashMapComparatorDefault<TKey>, void (*BeforeEvict)(TKey &, TData &) = nullptr> |
| 37 |
class LRUCache { |
| 38 |
public: |
| 39 |
struct Pair { |
| 40 |
TKey key; |
| 41 |
TData data; |
| 42 |
|
| 43 |
Pair() {} |
| 44 |
Pair(const TKey &p_key, const TData &p_data) : |
| 45 |
key(p_key), |
| 46 |
data(p_data) { |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
typedef typename List<Pair>::Element *Element; |
| 51 |
|
| 52 |
private: |
| 53 |
List<Pair> _list; |
| 54 |
HashMap<TKey, Element, Hasher, Comparator> _map; |
| 55 |
size_t capacity; |
| 56 |
|
| 57 |
public: |
| 58 |
const Pair *insert(const TKey &p_key, const TData &p_value) { |
| 59 |
Element *e = _map.getptr(p_key); |
| 60 |
Element n = _list.push_front(Pair(p_key, p_value)); |
| 61 |
|
| 62 |
if (e) { |
| 63 |
GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Waddress") |
| 64 |
if constexpr (BeforeEvict != nullptr) { |
| 65 |
BeforeEvict((*e)->get().key, (*e)->get().data); |
| 66 |
} |
| 67 |
GODOT_GCC_WARNING_POP |
| 68 |
_list.erase(*e); |
| 69 |
_map.erase(p_key); |
| 70 |
} |
| 71 |
_map[p_key] = _list.front(); |
| 72 |
|
| 73 |
while (_map.size() > capacity) { |
| 74 |
Element d = _list.back(); |
| 75 |
GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Waddress") |
| 76 |
if constexpr (BeforeEvict != nullptr) { |
| 77 |
BeforeEvict(d->get().key, d->get().data); |
| 78 |
} |
| 79 |
GODOT_GCC_WARNING_POP |
| 80 |
_map.erase(d->get().key); |
| 81 |
_list.pop_back(); |
| 82 |
} |
| 83 |
|
| 84 |
return &n->get(); |
| 85 |
} |
| 86 |
|
| 87 |
void clear() { |
| 88 |
_map.clear(); |
| 89 |
_list.clear(); |
| 90 |
} |
| 91 |
|
| 92 |
bool has(const TKey &p_key) const { |
| 93 |
return _map.getptr(p_key); |
| 94 |
} |
| 95 |
|
| 96 |
bool erase(const TKey &p_key) { |
| 97 |
Element *e = _map.getptr(p_key); |
| 98 |
if (!e) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
_list.move_to_front(*e); |
| 102 |
_map.erase(p_key); |
| 103 |
_list.pop_front(); |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
const TData &get(const TKey &p_key) { |
| 108 |
Element *e = _map.getptr(p_key); |
| 109 |
CRASH_COND(!e); |
| 110 |
_list.move_to_front(*e); |
| 111 |
return (*e)->get().data; |
| 112 |
} |
| 113 |
|
| 114 |
const TData *getptr(const TKey &p_key) { |
| 115 |
Element *e = _map.getptr(p_key); |
| 116 |
if (!e) { |
| 117 |
return nullptr; |
| 118 |
} else { |
| 119 |
_list.move_to_front(*e); |
| 120 |
return &(*e)->get().data; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
_FORCE_INLINE_ size_t get_capacity() const { return capacity; } |
| 125 |
_FORCE_INLINE_ size_t get_size() const { return _map.size(); } |
| 126 |
|
| 127 |
void set_capacity(size_t p_capacity) { |
| 128 |
if (capacity > 0) { |
| 129 |
capacity = p_capacity; |
| 130 |
while (_map.size() > capacity) { |
| 131 |
Element d = _list.back(); |
| 132 |
GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Waddress") |
| 133 |
if constexpr (BeforeEvict != nullptr) { |
| 134 |
BeforeEvict(d->get().key, d->get().data); |
| 135 |
} |
| 136 |
GODOT_GCC_WARNING_POP |
| 137 |
_map.erase(d->get().key); |
| 138 |
_list.pop_back(); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
LRUCache() { |
| 144 |
capacity = 64; |
| 145 |
} |
| 146 |
|
| 147 |
LRUCache(int p_capacity) { |
| 148 |
capacity = p_capacity; |
| 149 |
} |
| 150 |
}; |