aboutsummaryrefslogtreecommitdiff
path: root/src/mruset.h
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2012-02-27 17:55:53 +0100
committerPieter Wuille <pieter.wuille@gmail.com>2012-02-27 21:04:32 +0100
commitc4341fa6abf7510c6de72cd435f4d4146dce74c2 (patch)
tree99280bb817318cf04b6d9de6b4df82a171218168 /src/mruset.h
parentfbbd42a535813b2d7e30dba44c5c36b70833fe55 (diff)
Add mruset and use it for setInventoryKnown
Diffstat (limited to 'src/mruset.h')
-rw-r--r--src/mruset.h63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/mruset.h b/src/mruset.h
new file mode 100644
index 0000000000..0cf65853c4
--- /dev/null
+++ b/src/mruset.h
@@ -0,0 +1,63 @@
+// Copyright (c) 2012 The Bitcoin developers
+// Distributed under the MIT/X11 software license, see the accompanying
+// file license.txt or http://www.opensource.org/licenses/mit-license.php.
+#ifndef BITCOIN_MRUSET_H
+#define BITCOIN_MRUSET_H
+
+#include <set>
+#include <deque>
+
+template <typename T> class mruset
+{
+public:
+ typedef T key_type;
+ typedef T value_type;
+ typedef typename std::set<T>::iterator iterator;
+ typedef typename std::set<T>::const_iterator const_iterator;
+ typedef typename std::set<T>::size_type size_type;
+
+protected:
+ std::set<T> set;
+ std::deque<T> queue;
+ size_type nMaxSize;
+
+public:
+ mruset(size_type nMaxSizeIn = 0) { nMaxSize = nMaxSizeIn; }
+ iterator begin() const { return set.begin(); }
+ iterator end() const { return set.end(); }
+ size_type size() const { return set.size(); }
+ bool empty() const { return set.empty(); }
+ iterator find(const key_type& k) const { return set.find(k); }
+ size_type count(const key_type& k) const { return set.count(k); }
+ bool inline friend operator==(const mruset<T>& a, const mruset<T>& b) { return a.set == b.set; }
+ bool inline friend operator==(const mruset<T>& a, const std::set<T>& b) { return a.set == b; }
+ bool inline friend operator<(const mruset<T>& a, const mruset<T>& b) { return a.set < b.set; }
+ std::pair<iterator, bool> insert(const key_type& x)
+ {
+ std::pair<iterator, bool> ret = set.insert(x);
+ if (ret.second)
+ {
+ if (nMaxSize && queue.size() == nMaxSize)
+ {
+ set.erase(queue.front());
+ queue.pop_front();
+ }
+ queue.push_back(x);
+ }
+ return ret;
+ }
+ size_type max_size() const { return nMaxSize; }
+ size_type max_size(size_type s)
+ {
+ if (s)
+ while (queue.size() >= s)
+ {
+ set.erase(queue.front());
+ queue.pop_front();
+ }
+ nMaxSize = s;
+ return nMaxSize;
+ }
+};
+
+#endif