lru.h 4.0 KB

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