lru.h 5.0 KB

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