aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJorge Timón <jtimon@jtimon.cc>2017-04-13 00:24:40 +0200
committerJorge Timón <jtimon@jtimon.cc>2017-06-22 03:48:42 +0200
commit300851ec1690e545485e154b1a4df55bec7621ae (patch)
tree55acdf3fb3461030a5dde793c3064d5a27a079c8
parent1ad3d4e1261f4a444d982a1470c257c78233bda3 (diff)
downloadbitcoin-300851ec1690e545485e154b1a4df55bec7621ae.tar.xz
Introduce src/reverse_iterator.hpp and include it...
...where it will be needed Taken from https://gist.github.com/arvidsson/7231973 with small modifications to fit the bitcoin core project
-rw-r--r--src/Makefile.am1
-rw-r--r--src/checkpoints.cpp1
-rw-r--r--src/net_processing.cpp1
-rw-r--r--src/reverse_iterator.h39
-rw-r--r--src/test/prevector_tests.cpp1
-rw-r--r--src/txmempool.cpp1
-rw-r--r--src/validation.cpp1
7 files changed, 45 insertions, 0 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 9b9dd89f68..a37c6a502b 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -124,6 +124,7 @@ BITCOIN_CORE_H = \
pow.h \
protocol.h \
random.h \
+ reverse_iterator.h \
reverselock.h \
rpc/blockchain.h \
rpc/client.h \
diff --git a/src/checkpoints.cpp b/src/checkpoints.cpp
index 13b5876530..7afc6a00f4 100644
--- a/src/checkpoints.cpp
+++ b/src/checkpoints.cpp
@@ -6,6 +6,7 @@
#include "chain.h"
#include "chainparams.h"
+#include "reverse_iterator.h"
#include "validation.h"
#include "uint256.h"
diff --git a/src/net_processing.cpp b/src/net_processing.cpp
index b9357440e9..ce61a28daf 100644
--- a/src/net_processing.cpp
+++ b/src/net_processing.cpp
@@ -22,6 +22,7 @@
#include "primitives/block.h"
#include "primitives/transaction.h"
#include "random.h"
+#include "reverse_iterator.h"
#include "tinyformat.h"
#include "txmempool.h"
#include "ui_interface.h"
diff --git a/src/reverse_iterator.h b/src/reverse_iterator.h
new file mode 100644
index 0000000000..409f895ce0
--- /dev/null
+++ b/src/reverse_iterator.h
@@ -0,0 +1,39 @@
+// Taken from https://gist.github.com/arvidsson/7231973
+
+#ifndef BITCOIN_REVERSE_ITERATOR_HPP
+#define BITCOIN_REVERSE_ITERATOR_HPP
+
+/**
+ * Template used for reverse iteration in C++11 range-based for loops.
+ *
+ * std::vector<int> v = {1, 2, 3, 4, 5};
+ * for (auto x : reverse_iterate(v))
+ * std::cout << x << " ";
+ */
+
+template <typename T>
+class reverse_range
+{
+ T &x;
+
+public:
+ reverse_range(T &x) : x(x) {}
+
+ auto begin() const -> decltype(this->x.rbegin())
+ {
+ return x.rbegin();
+ }
+
+ auto end() const -> decltype(this->x.rend())
+ {
+ return x.rend();
+ }
+};
+
+template <typename T>
+reverse_range<T> reverse_iterate(T &x)
+{
+ return reverse_range<T>(x);
+}
+
+#endif // BITCOIN_REVERSE_ITERATOR_HPP
diff --git a/src/test/prevector_tests.cpp b/src/test/prevector_tests.cpp
index 11bb11d1e9..7182caab93 100644
--- a/src/test/prevector_tests.cpp
+++ b/src/test/prevector_tests.cpp
@@ -5,6 +5,7 @@
#include <vector>
#include "prevector.h"
+#include "reverse_iterator.h"
#include "serialize.h"
#include "streams.h"
diff --git a/src/txmempool.cpp b/src/txmempool.cpp
index afafc695f4..02a549b123 100644
--- a/src/txmempool.cpp
+++ b/src/txmempool.cpp
@@ -11,6 +11,7 @@
#include "validation.h"
#include "policy/policy.h"
#include "policy/fees.h"
+#include "reverse_iterator.h"
#include "streams.h"
#include "timedata.h"
#include "util.h"
diff --git a/src/validation.cpp b/src/validation.cpp
index bef17337b9..8108da4c55 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -23,6 +23,7 @@
#include "primitives/block.h"
#include "primitives/transaction.h"
#include "random.h"
+#include "reverse_iterator.h"
#include "script/script.h"
#include "script/sigcache.h"
#include "script/standard.h"