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


Loading...
Searching...
No Matches
SimpleLRUCache.h
Go to the documentation of this file.
1
2// 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
13namespace Lucene {
14
18template <class KEY, class VALUE, class HASH, class EQUAL>
20public:
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
28 this->cacheSize = cacheSize;
29 }
30
31 virtual ~SimpleLRUCache() {
32 }
33
34protected:
35 int32_t cacheSize;
38
39public:
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
73 return cacheList.begin();
74 }
75
77 return cacheList.end();
78 }
79};
80
81};
82
83#endif
Base class for all Lucene classes.
Definition LuceneObject.h:31
General purpose LRU cache map. Accessing an entry will keep the entry cached. get(const KEY&) and put...
Definition SimpleLRUCache.h:19
key_list::const_iterator const_iterator
Definition SimpleLRUCache.h:23
void put(const KEY &key, const VALUE &value)
Definition SimpleLRUCache.h:40
int32_t size() const
Definition SimpleLRUCache.h:68
map_type cacheMap
Definition SimpleLRUCache.h:37
map_type::const_iterator map_iterator
Definition SimpleLRUCache.h:25
key_list cacheList
Definition SimpleLRUCache.h:36
std::pair< KEY, VALUE > key_value
Definition SimpleLRUCache.h:21
bool contains(const KEY &key) const
Definition SimpleLRUCache.h:64
const_iterator end() const
Definition SimpleLRUCache.h:76
boost::unordered_map< KEY, typename key_list::iterator, HASH, EQUAL > map_type
Definition SimpleLRUCache.h:24
std::list< key_value > key_list
Definition SimpleLRUCache.h:22
const_iterator begin() const
Definition SimpleLRUCache.h:72
SimpleLRUCache(int32_t cacheSize)
Definition SimpleLRUCache.h:27
virtual ~SimpleLRUCache()
Definition SimpleLRUCache.h:31
VALUE get(const KEY &key)
Definition SimpleLRUCache.h:50
int32_t cacheSize
Definition SimpleLRUCache.h:35
Definition AbstractAllTermDocs.h:12

clucene.sourceforge.net