diff --git a/src/Makefile.am b/src/Makefile.am index 4354f2d36f..e89720f9ba 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -194,6 +194,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 fd6666c517..42c55756a0 100644 --- a/src/checkpoints.cpp +++ b/src/checkpoints.cpp @@ -7,6 +7,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 abe7a0aeea..0afc0e8e7b 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 v = {1, 2, 3, 4, 5}; + * for (auto x : reverse_iterate(v)) + * std::cout << x << " "; + */ + +template +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 +reverse_range reverse_iterate(T &x) +{ + return reverse_range(x); +} + +#endif // BITCOIN_REVERSE_ITERATOR_HPP diff --git a/src/test/prevector_tests.cpp b/src/test/prevector_tests.cpp index b0e9df8bb6..24524cf845 100644 --- a/src/test/prevector_tests.cpp +++ b/src/test/prevector_tests.cpp @@ -5,6 +5,7 @@ #include #include "prevector.h" +#include "reverse_iterator.h" #include "serialize.h" #include "streams.h" diff --git a/src/txmempool.cpp b/src/txmempool.cpp index d80d36de21..a0a1393d86 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -12,6 +12,7 @@ #include "policy/policy.h" #include "policy/fees.h" #include "random.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 50d677075d..461616ae25 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -24,6 +24,7 @@ #include "pow.h" #include "primitives/block.h" #include "primitives/transaction.h" +#include "reverse_iterator.h" #include "script/script.h" #include "script/sigcache.h" #include "script/standard.h"