Lucene++ - a full-featured, c++ search engine
API Documentation
Main Page
Related Pages
Namespaces
Data Structures
Files
File List
Globals
All
Data Structures
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
Pages
include
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
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
clucene.sourceforge.net