Lucene++ - a full-featured, c++ search engine
API Documentation

SimpleLRUCache.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2014 Alan Wright. All rights reserved.
3 // Distributable under the terms of either the Apache License (Version 2.0)
4 // or the GNU Lesser General Public License.
6
7 #ifndef SIMPLELRUCACHE_H
8 #define SIMPLELRUCACHE_H
9
10 #include <list>
11 #include "LuceneObject.h"
12
13 namespace Lucene {
14
18 template <class KEY, class VALUE, class HASH, class EQUAL>
19 class SimpleLRUCache : public LuceneObject {
20 public:
21 typedef std::pair<KEY, VALUE> key_value;
22 typedef std::list< key_value > key_list;
23 typedef typename key_list::const_iterator const_iterator;
24 typedef boost::unordered_map<KEY, typename key_list::iterator, HASH, EQUAL> map_type;
25 typedef typename map_type::const_iterator map_iterator;
26
27 SimpleLRUCache(int32_t cacheSize) {
28 this->cacheSize = cacheSize;
29 }
30
31 virtual ~SimpleLRUCache() {
32 }
33
34 protected:
35 int32_t cacheSize;
36 key_list cacheList;
37 map_type cacheMap;
38
39 public:
40 void put(const KEY& key, const VALUE& value) {
41 cacheList.push_front(std::make_pair(key, value));
42 cacheMap[key] = cacheList.begin();
43
44 if ((int32_t)cacheList.size() > cacheSize) {
45 cacheMap.erase(cacheList.back().first);
46 cacheList.pop_back();
47 }
48 }
49
50 VALUE get(const KEY& key) {
51 map_iterator find = cacheMap.find(key);
52 if (find == cacheMap.end()) {
53 return VALUE();
54 }
55
56 VALUE value(find->second->second);
57 cacheList.erase(find->second);
58 cacheList.push_front(std::make_pair(key, value));
59 cacheMap[key] = cacheList.begin();
60
61 return value;
62 }
63
64 bool contains(const KEY& key) const {
65 return (cacheMap.find(key) != cacheMap.end());
66 }
67
68 int32_t size() const {
69 return (int32_t)cacheList.size();
70 }
71
72 const_iterator begin() const {
73 return cacheList.begin();
74 }
75
76 const_iterator end() const {
77 return cacheList.end();
78 }
79 };
80
81 };
82
83 #endif
Lucene::SimpleLRUCache::const_iterator
key_list::const_iterator const_iterator
Definition: SimpleLRUCache.h:23
Lucene::SimpleLRUCache::map_type
boost::unordered_map< KEY, typename key_list::iterator, HASH, EQUAL > map_type
Definition: SimpleLRUCache.h:24
Lucene::SimpleLRUCache::key_list
std::list< key_value > key_list
Definition: SimpleLRUCache.h:22
Lucene::SimpleLRUCache::SimpleLRUCache
SimpleLRUCache(int32_t cacheSize)
Definition: SimpleLRUCache.h:27
Lucene::SimpleLRUCache::cacheSize
int32_t cacheSize
Definition: SimpleLRUCache.h:35
Lucene::SimpleLRUCache::~SimpleLRUCache
virtual ~SimpleLRUCache()
Definition: SimpleLRUCache.h:31
Lucene::SimpleLRUCache::cacheList
key_list cacheList
Definition: SimpleLRUCache.h:36
Lucene::SimpleLRUCache::cacheMap
map_type cacheMap
Definition: SimpleLRUCache.h:37
Lucene::SimpleLRUCache::put
void put(const KEY &key, const VALUE &value)
Definition: SimpleLRUCache.h:40
Lucene::SimpleLRUCache::end
const_iterator end() const
Definition: SimpleLRUCache.h:76
Lucene
Definition: AbstractAllTermDocs.h:12
Lucene::SimpleLRUCache::contains
bool contains(const KEY &key) const
Definition: SimpleLRUCache.h:64
Lucene::SimpleLRUCache::get
VALUE get(const KEY &key)
Definition: SimpleLRUCache.h:50
Lucene::LuceneObject
Base class for all Lucene classes.
Definition: LuceneObject.h:31
Lucene::SimpleLRUCache
General purpose LRU cache map. Accessing an entry will keep the entry cached. get(const KEY&) and put...
Definition: SimpleLRUCache.h:19
Lucene::SimpleLRUCache::key_value
std::pair< KEY, VALUE > key_value
Definition: SimpleLRUCache.h:21
Lucene::SimpleLRUCache::map_iterator
map_type::const_iterator map_iterator
Definition: SimpleLRUCache.h:25
Lucene::SimpleLRUCache::begin
const_iterator begin() const
Definition: SimpleLRUCache.h:72
Lucene::SimpleLRUCache::size
int32_t size() const
Definition: SimpleLRUCache.h:68
LuceneObject.h

clucene.sourceforge.net