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
// Copyright (c) 2009-2011 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
{
21
public
:
22
typedef
std::pair<KEY, VALUE>
key_value
;
23
typedef
std::list< key_value >
key_list
;
24
typedef
typename
key_list::const_iterator
const_iterator
;
25
typedef
boost::unordered_map< KEY, typename key_list::iterator, HASH, EQUAL, LuceneAllocator< std::pair<KEY, typename key_list::iterator> > >
map_type
;
26
typedef
typename
map_type::const_iterator
map_iterator
;
27
28
SimpleLRUCache
(int32_t
cacheSize
)
29
{
30
this->cacheSize =
cacheSize
;
31
}
32
33
virtual
~SimpleLRUCache
()
34
{
35
}
36
37
protected
:
38
int32_t
cacheSize
;
39
key_list
cacheList
;
40
map_type
cacheMap
;
41
42
public
:
43
void
put
(
const
KEY& key,
const
VALUE& value)
44
{
45
cacheList
.push_front(std::make_pair(key, value));
46
cacheMap
[key] =
cacheList
.begin();
47
48
if
((int32_t)
cacheList
.size() >
cacheSize
)
49
{
50
cacheMap
.erase(
cacheList
.back().first);
51
cacheList
.pop_back();
52
}
53
}
54
55
VALUE
get
(
const
KEY& key)
56
{
57
map_iterator
find =
cacheMap
.find(key);
58
if
(find ==
cacheMap
.end())
59
return
VALUE();
60
61
VALUE value(find->second->second);
62
cacheList
.erase(find->second);
63
cacheList
.push_front(std::make_pair(key, value));
64
cacheMap
[key] =
cacheList
.begin();
65
66
return
value;
67
}
68
69
bool
contains
(
const
KEY& key)
const
70
{
71
return
(
cacheMap
.find(key) !=
cacheMap
.end());
72
}
73
74
int32_t
size
()
const
75
{
76
return
(int32_t)
cacheList
.size();
77
}
78
79
const_iterator
begin
()
const
80
{
81
return
cacheList
.begin();
82
}
83
84
const_iterator
end
()
const
85
{
86
return
cacheList
.end();
87
}
88
};
89
};
90
91
#endif
clucene.sourceforge.net