2015-04-25 17:19:57 +02:00
|
|
|
// Copyright (c) 2012-2015 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 16:02:28 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2012-02-27 17:55:53 +01:00
|
|
|
#ifndef BITCOIN_MRUSET_H
|
|
|
|
#define BITCOIN_MRUSET_H
|
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <set>
|
2015-04-25 17:19:57 +02:00
|
|
|
#include <vector>
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <utility>
|
2012-02-27 17:55:53 +01:00
|
|
|
|
2012-03-26 16:48:23 +02:00
|
|
|
/** STL-like set container that only keeps the most recent N elements. */
|
2014-09-19 19:21:46 +02:00
|
|
|
template <typename T>
|
|
|
|
class mruset
|
2012-02-27 17:55:53 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef T key_type;
|
|
|
|
typedef T value_type;
|
|
|
|
typedef typename std::set<T>::iterator iterator;
|
|
|
|
typedef typename std::set<T>::const_iterator const_iterator;
|
|
|
|
typedef typename std::set<T>::size_type size_type;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::set<T> set;
|
2015-04-25 17:19:57 +02:00
|
|
|
std::vector<iterator> order;
|
|
|
|
size_type first_used;
|
|
|
|
size_type first_unused;
|
|
|
|
const size_type nMaxSize;
|
2012-02-27 17:55:53 +01:00
|
|
|
|
|
|
|
public:
|
2015-04-25 17:19:57 +02:00
|
|
|
mruset(size_type nMaxSizeIn = 1) : nMaxSize(nMaxSizeIn) { clear(); }
|
2012-02-27 17:55:53 +01:00
|
|
|
iterator begin() const { return set.begin(); }
|
|
|
|
iterator end() const { return set.end(); }
|
|
|
|
size_type size() const { return set.size(); }
|
|
|
|
bool empty() const { return set.empty(); }
|
|
|
|
iterator find(const key_type& k) const { return set.find(k); }
|
|
|
|
size_type count(const key_type& k) const { return set.count(k); }
|
2014-09-19 19:21:46 +02:00
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
set.clear();
|
2015-04-25 17:19:57 +02:00
|
|
|
order.assign(nMaxSize, set.end());
|
|
|
|
first_used = 0;
|
|
|
|
first_unused = 0;
|
2014-09-19 19:21:46 +02:00
|
|
|
}
|
2012-02-27 17:55:53 +01:00
|
|
|
bool inline friend operator==(const mruset<T>& a, const mruset<T>& b) { return a.set == b.set; }
|
|
|
|
bool inline friend operator==(const mruset<T>& a, const std::set<T>& b) { return a.set == b; }
|
|
|
|
bool inline friend operator<(const mruset<T>& a, const mruset<T>& b) { return a.set < b.set; }
|
|
|
|
std::pair<iterator, bool> insert(const key_type& x)
|
|
|
|
{
|
|
|
|
std::pair<iterator, bool> ret = set.insert(x);
|
2014-09-19 19:21:46 +02:00
|
|
|
if (ret.second) {
|
2015-04-25 17:19:57 +02:00
|
|
|
if (set.size() == nMaxSize + 1) {
|
|
|
|
set.erase(order[first_used]);
|
|
|
|
order[first_used] = set.end();
|
|
|
|
if (++first_used == nMaxSize) first_used = 0;
|
2012-02-27 17:55:53 +01:00
|
|
|
}
|
2015-04-25 17:19:57 +02:00
|
|
|
order[first_unused] = ret.first;
|
|
|
|
if (++first_unused == nMaxSize) first_unused = 0;
|
2012-02-27 17:55:53 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
size_type max_size() const { return nMaxSize; }
|
|
|
|
};
|
|
|
|
|
2014-08-28 22:21:03 +02:00
|
|
|
#endif // BITCOIN_MRUSET_H
|