19 #ifndef LIB_QUENTIER_UTILITY_LRU_CACHE_HPP 20 #define LIB_QUENTIER_UTILITY_LRU_CACHE_HPP 22 #include <quentier/utility/Macros.h> 34 class Allocator = std::allocator<std::pair<Key, Value>>>
38 LRUCache(
const size_t maxSize = 100) :
46 using mapped_type = Value;
47 using allocator_type = Allocator;
48 using value_type = std::pair<key_type, mapped_type>;
49 using container_type = std::list<value_type, allocator_type>;
50 using size_type =
typename container_type::size_type;
51 using difference_type =
typename container_type::difference_type;
52 using iterator =
typename container_type::iterator;
53 using const_iterator =
typename container_type::const_iterator;
54 using reverse_iterator = std::reverse_iterator<iterator>;
55 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
57 using reference = value_type&;
58 using const_reference =
const value_type&;
59 using pointer =
typename std::allocator_traits<allocator_type>::pointer;
62 typename std::allocator_traits<allocator_type>::const_pointer;
64 iterator begin() {
return m_container.begin(); }
65 const_iterator begin()
const {
return m_container.begin(); }
67 reverse_iterator rbegin() {
return m_container.rbegin(); }
68 const_reverse_iterator rbegin()
const {
return m_container.rbegin(); }
70 iterator end() {
return m_container.end(); }
71 const_iterator end()
const {
return m_container.end(); }
73 reverse_iterator rend() {
return m_container.rend(); }
74 const_reverse_iterator rend()
const {
return m_container.rend(); }
76 bool empty()
const {
return m_container.empty(); }
77 size_t size()
const {
return m_currentSize; }
78 size_t max_size()
const {
return m_maxSize; }
80 void clear() { m_container.clear(); m_mapper.clear(); m_currentSize = 0; }
82 void put(
const key_type & key,
const mapped_type & value)
86 m_container.push_front(value_type(key, value));
87 m_mapper[key] = m_container.begin();
93 const mapped_type *
get(
const key_type & key)
const 95 auto mapperIt = m_mapper.find(key);
96 if (mapperIt == m_mapper.end()) {
100 auto it = mapperIt.value();
101 if (it == m_container.end()) {
105 m_container.splice(m_container.begin(), m_container, it);
106 mapperIt.value() = m_container.begin();
107 return &(mapperIt.value()->second);
110 bool exists(
const key_type & key)
112 auto mapperIt = m_mapper.find(key);
113 if (mapperIt == m_mapper.end()) {
117 auto it = mapperIt.value();
118 return (it != m_container.end());
121 bool remove(
const key_type & key)
123 auto mapperIt = m_mapper.find(key);
124 if (mapperIt == m_mapper.end()) {
128 auto it = mapperIt.value();
129 Q_UNUSED(m_container.erase(it))
130 Q_UNUSED(m_mapper.erase(mapperIt))
132 if (m_currentSize != 0) {
139 void setMaxSize(
const size_t maxSize)
141 if (maxSize >= m_maxSize) {
146 size_t diff = m_maxSize - maxSize;
147 for(
size_t i = 0; (i < diff) && !m_container.empty(); ++i)
149 auto lastIt = m_container.end();
152 const key_type & lastElementKey = lastIt->first;
153 Q_UNUSED(m_mapper.remove(lastElementKey))
154 Q_UNUSED(m_container.erase(lastIt))
156 if (m_currentSize != 0) {
165 if (m_currentSize <= m_maxSize) {
169 if (Q_UNLIKELY(m_container.empty())) {
173 auto lastIt = m_container.end();
176 const key_type & lastElementKey = lastIt->first;
178 Q_UNUSED(m_mapper.remove(lastElementKey))
179 Q_UNUSED(m_container.erase(lastIt))
181 if (m_currentSize != 0) {
187 mutable container_type m_container;
188 size_t m_currentSize;
191 mutable QHash<Key, iterator> m_mapper;
196 #endif // LIB_QUENTIER_UTILITY_LRU_CACHE_HPP Definition: DecryptedTextManager.h:26
Definition: LRUCache.hpp:35