Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/templates/lru.h
9973 views
1
/**************************************************************************/
2
/* lru.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
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
};
151
152