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#ifndef LRU_H
32#define LRU_H
33
34#include "core/math/math_funcs.h"
35#include "hash_map.h"
36#include "list.h"
37
38template <class TKey, class TData, class Hasher = HashMapHasherDefault, class Comparator = HashMapComparatorDefault<TKey>>
39class LRUCache {
40private:
41 struct Pair {
42 TKey key;
43 TData data;
44
45 Pair() {}
46 Pair(const TKey &p_key, const TData &p_data) :
47 key(p_key),
48 data(p_data) {
49 }
50 };
51
52 typedef typename List<Pair>::Element *Element;
53
54 List<Pair> _list;
55 HashMap<TKey, Element, Hasher, Comparator> _map;
56 size_t capacity;
57
58public:
59 const TData *insert(const TKey &p_key, const TData &p_value) {
60 Element *e = _map.getptr(p_key);
61 Element n = _list.push_front(Pair(p_key, p_value));
62
63 if (e) {
64 _list.erase(*e);
65 _map.erase(p_key);
66 }
67 _map[p_key] = _list.front();
68
69 while (_map.size() > capacity) {
70 Element d = _list.back();
71 _map.erase(d->get().key);
72 _list.pop_back();
73 }
74
75 return &n->get().data;
76 }
77
78 void clear() {
79 _map.clear();
80 _list.clear();
81 }
82
83 bool has(const TKey &p_key) const {
84 return _map.getptr(p_key);
85 }
86
87 const TData &get(const TKey &p_key) {
88 Element *e = _map.getptr(p_key);
89 CRASH_COND(!e);
90 _list.move_to_front(*e);
91 return (*e)->get().data;
92 };
93
94 const TData *getptr(const TKey &p_key) {
95 Element *e = _map.getptr(p_key);
96 if (!e) {
97 return nullptr;
98 } else {
99 _list.move_to_front(*e);
100 return &(*e)->get().data;
101 }
102 }
103
104 _FORCE_INLINE_ size_t get_capacity() const { return capacity; }
105 _FORCE_INLINE_ size_t get_size() const { return _map.size(); }
106
107 void set_capacity(size_t p_capacity) {
108 if (capacity > 0) {
109 capacity = p_capacity;
110 while (_map.size() > capacity) {
111 Element d = _list.back();
112 _map.erase(d->get().key);
113 _list.pop_back();
114 }
115 }
116 }
117
118 LRUCache() {
119 capacity = 64;
120 }
121
122 LRUCache(int p_capacity) {
123 capacity = p_capacity;
124 }
125};
126
127#endif // LRU_H
128