1 // Copyright (c) 2012 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_MRUSET_H
6 #define BITCOIN_MRUSET_H
12 /** STL-like set container that only keeps the most recent N elements. */
19 typedef typename std::set<T>::iterator iterator;
20 typedef typename std::set<T>::const_iterator const_iterator;
21 typedef typename std::set<T>::size_type size_type;
29 mruset(size_type nMaxSizeIn = 0) { nMaxSize = nMaxSizeIn; }
30 iterator begin() const { return set.begin(); }
31 iterator end() const { return set.end(); }
32 size_type size() const { return set.size(); }
33 bool empty() const { return set.empty(); }
34 iterator find(const key_type& k) const { return set.find(k); }
35 size_type count(const key_type& k) const { return set.count(k); }
41 bool inline friend operator==(const mruset<T>& a, const mruset<T>& b) { return a.set == b.set; }
42 bool inline friend operator==(const mruset<T>& a, const std::set<T>& b) { return a.set == b; }
43 bool inline friend operator<(const mruset<T>& a, const mruset<T>& b) { return a.set < b.set; }
44 std::pair<iterator, bool> insert(const key_type& x)
46 std::pair<iterator, bool> ret = set.insert(x);
48 if (nMaxSize && queue.size() == nMaxSize) {
49 set.erase(queue.front());
56 size_type max_size() const { return nMaxSize; }
57 size_type max_size(size_type s)
60 while (queue.size() > s) {
61 set.erase(queue.front());
69 #endif // BITCOIN_MRUSET_H