4 #ifndef BITCOIN_LIMITEDMAP_H
5 #define BITCOIN_LIMITEDMAP_H
16 typedef std::pair<const key_type, mapped_type>
value_type;
18 typedef typename std::map<K, V>::size_type
size_type;
22 typedef typename std::map<K, V>::iterator
iterator;
23 std::multimap<V, iterator>
rmap;
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); }
37 std::pair<iterator, bool> ret = map.insert(x);
40 if (nMaxSize && map.size() ==
nMaxSize)
42 map.erase(rmap.begin()->second);
43 rmap.erase(rmap.begin());
45 rmap.insert(make_pair(x.second, ret.first));
51 iterator itTarget = map.find(k);
52 if (itTarget == map.end())
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)
66 void update(const_iterator itIn,
const mapped_type& v)
69 iterator itTarget = map.find(itIn->first);
70 if (itTarget == map.end())
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)
78 rmap.insert(make_pair(v, itTarget));
84 rmap.insert(make_pair(v, itTarget));
90 while (map.size() > s)
92 map.erase(rmap.begin()->second);
93 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