Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

cachemap.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2020 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)
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 
72 public:
73  explicit CacheMap(size_type nMaxSizeIn = 0)
74  : nMaxSize(nMaxSizeIn),
75  listItems(),
76  mapIndex()
77  {}
78 
79  CacheMap(const CacheMap<K,V>& other)
80  : nMaxSize(other.nMaxSize),
81  listItems(other.listItems),
82  mapIndex()
83  {
84  RebuildIndex();
85  }
86 
87  void Clear()
88  {
89  mapIndex.clear();
90  listItems.clear();
91  }
92 
93  void SetMaxSize(size_type nMaxSizeIn)
94  {
95  nMaxSize = nMaxSizeIn;
96  }
97 
99  return nMaxSize;
100  }
101 
102  size_type GetSize() const {
103  return listItems.size();
104  }
105 
106  bool Insert(const K& key, const V& value)
107  {
108  if(mapIndex.find(key) != mapIndex.end()) {
109  return false;
110  }
111  if(listItems.size() == nMaxSize) {
112  PruneLast();
113  }
114  listItems.push_front(item_t(key, value));
115  mapIndex.emplace(key, listItems.begin());
116  return true;
117  }
118 
119  bool HasKey(const K& key) const
120  {
121  return (mapIndex.find(key) != mapIndex.end());
122  }
123 
124  bool Get(const K& key, V& value) const
125  {
126  map_cit it = mapIndex.find(key);
127  if(it == mapIndex.end()) {
128  return false;
129  }
130  item_t& item = *(it->second);
131  value = item.value;
132  return true;
133  }
134 
135  void Erase(const K& key)
136  {
137  map_it it = mapIndex.find(key);
138  if(it == mapIndex.end()) {
139  return;
140  }
141  listItems.erase(it->second);
142  mapIndex.erase(it);
143  }
144 
145  const list_t& GetItemList() const {
146  return listItems;
147  }
148 
150  {
151  nMaxSize = other.nMaxSize;
152  listItems = other.listItems;
153  RebuildIndex();
154  return *this;
155  }
156 
158 
159  template <typename Stream, typename Operation>
160  inline void SerializationOp(Stream& s, Operation ser_action)
161  {
164  if(ser_action.ForRead()) {
165  RebuildIndex();
166  }
167  }
168 
169 private:
170  void PruneLast()
171  {
172  if(listItems.empty()) {
173  return;
174  }
175  item_t& item = listItems.back();
176  mapIndex.erase(item.key);
177  listItems.pop_back();
178  }
179 
181  {
182  mapIndex.clear();
183  for(list_it it = listItems.begin(); it != listItems.end(); ++it) {
184  mapIndex.emplace(it->key, it);
185  }
186  }
187 };
188 
189 #endif /* CACHEMAP_H_ */
std::list< item_t > list_t
Definition: cachemap.h:53
ADD_SERIALIZE_METHODS
Definition: cachemap.h:157
bool HasKey(const K &key) const
Definition: cachemap.h:119
#define READWRITE(obj)
Definition: serialize.h:165
const list_t & GetItemList() const
Definition: cachemap.h:145
list_t::iterator list_it
Definition: cachemap.h:55
CacheItem< K, V > item_t
Definition: cachemap.h:51
size_type GetMaxSize() const
Definition: cachemap.h:98
map_t::iterator map_it
Definition: cachemap.h:61
void PruneLast()
Definition: cachemap.h:170
void SerializationOp(Stream &s, Operation ser_action)
Definition: cachemap.h:34
CacheMap(size_type nMaxSizeIn=0)
Definition: cachemap.h:73
void Clear()
Definition: cachemap.h:87
CacheItem(const K &keyIn, const V &valueIn)
Definition: cachemap.h:23
list_t listItems
Definition: cachemap.h:68
bool Get(const K &key, V &value) const
Definition: cachemap.h:124
void Erase(const K &key)
Definition: cachemap.h:135
Map like container that keeps the N most recently added items.
Definition: cachemap.h:46
ADD_SERIALIZE_METHODS
Definition: cachemap.h:31
Serializable structure for key/value items.
Definition: cachemap.h:18
map_t mapIndex
Definition: cachemap.h:70
void SetMaxSize(size_type nMaxSizeIn)
Definition: cachemap.h:93
list_t::const_iterator list_cit
Definition: cachemap.h:57
CacheItem()
Definition: cachemap.h:20
void SerializationOp(Stream &s, Operation ser_action)
Definition: cachemap.h:160
bool Insert(const K &key, const V &value)
Definition: cachemap.h:106
size_type GetSize() const
Definition: cachemap.h:102
map_t::const_iterator map_cit
Definition: cachemap.h:63
std::map< K, list_it > map_t
Definition: cachemap.h:59
void RebuildIndex()
Definition: cachemap.h:180
CacheMap< K, V > & operator=(const CacheMap< K, V > &other)
Definition: cachemap.h:149
size_type nMaxSize
Definition: cachemap.h:66
Size size_type
Definition: cachemap.h:49
CacheMap(const CacheMap< K, V > &other)
Definition: cachemap.h:79
Released under the MIT license