lru.h 5.1 KB

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