Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
mruset.h
Go to the documentation of this file.
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.
4 
5 #ifndef BITCOIN_MRUSET_H
6 #define BITCOIN_MRUSET_H
7 
8 #include <deque>
9 #include <set>
10 #include <utility>
11 
13 template <typename T> class mruset
14 {
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;
21 
22 protected:
23  std::set<T> set;
24  std::deque<T> queue;
25  size_type nMaxSize;
26 
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  void clear() { set.clear(); queue.clear(); }
36  bool inline friend operator==(const mruset<T>& a, const mruset<T>& b) { return a.set == b.set; }
37  bool inline friend operator==(const mruset<T>& a, const std::set<T>& b) { return a.set == b; }
38  bool inline friend operator<(const mruset<T>& a, const mruset<T>& b) { return a.set < b.set; }
39  std::pair<iterator, bool> insert(const key_type& x)
40  {
41  std::pair<iterator, bool> ret = set.insert(x);
42  if (ret.second)
43  {
44  if (nMaxSize && queue.size() == nMaxSize)
45  {
46  set.erase(queue.front());
47  queue.pop_front();
48  }
49  queue.push_back(x);
50  }
51  return ret;
52  }
53  size_type max_size() const { return nMaxSize; }
54  size_type max_size(size_type s)
55  {
56  if (s)
57  while (queue.size() > s)
58  {
59  set.erase(queue.front());
60  queue.pop_front();
61  }
62  nMaxSize = s;
63  return nMaxSize;
64  }
65 };
66 
67 #endif
bool empty() const
Definition: mruset.h:32
T value_type
Definition: mruset.h:17
void clear()
Definition: mruset.h:35
std::set< T >::iterator iterator
Definition: mruset.h:18
size_type size() const
Definition: mruset.h:31
std::pair< iterator, bool > insert(const key_type &x)
Definition: mruset.h:39
iterator find(const key_type &k) const
Definition: mruset.h:33
size_type max_size() const
Definition: mruset.h:53
std::deque< T > queue
Definition: mruset.h:24
STL-like set container that only keeps the most recent N elements.
Definition: mruset.h:13
size_type count(const key_type &k) const
Definition: mruset.h:34
iterator end() const
Definition: mruset.h:30
std::set< T > set
Definition: mruset.h:23
bool friend operator==(const mruset< T > &a, const std::set< T > &b)
Definition: mruset.h:37
size_type max_size(size_type s)
Definition: mruset.h:54
std::set< T >::const_iterator const_iterator
Definition: mruset.h:19
T key_type
Definition: mruset.h:16
iterator begin() const
Definition: mruset.h:29
bool friend operator==(const mruset< T > &a, const mruset< T > &b)
Definition: mruset.h:36
mruset(size_type nMaxSizeIn=0)
Definition: mruset.h:28
size_type nMaxSize
Definition: mruset.h:25
std::set< T >::size_type size_type
Definition: mruset.h:20