|
|
@ -1,12 +1,12 @@ |
|
|
|
// Copyright (c) 2012 The Bitcoin Core developers
|
|
|
|
// Copyright (c) 2012-2015 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef BITCOIN_MRUSET_H |
|
|
|
#ifndef BITCOIN_MRUSET_H |
|
|
|
#define BITCOIN_MRUSET_H |
|
|
|
#define BITCOIN_MRUSET_H |
|
|
|
|
|
|
|
|
|
|
|
#include <deque> |
|
|
|
|
|
|
|
#include <set> |
|
|
|
#include <set> |
|
|
|
|
|
|
|
#include <vector> |
|
|
|
#include <utility> |
|
|
|
#include <utility> |
|
|
|
|
|
|
|
|
|
|
|
/** STL-like set container that only keeps the most recent N elements. */ |
|
|
|
/** STL-like set container that only keeps the most recent N elements. */ |
|
|
@ -22,11 +22,13 @@ public: |
|
|
|
|
|
|
|
|
|
|
|
protected: |
|
|
|
protected: |
|
|
|
std::set<T> set; |
|
|
|
std::set<T> set; |
|
|
|
std::deque<T> queue; |
|
|
|
std::vector<iterator> order; |
|
|
|
size_type nMaxSize; |
|
|
|
size_type first_used; |
|
|
|
|
|
|
|
size_type first_unused; |
|
|
|
|
|
|
|
const size_type nMaxSize; |
|
|
|
|
|
|
|
|
|
|
|
public: |
|
|
|
public: |
|
|
|
mruset(size_type nMaxSizeIn = 0) { nMaxSize = nMaxSizeIn; } |
|
|
|
mruset(size_type nMaxSizeIn = 1) : nMaxSize(nMaxSizeIn) { clear(); } |
|
|
|
iterator begin() const { return set.begin(); } |
|
|
|
iterator begin() const { return set.begin(); } |
|
|
|
iterator end() const { return set.end(); } |
|
|
|
iterator end() const { return set.end(); } |
|
|
|
size_type size() const { return set.size(); } |
|
|
|
size_type size() const { return set.size(); } |
|
|
@ -36,7 +38,9 @@ public: |
|
|
|
void clear() |
|
|
|
void clear() |
|
|
|
{ |
|
|
|
{ |
|
|
|
set.clear(); |
|
|
|
set.clear(); |
|
|
|
queue.clear(); |
|
|
|
order.assign(nMaxSize, set.end()); |
|
|
|
|
|
|
|
first_used = 0; |
|
|
|
|
|
|
|
first_unused = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
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 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 std::set<T>& b) { return a.set == b; } |
|
|
@ -45,25 +49,17 @@ public: |
|
|
|
{ |
|
|
|
{ |
|
|
|
std::pair<iterator, bool> ret = set.insert(x); |
|
|
|
std::pair<iterator, bool> ret = set.insert(x); |
|
|
|
if (ret.second) { |
|
|
|
if (ret.second) { |
|
|
|
if (nMaxSize && queue.size() == nMaxSize) { |
|
|
|
if (set.size() == nMaxSize + 1) { |
|
|
|
set.erase(queue.front()); |
|
|
|
set.erase(order[first_used]); |
|
|
|
queue.pop_front(); |
|
|
|
order[first_used] = set.end(); |
|
|
|
|
|
|
|
if (++first_used == nMaxSize) first_used = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
queue.push_back(x); |
|
|
|
order[first_unused] = ret.first; |
|
|
|
|
|
|
|
if (++first_unused == nMaxSize) first_unused = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
return ret; |
|
|
|
return ret; |
|
|
|
} |
|
|
|
} |
|
|
|
size_type max_size() const { return nMaxSize; } |
|
|
|
size_type max_size() const { return nMaxSize; } |
|
|
|
size_type max_size(size_type s) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (s) |
|
|
|
|
|
|
|
while (queue.size() > s) { |
|
|
|
|
|
|
|
set.erase(queue.front()); |
|
|
|
|
|
|
|
queue.pop_front(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
nMaxSize = s; |
|
|
|
|
|
|
|
return nMaxSize; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
#endif // BITCOIN_MRUSET_H
|
|
|
|
#endif // BITCOIN_MRUSET_H
|
|
|
|