Anoncoin  0.9.4
P2P Digital Currency
mruset.h
Go to the documentation of this file.
1 // Copyright (c) 2012 The Bitcoin developers
2 // Copyright (c) 2013-2014 The Anoncoin Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef ANONCOIN_MRUSET_H
7 #define ANONCOIN_MRUSET_H
8 
9 #include <deque>
10 #include <set>
11 #include <utility>
12 
14 template <typename T> class mruset
15 {
16 public:
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 
23 protected:
24  std::set<T> set;
25  std::deque<T> queue;
26  size_type nMaxSize;
27 
28 public:
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); }
36  void clear() { set.clear(); queue.clear(); }
37  bool inline friend operator==(const mruset<T>& a, const mruset<T>& b) { return a.set == b.set; }
38  bool inline friend operator==(const mruset<T>& a, const std::set<T>& b) { return a.set == b; }
39  bool inline friend operator<(const mruset<T>& a, const mruset<T>& b) { return a.set < b.set; }
40  std::pair<iterator, bool> insert(const key_type& x)
41  {
42  std::pair<iterator, bool> ret = set.insert(x);
43  if (ret.second)
44  {
45  if (nMaxSize && queue.size() == nMaxSize)
46  {
47  set.erase(queue.front());
48  queue.pop_front();
49  }
50  queue.push_back(x);
51  }
52  return ret;
53  }
54  size_type max_size() const { return nMaxSize; }
55  size_type max_size(size_type s)
56  {
57  if (s)
58  while (queue.size() > s)
59  {
60  set.erase(queue.front());
61  queue.pop_front();
62  }
63  nMaxSize = s;
64  return nMaxSize;
65  }
66 };
67 
68 #endif
bool empty() const
Definition: mruset.h:33
T value_type
Definition: mruset.h:18
void clear()
Definition: mruset.h:36
std::set< T >::iterator iterator
Definition: mruset.h:19
size_type size() const
Definition: mruset.h:32
std::pair< iterator, bool > insert(const key_type &x)
Definition: mruset.h:40
iterator find(const key_type &k) const
Definition: mruset.h:34
size_type max_size() const
Definition: mruset.h:54
std::deque< T > queue
Definition: mruset.h:25
STL-like set container that only keeps the most recent N elements.
Definition: mruset.h:14
size_type count(const key_type &k) const
Definition: mruset.h:35
iterator end() const
Definition: mruset.h:31
std::set< T > set
Definition: mruset.h:24
bool friend operator==(const mruset< T > &a, const std::set< T > &b)
Definition: mruset.h:38
size_type max_size(size_type s)
Definition: mruset.h:55
std::set< T >::const_iterator const_iterator
Definition: mruset.h:20
T key_type
Definition: mruset.h:17
iterator begin() const
Definition: mruset.h:30
bool friend operator==(const mruset< T > &a, const mruset< T > &b)
Definition: mruset.h:37
mruset(size_type nMaxSizeIn=0)
Definition: mruset.h:29
size_type nMaxSize
Definition: mruset.h:26
std::set< T >::size_type size_type
Definition: mruset.h:21