ctkEALeastRecentlyUsedCacheMap_p.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #ifndef CTKEALEASTRECENTLYUSEDCACHEMAP_P_H
  16. #define CTKEALEASTRECENTLYUSEDCACHEMAP_P_H
  17. #include <QHash>
  18. #include <QList>
  19. #include <QMutex>
  20. #include "ctkEACacheMap_p.h"
  21. /**
  22. * This class implements a least recently used cache map. It will hold
  23. * a given size of key-value pairs and drop the least recently used entry once this
  24. * size is reached. This class is thread safe.
  25. */
  26. template<typename K, typename V>
  27. class ctkEALeastRecentlyUsedCacheMap : public ctkEACacheMap<K,V, ctkEALeastRecentlyUsedCacheMap<K,V> >
  28. {
  29. private:
  30. // The internal lock for this object
  31. mutable QMutex mutex;
  32. // The max number of entries in the cache. Once reached entries are replaced
  33. const int maxSize;
  34. // The cache
  35. QHash<K,V> cache;
  36. // The history used to determine the least recently used entries. The end of the
  37. // list is the most recently used key. In other words history.front() returns
  38. // the least recently used key.
  39. mutable QList<K> history;
  40. public:
  41. typedef K KeyType;
  42. typedef V ValueType;
  43. /**
  44. * The constructor of the cache. The given max size will be used to determine the
  45. * size of the cache that triggers replacing least recently used entries with
  46. * new ones.
  47. *
  48. * @param maxSize The max number of entries in the cache
  49. */
  50. ctkEALeastRecentlyUsedCacheMap(int maxSize);
  51. /**
  52. * Returns the value for the key in case there is one. Additionally, the
  53. * LRU counter for the key is updated.
  54. *
  55. * @param key The key for the value to return
  56. *
  57. * @return The value of the key in case there is one, a default constructed value otherwise
  58. *
  59. */
  60. const V value(const K& key) const;
  61. /**
  62. * Returns the value for the key in case there is one. Additionally, the
  63. * LRU counter for the key is updated.
  64. *
  65. * @param key The key for the value to return
  66. * @param defaultValue The value to return in case the key does not exist.
  67. *
  68. * @return The value of the key in case there is one, the defaultValue otherwise.
  69. *
  70. */
  71. const V value(const K& key, const V& defaultValue) const;
  72. /**
  73. * Add the key-value pair to the cache. The key will be come the most recently
  74. * used entry. In case max size is (or has been) reached this will remove the
  75. * least recently used entry in the cache. In case that the cache already
  76. * contains this specific key-value pair it LRU counter is updated only.
  77. *
  78. * @param key The key for the value
  79. * @param value The value for the key
  80. */
  81. void insert(const K& key, const V& value);
  82. /**
  83. * Remove the entry denoted by key from the cache and return its value.
  84. *
  85. * @param key The key of the entry to be removed
  86. *
  87. * @return The value of the entry removed, a default-constructed value if none
  88. */
  89. const V remove(const K& key);
  90. /**
  91. * Return the current size of the cache.
  92. *
  93. * @return The number of entries currently in the cache.
  94. */
  95. int size() const;
  96. /**
  97. * Remove all entries from the cache.
  98. */
  99. void clear();
  100. };
  101. #include "ctkEALeastRecentlyUsedCacheMap.tpp"
  102. #endif // CTKEALEASTRECENTLYUSEDCACHEMAP_P_H