6 #ifndef ANONCOIN_LIMITEDMAP_H
7 #define ANONCOIN_LIMITEDMAP_H
18 typedef std::pair<const key_type, mapped_type>
value_type;
20 typedef typename std::map<K, V>::size_type
size_type;
24 typedef typename std::map<K, V>::iterator
iterator;
25 std::multimap<V, iterator>
rmap;
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); }
39 std::pair<iterator, bool> ret = map.insert(x);
42 if (nMaxSize && map.size() ==
nMaxSize)
44 map.erase(rmap.begin()->second);
45 rmap.erase(rmap.begin());
47 rmap.insert(make_pair(x.second, ret.first));
53 iterator itTarget = map.find(k);
54 if (itTarget == map.end())
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)
68 void update(const_iterator itIn,
const mapped_type& v)
71 iterator itTarget = map.find(itIn->first);
72 if (itTarget == map.end())
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)
80 rmap.insert(make_pair(v, itTarget));
86 rmap.insert(make_pair(v, itTarget));
92 while (map.size() > s)
94 map.erase(rmap.begin()->second);
95 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