ctkEALeastRecentlyUsedCacheMap.tpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. template<typename K, typename V>
  16. ctkEALeastRecentlyUsedCacheMap<K,V>::
  17. ctkEALeastRecentlyUsedCacheMap(int maxSize)
  18. : maxSize(maxSize)
  19. {
  20. if(0 >= maxSize)
  21. {
  22. throw std::invalid_argument("Size must be positive");
  23. }
  24. // We need one more entry then m_maxSize in the cache and a HashMap is
  25. // expanded when it reaches 3/4 of its size hence, the funny numbers.
  26. cache.reserve(((maxSize + 1) * 4)/3);
  27. }
  28. template<typename K, typename V>
  29. const V
  30. ctkEALeastRecentlyUsedCacheMap<K,V>::
  31. value(const K& key) const
  32. {
  33. QMutexLocker lock(&mutex);
  34. if (cache.contains(key))
  35. {
  36. history.removeAll(key);
  37. history.push_back(key);
  38. return cache.value(key);
  39. }
  40. else
  41. {
  42. return V();
  43. }
  44. }
  45. template<typename K, typename V>
  46. const V
  47. ctkEALeastRecentlyUsedCacheMap<K,V>::
  48. value(const K& key, const V& defaultValue) const
  49. {
  50. QMutexLocker lock(&mutex);
  51. if (cache.contains(key))
  52. {
  53. history.removeAll(key);
  54. history.push_back(key);
  55. return cache.value(key);
  56. }
  57. else
  58. {
  59. return defaultValue;
  60. }
  61. }
  62. template<typename K, typename V>
  63. void
  64. ctkEALeastRecentlyUsedCacheMap<K,V>::
  65. insert(const K& key, const V& value)
  66. {
  67. QMutexLocker lock(&mutex);
  68. if (cache.contains(key))
  69. {
  70. history.removeAll(key);
  71. }
  72. cache.insert(key, value);
  73. history.push_back(key);
  74. if(maxSize < cache.size())
  75. {
  76. cache.remove(history.takeFirst());
  77. }
  78. }
  79. template<typename K, typename V>
  80. const V
  81. ctkEALeastRecentlyUsedCacheMap<K,V>::
  82. remove(const K& key)
  83. {
  84. QMutexLocker lock(&mutex);
  85. if (cache.contains(key))
  86. {
  87. history.removeAll(key);
  88. }
  89. return cache.take(key);
  90. }
  91. template<typename K, typename V>
  92. int
  93. ctkEALeastRecentlyUsedCacheMap<K,V>::
  94. size() const
  95. {
  96. QMutexLocker lock(&mutex);
  97. return cache.size();
  98. }
  99. template<typename K, typename V>
  100. void
  101. ctkEALeastRecentlyUsedCacheMap<K,V>::
  102. clear()
  103. {
  104. QMutexLocker lock(&mutex);
  105. cache.clear();
  106. history.clear();
  107. }