Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

cachemultimap.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2019 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 CACHEMULTIMAP_H_
6 #define CACHEMULTIMAP_H_
7 
8 #include <cstddef>
9 #include <map>
10 #include <list>
11 #include <set>
12 
13 #include <serialize.h>
14 
15 #include <cachemap.h>
16 
20 template<typename K, typename V, typename Size = uint32_t>
22 {
23 public:
24  typedef Size size_type;
25 
27 
28  typedef std::list<item_t> list_t;
29 
30  typedef typename list_t::iterator list_it;
31 
32  typedef typename list_t::const_iterator list_cit;
33 
34  typedef std::map<V,list_it> it_map_t;
35 
36  typedef typename it_map_t::iterator it_map_it;
37 
38  typedef typename it_map_t::const_iterator it_map_cit;
39 
40  typedef std::map<K, it_map_t> map_t;
41 
42  typedef typename map_t::iterator map_it;
43 
44  typedef typename map_t::const_iterator map_cit;
45 
46 private:
48 
50 
52 
53 public:
54  CacheMultiMap(size_type nMaxSizeIn = 0)
55  : nMaxSize(nMaxSizeIn),
56  listItems(),
57  mapIndex()
58  {}
59 
61  : nMaxSize(other.nMaxSize),
62  listItems(other.listItems),
63  mapIndex()
64  {
65  RebuildIndex();
66  }
67 
68  void Clear()
69  {
70  mapIndex.clear();
71  listItems.clear();
72  }
73 
74  void SetMaxSize(size_type nMaxSizeIn)
75  {
76  nMaxSize = nMaxSizeIn;
77  }
78 
80  return nMaxSize;
81  }
82 
83  size_type GetSize() const {
84  return listItems.size();
85  }
86 
87  bool Insert(const K& key, const V& value)
88  {
89  map_it mit = mapIndex.find(key);
90  if(mit == mapIndex.end()) {
91  mit = mapIndex.emplace(key, it_map_t()).first;
92  }
93  it_map_t& mapIt = mit->second;
94 
95  if(mapIt.count(value) > 0) {
96  // Don't insert duplicates
97  return false;
98  }
99 
100  if(listItems.size() == nMaxSize) {
101  PruneLast();
102  }
103  listItems.push_front(item_t(key, value));
104  mapIt.emplace(value, listItems.begin());
105  return true;
106  }
107 
108  bool HasKey(const K& key) const
109  {
110  return (mapIndex.find(key) != mapIndex.end());
111  }
112 
113  bool Get(const K& key, V& value) const
114  {
115  map_cit it = mapIndex.find(key);
116  if(it == mapIndex.end()) {
117  return false;
118  }
119  const it_map_t& mapIt = it->second;
120  const item_t& item = *(mapIt.begin()->second);
121  value = item.value;
122  return true;
123  }
124 
125  bool GetAll(const K& key, std::vector<V>& vecValues)
126  {
127  map_cit mit = mapIndex.find(key);
128  if(mit == mapIndex.end()) {
129  return false;
130  }
131  const it_map_t& mapIt = mit->second;
132 
133  for(it_map_cit it = mapIt.begin(); it != mapIt.end(); ++it) {
134  const item_t& item = *(it->second);
135  vecValues.push_back(item.value);
136  }
137  return true;
138  }
139 
140  void GetKeys(std::vector<K>& vecKeys)
141  {
142  for(map_cit it = mapIndex.begin(); it != mapIndex.end(); ++it) {
143  vecKeys.push_back(it->first);
144  }
145  }
146 
147  void Erase(const K& key)
148  {
149  map_it mit = mapIndex.find(key);
150  if(mit == mapIndex.end()) {
151  return;
152  }
153  it_map_t& mapIt = mit->second;
154 
155  for(it_map_it it = mapIt.begin(); it != mapIt.end(); ++it) {
156  listItems.erase(it->second);
157  }
158 
159  mapIndex.erase(mit);
160  }
161 
162  void Erase(const K& key, const V& value)
163  {
164  map_it mit = mapIndex.find(key);
165  if(mit == mapIndex.end()) {
166  return;
167  }
168  it_map_t& mapIt = mit->second;
169 
170  it_map_it it = mapIt.find(value);
171  if(it == mapIt.end()) {
172  return;
173  }
174 
175  listItems.erase(it->second);
176  mapIt.erase(it);
177 
178  if(mapIt.empty()) {
179  mapIndex.erase(mit);
180  }
181  }
182 
183  const list_t& GetItemList() const {
184  return listItems;
185  }
186 
188  {
189  nMaxSize = other.nMaxSize;
190  listItems = other.listItems;
191  RebuildIndex();
192  return *this;
193  }
194 
196 
197  template <typename Stream, typename Operation>
198  inline void SerializationOp(Stream& s, Operation ser_action)
199  {
202  if(ser_action.ForRead()) {
203  RebuildIndex();
204  }
205  }
206 
207 private:
208  void PruneLast()
209  {
210  if(listItems.empty()) {
211  return;
212  }
213 
214  list_it lit = listItems.end();
215  --lit;
216  item_t& item = *lit;
217 
218  map_it mit = mapIndex.find(item.key);
219 
220  if(mit != mapIndex.end()) {
221  it_map_t& mapIt = mit->second;
222 
223  mapIt.erase(item.value);
224 
225  if(mapIt.empty()) {
226  mapIndex.erase(item.key);
227  }
228  }
229 
230  listItems.pop_back();
231  }
232 
234  {
235  mapIndex.clear();
236  for(list_it lit = listItems.begin(); lit != listItems.end(); ++lit) {
237  item_t& item = *lit;
238  map_it mit = mapIndex.find(item.key);
239  if(mit == mapIndex.end()) {
240  mit = mapIndex.emplace(item.key, it_map_t()).first;
241  }
242  it_map_t& mapIt = mit->second;
243  mapIt.emplace(item.value, lit);
244  }
245  }
246 };
247 
248 #endif /* CACHEMULTIMAP_H_ */
std::map< V, list_it > it_map_t
Definition: cachemultimap.h:34
Map like container that keeps the N most recently added items.
Definition: cachemultimap.h:21
CacheMap< K, V > & operator=(const CacheMap< K, V > &other)
#define READWRITE(obj)
Definition: serialize.h:165
void RebuildIndex()
it_map_t::iterator it_map_it
Definition: cachemultimap.h:36
void Erase(const K &key, const V &value)
CacheMultiMap(size_type nMaxSizeIn=0)
Definition: cachemultimap.h:54
std::list< item_t > list_t
Definition: cachemultimap.h:28
bool Get(const K &key, V &value) const
list_t listItems
Definition: cachemap.h:68
void Erase(const K &key)
list_t listItems
Definition: cachemultimap.h:49
Map like container that keeps the N most recently added items.
Definition: cachemap.h:46
bool HasKey(const K &key) const
Serializable structure for key/value items.
Definition: cachemap.h:18
const list_t & GetItemList() const
list_t::iterator list_it
Definition: cachemultimap.h:30
CacheItem< K, V > item_t
Definition: cachemultimap.h:26
bool GetAll(const K &key, std::vector< V > &vecValues)
void SerializationOp(Stream &s, Operation ser_action)
it_map_t::const_iterator it_map_cit
Definition: cachemultimap.h:38
size_type GetMaxSize() const
Definition: cachemultimap.h:79
size_type GetSize() const
Definition: cachemultimap.h:83
std::map< K, it_map_t > map_t
Definition: cachemultimap.h:40
list_t::const_iterator list_cit
Definition: cachemultimap.h:32
CacheMultiMap(const CacheMap< K, V > &other)
Definition: cachemultimap.h:60
map_t::iterator map_it
Definition: cachemultimap.h:42
size_type nMaxSize
Definition: cachemultimap.h:47
map_t::const_iterator map_cit
Definition: cachemultimap.h:44
void SetMaxSize(size_type nMaxSizeIn)
Definition: cachemultimap.h:74
size_type nMaxSize
Definition: cachemap.h:66
void GetKeys(std::vector< K > &vecKeys)
bool Insert(const K &key, const V &value)
Definition: cachemultimap.h:87
Released under the MIT license