5 #ifndef BITCOIN_LIMITEDMAP_H
6 #define BITCOIN_LIMITEDMAP_H
17 typedef std::pair<const key_type, mapped_type>
value_type;
19 typedef typename std::map<K, V>::size_type
size_type;
23 typedef typename std::map<K, V>::iterator
iterator;
24 std::multimap<V, iterator>
rmap;
29 limitedmap(size_type nMaxSizeIn = 0) { nMaxSize = nMaxSizeIn; }
30 const_iterator
begin()
const {
return map.begin(); }
31 const_iterator
end()
const {
return map.end(); }
32 size_type
size()
const {
return map.size(); }
33 bool empty()
const {
return map.empty(); }
34 const_iterator
find(
const key_type& k)
const {
return map.find(k); }
35 size_type
count(
const key_type& k)
const {
return map.count(k); }
38 std::pair<iterator, bool> ret = map.insert(x);
41 if (nMaxSize && map.size() ==
nMaxSize)
43 map.erase(rmap.begin()->second);
44 rmap.erase(rmap.begin());
46 rmap.insert(make_pair(x.second, ret.first));
52 iterator itTarget = map.find(k);
53 if (itTarget == map.end())
55 std::pair<rmap_iterator, rmap_iterator> itPair = rmap.equal_range(itTarget->second);
56 for (rmap_iterator it = itPair.first; it != itPair.second; ++it)
57 if (it->second == itTarget)
67 void update(const_iterator itIn,
const mapped_type& v)
70 iterator itTarget = map.find(itIn->first);
71 if (itTarget == map.end())
73 std::pair<rmap_iterator, rmap_iterator> itPair = rmap.equal_range(itTarget->second);
74 for (rmap_iterator it = itPair.first; it != itPair.second; ++it)
75 if (it->second == itTarget)
79 rmap.insert(make_pair(v, itTarget));
85 rmap.insert(make_pair(v, itTarget));
91 while (map.size() > s)
93 map.erase(rmap.begin()->second);
94 rmap.erase(rmap.begin());
std::map< K, V >::const_iterator const_iterator
std::pair< const key_type, mapped_type > value_type
STL-like map container that only keeps the N elements with the highest value.
void insert(const value_type &x)
std::multimap< V, iterator > rmap
void update(const_iterator itIn, const mapped_type &v)
limitedmap(size_type nMaxSizeIn=0)
std::multimap< V, iterator >::iterator rmap_iterator
size_type count(const key_type &k) const
const_iterator end() const
size_type max_size() const
void erase(const key_type &k)
const_iterator begin() const
size_type max_size(size_type s)
std::map< K, V >::iterator iterator
const_iterator find(const key_type &k) const
std::map< K, V >::size_type size_type