Raven Core  3.0.0
P2P Digital Currency
limitedmap.h
Go to the documentation of this file.
1 // Copyright (c) 2012-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef RAVEN_LIMITEDMAP_H
7 #define RAVEN_LIMITEDMAP_H
8 
9 #include <assert.h>
10 #include <map>
11 
13 template <typename K, typename V>
15 {
16 public:
17  typedef K key_type;
18  typedef V mapped_type;
19  typedef std::pair<const key_type, mapped_type> value_type;
20  typedef typename std::map<K, V>::const_iterator const_iterator;
21  typedef typename std::map<K, V>::size_type size_type;
22 
23 protected:
24  std::map<K, V> map;
25  typedef typename std::map<K, V>::iterator iterator;
26  std::multimap<V, iterator> rmap;
27  typedef typename std::multimap<V, iterator>::iterator rmap_iterator;
28  size_type nMaxSize;
29 
30 public:
31  explicit limitedmap(size_type nMaxSizeIn)
32  {
33  assert(nMaxSizeIn > 0);
34  nMaxSize = nMaxSizeIn;
35  }
36  const_iterator begin() const { return map.begin(); }
37  const_iterator end() const { return map.end(); }
38  size_type size() const { return map.size(); }
39  bool empty() const { return map.empty(); }
40  const_iterator find(const key_type& k) const { return map.find(k); }
41  size_type count(const key_type& k) const { return map.count(k); }
42  void insert(const value_type& x)
43  {
44  std::pair<iterator, bool> ret = map.insert(x);
45  if (ret.second) {
46  if (map.size() > nMaxSize) {
47  map.erase(rmap.begin()->second);
48  rmap.erase(rmap.begin());
49  }
50  rmap.insert(make_pair(x.second, ret.first));
51  }
52  }
53  void erase(const key_type& k)
54  {
55  iterator itTarget = map.find(k);
56  if (itTarget == map.end())
57  return;
58  std::pair<rmap_iterator, rmap_iterator> itPair = rmap.equal_range(itTarget->second);
59  for (rmap_iterator it = itPair.first; it != itPair.second; ++it)
60  if (it->second == itTarget) {
61  rmap.erase(it);
62  map.erase(itTarget);
63  return;
64  }
65  // Shouldn't ever get here
66  assert(0);
67  }
68  void update(const_iterator itIn, const mapped_type& v)
69  {
70  // Using map::erase() with empty range instead of map::find() to get a non-const iterator,
71  // since it is a constant time operation in C++11. For more details, see
72  // https://stackoverflow.com/questions/765148/how-to-remove-constness-of-const-iterator
73  iterator itTarget = map.erase(itIn, itIn);
74 
75  if (itTarget == map.end())
76  return;
77  std::pair<rmap_iterator, rmap_iterator> itPair = rmap.equal_range(itTarget->second);
78  for (rmap_iterator it = itPair.first; it != itPair.second; ++it)
79  if (it->second == itTarget) {
80  rmap.erase(it);
81  itTarget->second = v;
82  rmap.insert(make_pair(v, itTarget));
83  return;
84  }
85  // Shouldn't ever get here
86  assert(0);
87  }
88  size_type max_size() const { return nMaxSize; }
89  size_type max_size(size_type s)
90  {
91  assert(s > 0);
92  while (map.size() > s) {
93  map.erase(rmap.begin()->second);
94  rmap.erase(rmap.begin());
95  }
96  nMaxSize = s;
97  return nMaxSize;
98  }
99 };
100 
101 #endif // RAVEN_LIMITEDMAP_H
std::map< K, V >::const_iterator const_iterator
Definition: limitedmap.h:20
bool empty() const
Definition: limitedmap.h:39
std::pair< const key_type, mapped_type > value_type
Definition: limitedmap.h:19
std::map< K, V > map
Definition: limitedmap.h:24
STL-like map container that only keeps the N elements with the highest value.
Definition: limitedmap.h:14
void insert(const value_type &x)
Definition: limitedmap.h:42
std::multimap< V, iterator > rmap
Definition: limitedmap.h:26
void update(const_iterator itIn, const mapped_type &v)
Definition: limitedmap.h:68
size_type size() const
Definition: limitedmap.h:38
limitedmap(size_type nMaxSizeIn)
Definition: limitedmap.h:31
const_iterator find(const key_type &k) const
Definition: limitedmap.h:40
std::multimap< V, iterator >::iterator rmap_iterator
Definition: limitedmap.h:27
const_iterator begin() const
Definition: limitedmap.h:36
const_iterator end() const
Definition: limitedmap.h:37
void erase(const key_type &k)
Definition: limitedmap.h:53
size_type count(const key_type &k) const
Definition: limitedmap.h:41
size_type max_size() const
Definition: limitedmap.h:88
size_type max_size(size_type s)
Definition: limitedmap.h:89
size_type nMaxSize
Definition: limitedmap.h:28
std::map< K, V >::iterator iterator
Definition: limitedmap.h:25
std::map< K, V >::size_type size_type
Definition: limitedmap.h:21