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