]> Git Repo - VerusCoin.git/blame - src/mruset.h
Remove references to X11 licence
[VerusCoin.git] / src / mruset.h
CommitLineData
c4341fa6 1// Copyright (c) 2012 The Bitcoin developers
78253fcb 2// Distributed under the MIT software license, see the accompanying
3a25a2b9 3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
51ed9ec9 4
c4341fa6
PW
5#ifndef BITCOIN_MRUSET_H
6#define BITCOIN_MRUSET_H
7
c4341fa6 8#include <deque>
51ed9ec9
BD
9#include <set>
10#include <utility>
c4341fa6 11
6b8de05d 12/** STL-like set container that only keeps the most recent N elements. */
20e01b1a
PW
13template <typename T>
14class mruset
c4341fa6
PW
15{
16public:
17 typedef T key_type;
18 typedef T value_type;
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;
22
23protected:
24 std::set<T> set;
25 std::deque<T> queue;
26 size_type nMaxSize;
27
28public:
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); }
20e01b1a
PW
36 void clear()
37 {
38 set.clear();
39 queue.clear();
40 }
c4341fa6
PW
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)
45 {
46 std::pair<iterator, bool> ret = set.insert(x);
20e01b1a
PW
47 if (ret.second) {
48 if (nMaxSize && queue.size() == nMaxSize) {
c4341fa6
PW
49 set.erase(queue.front());
50 queue.pop_front();
51 }
52 queue.push_back(x);
53 }
54 return ret;
55 }
56 size_type max_size() const { return nMaxSize; }
57 size_type max_size(size_type s)
58 {
59 if (s)
20e01b1a 60 while (queue.size() > s) {
c4341fa6
PW
61 set.erase(queue.front());
62 queue.pop_front();
63 }
64 nMaxSize = s;
65 return nMaxSize;
66 }
67};
68
093303a8 69#endif // BITCOIN_MRUSET_H
This page took 0.141959 seconds and 4 git commands to generate.