6 #ifndef RAVEN_LIMITEDMAP_H 7 #define RAVEN_LIMITEDMAP_H 13 template <
typename K,
typename V>
19 typedef std::pair<const key_type, mapped_type>
value_type;
21 typedef typename std::map<K, V>::size_type
size_type;
25 typedef typename std::map<K, V>::iterator
iterator;
26 std::multimap<V, iterator>
rmap;
33 assert(nMaxSizeIn > 0);
34 nMaxSize = nMaxSizeIn;
36 const_iterator
begin()
const {
return map.begin(); }
37 const_iterator
end()
const {
return map.end(); }
38 size_type
size()
const {
return map.size(); }
39 bool empty()
const {
return map.empty(); }
40 const_iterator
find(
const key_type& k)
const {
return map.find(k); }
41 size_type
count(
const key_type& k)
const {
return map.count(k); }
44 std::pair<iterator, bool> ret = map.insert(x);
47 map.erase(rmap.begin()->second);
48 rmap.erase(rmap.begin());
50 rmap.insert(make_pair(x.second, ret.first));
55 iterator itTarget = map.find(k);
56 if (itTarget == map.end())
58 std::pair<rmap_iterator, rmap_iterator> itPair = rmap.equal_range(itTarget->second);
59 for (rmap_iterator it = itPair.first; it != itPair.second; ++it)
60 if (it->second == itTarget) {
68 void update(const_iterator itIn,
const mapped_type& v)
73 iterator itTarget = map.erase(itIn, itIn);
75 if (itTarget == map.end())
77 std::pair<rmap_iterator, rmap_iterator> itPair = rmap.equal_range(itTarget->second);
78 for (rmap_iterator it = itPair.first; it != itPair.second; ++it)
79 if (it->second == itTarget) {
82 rmap.insert(make_pair(v, itTarget));
92 while (map.size() > s) {
93 map.erase(rmap.begin()->second);
94 rmap.erase(rmap.begin());
101 #endif // RAVEN_LIMITEDMAP_H 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)
const_iterator find(const key_type &k) const
std::multimap< V, iterator >::iterator rmap_iterator
const_iterator begin() const
const_iterator end() const
void erase(const key_type &k)
size_type count(const key_type &k) const
size_type max_size() const
size_type max_size(size_type s)
std::map< K, V >::iterator iterator
std::map< K, V >::size_type size_type