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