Merge pull request #4048 from sipa/nobigb58
[bitcoinplatinum.git] / src / mruset.h
blobc36a0c8f37992d74b44d1c3ecf93e4c867484f8b
1 // Copyright (c) 2012 The Bitcoin developers
2 // Distributed under the MIT/X11 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
8 #include <deque>
9 #include <set>
10 #include <utility>
12 /** STL-like set container that only keeps the most recent N elements. */
13 template <typename T> class mruset
15 public:
16 typedef T key_type;
17 typedef T value_type;
18 typedef typename std::set<T>::iterator iterator;
19 typedef typename std::set<T>::const_iterator const_iterator;
20 typedef typename std::set<T>::size_type size_type;
22 protected:
23 std::set<T> set;
24 std::deque<T> queue;
25 size_type nMaxSize;
27 public:
28 mruset(size_type nMaxSizeIn = 0) { nMaxSize = nMaxSizeIn; }
29 iterator begin() const { return set.begin(); }
30 iterator end() const { return set.end(); }
31 size_type size() const { return set.size(); }
32 bool empty() const { return set.empty(); }
33 iterator find(const key_type& k) const { return set.find(k); }
34 size_type count(const key_type& k) const { return set.count(k); }
35 bool inline friend operator==(const mruset<T>& a, const mruset<T>& b) { return a.set == b.set; }
36 bool inline friend operator==(const mruset<T>& a, const std::set<T>& b) { return a.set == b; }
37 bool inline friend operator<(const mruset<T>& a, const mruset<T>& b) { return a.set < b.set; }
38 std::pair<iterator, bool> insert(const key_type& x)
40 std::pair<iterator, bool> ret = set.insert(x);
41 if (ret.second)
43 if (nMaxSize && queue.size() == nMaxSize)
45 set.erase(queue.front());
46 queue.pop_front();
48 queue.push_back(x);
50 return ret;
52 size_type max_size() const { return nMaxSize; }
53 size_type max_size(size_type s)
55 if (s)
56 while (queue.size() > s)
58 set.erase(queue.front());
59 queue.pop_front();
61 nMaxSize = s;
62 return nMaxSize;
66 #endif