-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathcachemap.h
203 lines (165 loc) · 4.17 KB
/
cachemap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright (c) 2014-2017 The Dash Core developers
// Copyright (c) 2019 Limxtec developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCORE_CACHEMAP_H
#define BITCORE_CACHEMAP_H
#include <map>
#include <list>
#include <cstddef>
#include <serialize.h>
/**
* Serializable structure for key/value items
*/
template<typename K, typename V>
struct CacheItem
{
CacheItem()
{}
CacheItem(const K& keyIn, const V& valueIn)
: key(keyIn),
value(valueIn)
{}
K key;
V value;
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(key);
READWRITE(value);
}
};
/**
* Map like container that keeps the N most recently added items
*/
template<typename K, typename V, typename Size = uint32_t>
class CacheMap
{
public:
typedef Size size_type;
typedef CacheItem<K,V> item_t;
typedef std::list<item_t> list_t;
typedef typename list_t::iterator list_it;
typedef typename list_t::const_iterator list_cit;
typedef std::map<K, list_it> map_t;
typedef typename map_t::iterator map_it;
typedef typename map_t::const_iterator map_cit;
private:
size_type nMaxSize;
size_type nCurrentSize;
list_t listItems;
map_t mapIndex;
public:
CacheMap(size_type nMaxSizeIn = 0)
: nMaxSize(nMaxSizeIn),
nCurrentSize(0),
listItems(),
mapIndex()
{}
CacheMap(const CacheMap<K,V>& other)
: nMaxSize(other.nMaxSize),
nCurrentSize(other.nCurrentSize),
listItems(other.listItems),
mapIndex()
{
RebuildIndex();
}
void Clear()
{
mapIndex.clear();
listItems.clear();
nCurrentSize = 0;
}
void SetMaxSize(size_type nMaxSizeIn)
{
nMaxSize = nMaxSizeIn;
}
size_type GetMaxSize() const {
return nMaxSize;
}
size_type GetSize() const {
return nCurrentSize;
}
void Insert(const K& key, const V& value)
{
map_it it = mapIndex.find(key);
if(it != mapIndex.end()) {
item_t& item = *(it->second);
item.value = value;
return;
}
if(nCurrentSize == nMaxSize) {
PruneLast();
}
listItems.push_front(item_t(key, value));
mapIndex[key] = listItems.begin();
++nCurrentSize;
}
bool HasKey(const K& key) const
{
map_cit it = mapIndex.find(key);
return (it != mapIndex.end());
}
bool Get(const K& key, V& value) const
{
map_cit it = mapIndex.find(key);
if(it == mapIndex.end()) {
return false;
}
item_t& item = *(it->second);
value = item.value;
return true;
}
void Erase(const K& key)
{
map_it it = mapIndex.find(key);
if(it == mapIndex.end()) {
return;
}
listItems.erase(it->second);
mapIndex.erase(it);
--nCurrentSize;
}
const list_t& GetItemList() const {
return listItems;
}
CacheMap<K,V>& operator=(const CacheMap<K,V>& other)
{
nMaxSize = other.nMaxSize;
nCurrentSize = other.nCurrentSize;
listItems = other.listItems;
RebuildIndex();
return *this;
}
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(nMaxSize);
READWRITE(nCurrentSize);
READWRITE(listItems);
if(ser_action.ForRead()) {
RebuildIndex();
}
}
private:
void PruneLast()
{
if(nCurrentSize < 1) {
return;
}
item_t& item = listItems.back();
mapIndex.erase(item.key);
listItems.pop_back();
--nCurrentSize;
}
void RebuildIndex()
{
mapIndex.clear();
for(list_it it = listItems.begin(); it != listItems.end(); ++it) {
mapIndex[it->key] = it;
}
}
};
#endif // BITCORE_CACHEMAP_H