19 #ifndef LIB_QUENTIER_UTILITY_LRU_CACHE_HPP 20 #define LIB_QUENTIER_UTILITY_LRU_CACHE_HPP 22 #include <quentier/utility/Macros.h> 29 template<
class Key,
class Value,
class Allocator = std::allocator<std::pair<Key, Value> > >
33 LRUCache(
const size_t maxSize = 100) :
41 typedef Value mapped_type;
42 typedef Allocator allocator_type;
43 typedef std::pair<key_type, mapped_type> value_type;
44 typedef std::list<value_type, allocator_type> container_type;
45 typedef typename container_type::size_type size_type;
46 typedef typename container_type::difference_type difference_type;
47 typedef typename container_type::iterator iterator;
48 typedef typename container_type::const_iterator const_iterator;
49 typedef std::reverse_iterator<iterator> reverse_iterator;
50 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
52 #if __cplusplus < 201103L 53 typedef typename allocator_type::reference reference;
54 typedef typename allocator_type::const_reference const_reference;
55 typedef typename allocator_type::pointer pointer;
56 typedef typename allocator_type::const_pointer const_pointer;
58 typedef value_type& reference;
59 typedef const value_type& const_reference;
60 typedef typename std::allocator_traits<allocator_type>::pointer pointer;
61 typedef typename std::allocator_traits<allocator_type>::const_pointer 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)
84 auto mapperIt = m_mapper.find(key);
85 if (mapperIt != m_mapper.end()) {
86 auto it = mapperIt.value();
87 Q_UNUSED(m_container.erase(it))
88 Q_UNUSED(m_mapper.erase(mapperIt))
89 if (m_currentSize != 0) {
94 m_container.push_front(value_type(key, value));
95 m_mapper[key] = m_container.begin();
98 if (m_currentSize > m_maxSize)
100 auto lastIt = m_container.end();
103 const key_type & lastElementKey = lastIt->first;
105 Q_UNUSED(m_mapper.remove(lastElementKey))
106 Q_UNUSED(m_container.erase(lastIt))
108 if (m_currentSize != 0) {
114 const mapped_type * get(
const key_type & key)
const 116 auto mapperIt = m_mapper.find(key);
117 if (mapperIt == m_mapper.end()) {
121 auto it = mapperIt.value();
122 m_container.splice(m_container.begin(), m_container, it);
123 mapperIt.value() = m_container.begin();
124 return &(mapperIt.value()->second);
127 bool exists(
const key_type & key)
129 auto mapperIt = m_mapper.find(key);
130 return (mapperIt != m_mapper.end());
133 bool remove(
const key_type & key)
135 auto mapperIt = m_mapper.find(key);
136 if (mapperIt == m_mapper.end()) {
140 auto it = mapperIt.value();
141 Q_UNUSED(m_container.erase(it))
142 Q_UNUSED(m_mapper.erase(mapperIt))
143 if (m_currentSize != 0) {
151 mutable container_type m_container;
152 size_t m_currentSize;
155 mutable QHash<Key, iterator> m_mapper;
160 #endif // LIB_QUENTIER_UTILITY_LRU_CACHE_HPP Definition: LRUCache.hpp:30