21 #ifndef DDC_LRUCACHE_H 22 #define DDC_LRUCACHE_H 24 #include "../CommonLib/ddcConfig.h" 30 # define DDC_IFPTHREAD(x) x 31 # define DDC_IFPTHREADELSE(x,y) x 33 # define DDC_IFPTHREAD(x) 34 # define DDC_IFPTHREADELSE(x,y) y 41 #define DDC_DEFAULT_LRU_CACHE_CAPACITY 1024 45 template<
class Key,
class Val>
58 typedef pthread_mutex_t*
MutexT;
66 typedef std::pair<KeyT,ValT>
ItemT;
72 typedef std::map<KeyT, typename ListT::iterator>
MapT;
124 while (m_size > m_capacity) {
125 const ItemT &lru_item = m_list.back();
126 m_map.erase(lru_item.first);
141 {
return m_mutex=mut; };
164 {
DDC_IFPTHREAD(
if (m_mutex) pthread_mutex_lock(m_mutex)); };
168 {
DDC_IFPTHREAD(
if (m_mutex) pthread_mutex_unlock(m_mutex)); };
173 inline iterator
begin() {
return m_map.begin(); };
174 inline iterator
end() {
return m_map.end(); };
175 inline const_iterator
begin()
const {
return m_map.begin(); };
176 inline const_iterator
end()
const {
return m_map.end(); };
182 inline iterator
find(
const KeyT &key)
183 {
return m_map.find(key); };
187 {
return m_map.lower_bound(key); };
191 {
return m_map.upper_bound(key); };
194 inline const_iterator
find(
const KeyT &key)
const 195 {
return m_map.find(key); };
199 {
return m_map.lower_bound(key); };
203 {
return m_map.upper_bound(key); };
206 inline iterator
get(
const KeyT &key)
208 iterator mapi = m_map.find(key);
209 if (mapi != m_map.end() && mapi->second != m_list.begin())
216 {
if (it->second != m_list.begin()) m_list.splice(m_list.begin(),
m_list, it->second); };
220 {
if (it->second != m_list.begin()) m_list.splice(m_list.begin(),
m_list, it->second); };
226 void insert(
const KeyT &key,
const ValT &val)
228 iterator mapi =
get(key);
229 if (mapi != m_map.end()) {
231 mapi->second->second = val;
233 else if (m_capacity > 0) {
235 m_list.push_front(std::make_pair(key,val));
236 m_map.insert(std::make_pair(key, m_list.begin()));
MapT::iterator iterator
iterator wraps MapT::iterator (~ pair< KeyT, pair<KeyT,ValT>* > *)
Definition: LRUCache.h:75
const_iterator begin() const
Definition: LRUCache.h:175
std::map< KeyT, typename ListT::iterator > MapT
maps keys to lru-list iterators, used to check key existence
Definition: LRUCache.h:72
iterator upper_bound(const KeyT &key)
find upper bound for cache key; does NOT update the MRU-list (non-const)
Definition: LRUCache.h:190
pthread_mutex_t * MutexT
mutex type (pthreads enabled)
Definition: LRUCache.h:59
const_iterator lower_bound(const KeyT &key) const
find lower bound for cache key; does NOT update the MRU-list (const version)
Definition: LRUCache.h:198
iterator lower_bound(const KeyT &key)
find lower bound for cache key; does NOT update the MRU-list (non-const)
Definition: LRUCache.h:186
MapT m_map
maps keys to m_list iterators, used to check key existence
Definition: LRUCache.h:94
std::pair< KeyT, ValT > ItemT
elementary data item: (key,val) pair
Definition: LRUCache.h:66
generic least-recently-used cache template
Definition: LRUCache.h:46
void insert(const KeyT &key, const ValT &val)
Definition: LRUCache.h:226
MutexT mutex() const
get mutex pointer
Definition: LRUCache.h:136
size_t capacity() const
get cache capacity
Definition: LRUCache.h:148
const_iterator find(const KeyT &key) const
find cached item; returns end() if not found; does NOT update the MRU-list (const version) ...
Definition: LRUCache.h:194
ListT m_list
list of (key,val) pairs, position indicates recency of use (most recent first)
Definition: LRUCache.h:91
iterator end()
Definition: LRUCache.h:174
const_iterator upper_bound(const KeyT &key) const
find upper bound for cache key; does NOT update the MRU-list (const version)
Definition: LRUCache.h:202
void lock()
lock the cache mutex, if any
Definition: LRUCache.h:163
#define DDC_DEFAULT_LRU_CACHE_CAPACITY
default cache capacity
Definition: LRUCache.h:41
void clear()
clear all cached items (no implicit locks)
Definition: LRUCache.h:113
void unlock()
unlock cache mutex
Definition: LRUCache.h:167
size_t reserve(size_t cap)
update cache capacity; implicitly calls clean()
Definition: LRUCache.h:152
size_t m_capacity
maximum number of cached items
Definition: LRUCache.h:85
size_t m_size
current number of cached items
Definition: LRUCache.h:88
#define DDC_IFPTHREAD(x)
Definition: LRUCache.h:30
size_t size() const
get number of currently cached items
Definition: LRUCache.h:144
MutexT m_mutex
mutex pointer to use for lock() and unlock() methods
Definition: LRUCache.h:97
MapT::const_iterator const_iterator
const_iterator wraps MapT::const_iterator
Definition: LRUCache.h:78
MutexT mutex(MutexT mut)
set mutex pointer
Definition: LRUCache.h:140
ddcLRUCache(size_t capacity=1024, MutexT mutex=NULL)
default constructor
Definition: LRUCache.h:105
void promote(const_iterator &it)
promote an existing item to the most-recently-used position
Definition: LRUCache.h:215
iterator find(const KeyT &key)
find cached item; returns end() if not found, does NOT update the MRU-list (non-const) ...
Definition: LRUCache.h:182
Key KeyT
key type
Definition: LRUCache.h:52
void promote(iterator &it)
promote an existing item to the most-recently-used position
Definition: LRUCache.h:219
Val ValT
value type
Definition: LRUCache.h:55
iterator begin()
Definition: LRUCache.h:173
std::list< ItemT > ListT
list of (key,val) pairs, position indicates recency of use (most recent first)
Definition: LRUCache.h:69
const_iterator end() const
Definition: LRUCache.h:176
void clean()
drop least recently used cache items until size <= capacity
Definition: LRUCache.h:122