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