2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2012-2015 The Bitcoin Core developers
|
2014-08-28 23:00:55 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-03-27 06:07:20 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2013-03-27 06:07:20 +01:00
|
|
|
#ifndef BITCOIN_LIMITEDMAP_H
|
|
|
|
#define BITCOIN_LIMITEDMAP_H
|
|
|
|
|
2014-08-28 23:00:55 +02:00
|
|
|
#include <assert.h>
|
2013-03-27 06:07:20 +01:00
|
|
|
#include <map>
|
|
|
|
|
|
|
|
/** STL-like map container that only keeps the N elements with the highest value. */
|
2014-09-19 19:21:46 +02:00
|
|
|
template <typename K, typename V>
|
|
|
|
class limitedmap
|
2013-03-27 06:07:20 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef K key_type;
|
|
|
|
typedef V mapped_type;
|
|
|
|
typedef std::pair<const key_type, mapped_type> value_type;
|
|
|
|
typedef typename std::map<K, V>::const_iterator const_iterator;
|
|
|
|
typedef typename std::map<K, V>::size_type size_type;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::map<K, V> map;
|
|
|
|
typedef typename std::map<K, V>::iterator iterator;
|
|
|
|
std::multimap<V, iterator> rmap;
|
|
|
|
typedef typename std::multimap<V, iterator>::iterator rmap_iterator;
|
|
|
|
size_type nMaxSize;
|
|
|
|
|
|
|
|
public:
|
2015-08-17 18:06:45 +02:00
|
|
|
limitedmap(size_type nMaxSizeIn)
|
|
|
|
{
|
|
|
|
assert(nMaxSizeIn > 0);
|
|
|
|
nMaxSize = nMaxSizeIn;
|
|
|
|
}
|
2013-03-27 06:07:20 +01:00
|
|
|
const_iterator begin() const { return map.begin(); }
|
|
|
|
const_iterator end() const { return map.end(); }
|
|
|
|
size_type size() const { return map.size(); }
|
|
|
|
bool empty() const { return map.empty(); }
|
|
|
|
const_iterator find(const key_type& k) const { return map.find(k); }
|
|
|
|
size_type count(const key_type& k) const { return map.count(k); }
|
|
|
|
void insert(const value_type& x)
|
|
|
|
{
|
|
|
|
std::pair<iterator, bool> ret = map.insert(x);
|
2014-09-19 19:21:46 +02:00
|
|
|
if (ret.second) {
|
2015-08-17 18:06:45 +02:00
|
|
|
if (map.size() > nMaxSize) {
|
2013-03-27 06:07:20 +01:00
|
|
|
map.erase(rmap.begin()->second);
|
|
|
|
rmap.erase(rmap.begin());
|
|
|
|
}
|
|
|
|
rmap.insert(make_pair(x.second, ret.first));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void erase(const key_type& k)
|
|
|
|
{
|
|
|
|
iterator itTarget = map.find(k);
|
|
|
|
if (itTarget == map.end())
|
|
|
|
return;
|
|
|
|
std::pair<rmap_iterator, rmap_iterator> itPair = rmap.equal_range(itTarget->second);
|
|
|
|
for (rmap_iterator it = itPair.first; it != itPair.second; ++it)
|
2014-09-19 19:21:46 +02:00
|
|
|
if (it->second == itTarget) {
|
2013-03-27 06:07:20 +01:00
|
|
|
rmap.erase(it);
|
|
|
|
map.erase(itTarget);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Shouldn't ever get here
|
2014-08-28 23:00:55 +02:00
|
|
|
assert(0);
|
2013-03-27 06:07:20 +01:00
|
|
|
}
|
|
|
|
void update(const_iterator itIn, const mapped_type& v)
|
|
|
|
{
|
2016-08-13 15:50:23 +02:00
|
|
|
// Using map::erase() with empty range instead of map::find() to get a non-const iterator,
|
|
|
|
// since it is a constant time operation in C++11. For more details, see
|
|
|
|
// https://stackoverflow.com/questions/765148/how-to-remove-constness-of-const-iterator
|
|
|
|
iterator itTarget = map.erase(itIn, itIn);
|
|
|
|
|
2013-03-27 06:07:20 +01:00
|
|
|
if (itTarget == map.end())
|
|
|
|
return;
|
|
|
|
std::pair<rmap_iterator, rmap_iterator> itPair = rmap.equal_range(itTarget->second);
|
|
|
|
for (rmap_iterator it = itPair.first; it != itPair.second; ++it)
|
2014-09-19 19:21:46 +02:00
|
|
|
if (it->second == itTarget) {
|
2013-03-27 06:07:20 +01:00
|
|
|
rmap.erase(it);
|
|
|
|
itTarget->second = v;
|
|
|
|
rmap.insert(make_pair(v, itTarget));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Shouldn't ever get here
|
2014-08-28 23:00:55 +02:00
|
|
|
assert(0);
|
2013-03-27 06:07:20 +01:00
|
|
|
}
|
|
|
|
size_type max_size() const { return nMaxSize; }
|
|
|
|
size_type max_size(size_type s)
|
|
|
|
{
|
2015-08-17 18:06:45 +02:00
|
|
|
assert(s > 0);
|
|
|
|
while (map.size() > s) {
|
|
|
|
map.erase(rmap.begin()->second);
|
|
|
|
rmap.erase(rmap.begin());
|
|
|
|
}
|
2013-03-27 06:07:20 +01:00
|
|
|
nMaxSize = s;
|
|
|
|
return nMaxSize;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-28 22:21:03 +02:00
|
|
|
#endif // BITCOIN_LIMITEDMAP_H
|