Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

limitedmap.h
Go to the documentation of this file.
1 // Copyright (c) 2012-2015 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_LIMITEDMAP_H
6 #define BITCOIN_LIMITEDMAP_H
7 
8 #include <assert.h>
9 #include <algorithm>
10 #include <unordered_map>
11 #include <vector>
12 
14 // WARNING, this was initially the "limitedmap" class from Bitcoin, but now does not maintain ordering. If any backports
15 // ever start using this map in a way that requires ordering, do NOT use this as it is but instead reintroduce the original
16 // limitedmap
17 template <typename K, typename V, typename Hash = std::hash<K>>
19 {
20 public:
21  typedef K key_type;
22  typedef V mapped_type;
23  typedef std::pair<const key_type, mapped_type> value_type;
24  typedef typename std::unordered_map<K, V, Hash>::const_iterator const_iterator;
25  typedef typename std::unordered_map<K, V, Hash>::size_type size_type;
26 
27 protected:
28  std::unordered_map<K, V, Hash> map;
29  typedef typename std::unordered_map<K, V, Hash>::iterator iterator;
32 
33 public:
34  explicit unordered_limitedmap(size_type nMaxSizeIn, size_type nPruneAfterSizeIn = 0)
35  {
36  assert(nMaxSizeIn > 0);
37  nMaxSize = nMaxSizeIn;
38  if (nPruneAfterSizeIn == 0) {
40  } else {
41  nPruneAfterSize = nPruneAfterSizeIn;
42  }
43  assert(nPruneAfterSize >= nMaxSize);
44  }
45  const_iterator begin() const { return map.begin(); }
46  const_iterator end() const { return map.end(); }
47  size_type size() const { return map.size(); }
48  bool empty() const { return map.empty(); }
49  const_iterator find(const key_type& k) const { return map.find(k); }
50  size_type count(const key_type& k) const { return map.count(k); }
51  void insert(const value_type& x)
52  {
53  std::pair<iterator, bool> ret = map.insert(x);
54  if (ret.second)
55  prune();
56  }
58  {
59  std::pair<iterator, bool> ret = map.insert(x);
60  if (ret.second)
61  prune();
62  else
63  ret.first->second = x.second;
64  }
65  void erase(const key_type& k)
66  {
67  map.erase(k);
68  }
69  void update(const_iterator itIn, const mapped_type& v)
70  {
71  // Using map::erase() with empty range instead of map::find() to get a non-const iterator,
72  // since it is a constant time operation in C++11. For more details, see
73  // https://stackoverflow.com/questions/765148/how-to-remove-constness-of-const-iterator
74  iterator itTarget = map.erase(itIn, itIn);
75  if (itTarget == map.end())
76  return;
77  itTarget->second = v;
78  }
79  size_type max_size() const { return nMaxSize; }
80  size_type max_size(size_type nMaxSizeIn, size_type nPruneAfterSizeIn = 0)
81  {
82  assert(nMaxSizeIn > 0);
83  nMaxSize = nMaxSizeIn;
84  if (nPruneAfterSizeIn == 0) {
86  } else {
87  nPruneAfterSize = nPruneAfterSizeIn;
88  }
89  assert(nPruneAfterSize >= nMaxSize);
90  prune();
91  return nMaxSize;
92  }
93  void prune()
94  {
95  if (map.size() <= nPruneAfterSize) {
96  return;
97  }
98 
99  std::vector<iterator> sortedIterators;
100  sortedIterators.reserve(map.size());
101  for (auto it = map.begin(); it != map.end(); ++it) {
102  sortedIterators.emplace_back(it);
103  }
104  std::sort(sortedIterators.begin(), sortedIterators.end(), [](const iterator& it1, const iterator& it2) {
105  return it1->second < it2->second;
106  });
107 
108  size_type tooMuch = map.size() - nMaxSize;
109  assert(tooMuch > 0);
110  sortedIterators.resize(tooMuch);
111 
112  for (auto& it : sortedIterators) {
113  map.erase(it);
114  }
115  }
116 };
117 
118 #endif // BITCOIN_LIMITEDMAP_H
std::pair< const key_type, mapped_type > value_type
Definition: limitedmap.h:23
size_type nPruneAfterSize
Definition: limitedmap.h:31
void erase(const key_type &k)
Definition: limitedmap.h:65
void insert(const value_type &x)
Definition: limitedmap.h:51
bool empty() const
Definition: limitedmap.h:48
std::unordered_map< K, V, Hash >::iterator iterator
Definition: limitedmap.h:29
void update(const_iterator itIn, const mapped_type &v)
Definition: limitedmap.h:69
std::unordered_map< K, V, Hash >::const_iterator const_iterator
Definition: limitedmap.h:24
const_iterator begin() const
Definition: limitedmap.h:45
const_iterator end() const
Definition: limitedmap.h:46
std::unordered_map< K, V, Hash > map
Definition: limitedmap.h:28
size_type max_size() const
Definition: limitedmap.h:79
size_type count(const key_type &k) const
Definition: limitedmap.h:50
256-bit opaque blob.
Definition: uint256.h:123
size_type max_size(size_type nMaxSizeIn, size_type nPruneAfterSizeIn=0)
Definition: limitedmap.h:80
size_type nMaxSize
Definition: limitedmap.h:30
unordered_limitedmap(size_type nMaxSizeIn, size_type nPruneAfterSizeIn=0)
Definition: limitedmap.h:34
std::unordered_map< K, V, Hash >::size_type size_type
Definition: limitedmap.h:25
void insert_or_update(const value_type &x)
Definition: limitedmap.h:57
STL-like map container that only keeps the N elements with the highest value.
Definition: limitedmap.h:18
size_type size() const
Definition: limitedmap.h:47
const_iterator find(const key_type &k) const
Definition: limitedmap.h:49
Released under the MIT license