Skip to content

Commit 300851e

Browse files
committed
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
1 parent 1ad3d4e commit 300851e

File tree

7 files changed

+45
-0
lines changed

7 files changed

+45
-0
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ BITCOIN_CORE_H = \
124124
pow.h \
125125
protocol.h \
126126
random.h \
127+
reverse_iterator.h \
127128
reverselock.h \
128129
rpc/blockchain.h \
129130
rpc/client.h \

src/checkpoints.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "chain.h"
88
#include "chainparams.h"
9+
#include "reverse_iterator.h"
910
#include "validation.h"
1011
#include "uint256.h"
1112

src/net_processing.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "primitives/block.h"
2323
#include "primitives/transaction.h"
2424
#include "random.h"
25+
#include "reverse_iterator.h"
2526
#include "tinyformat.h"
2627
#include "txmempool.h"
2728
#include "ui_interface.h"

src/reverse_iterator.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Taken from https://gist.github.com/arvidsson/7231973
2+
3+
#ifndef BITCOIN_REVERSE_ITERATOR_HPP
4+
#define BITCOIN_REVERSE_ITERATOR_HPP
5+
6+
/**
7+
* Template used for reverse iteration in C++11 range-based for loops.
8+
*
9+
* std::vector<int> v = {1, 2, 3, 4, 5};
10+
* for (auto x : reverse_iterate(v))
11+
* std::cout << x << " ";
12+
*/
13+
14+
template <typename T>
15+
class reverse_range
16+
{
17+
T &x;
18+
19+
public:
20+
reverse_range(T &x) : x(x) {}
21+
22+
auto begin() const -> decltype(this->x.rbegin())
23+
{
24+
return x.rbegin();
25+
}
26+
27+
auto end() const -> decltype(this->x.rend())
28+
{
29+
return x.rend();
30+
}
31+
};
32+
33+
template <typename T>
34+
reverse_range<T> reverse_iterate(T &x)
35+
{
36+
return reverse_range<T>(x);
37+
}
38+
39+
#endif // BITCOIN_REVERSE_ITERATOR_HPP

src/test/prevector_tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <vector>
66
#include "prevector.h"
77

8+
#include "reverse_iterator.h"
89
#include "serialize.h"
910
#include "streams.h"
1011

src/txmempool.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "validation.h"
1212
#include "policy/policy.h"
1313
#include "policy/fees.h"
14+
#include "reverse_iterator.h"
1415
#include "streams.h"
1516
#include "timedata.h"
1617
#include "util.h"

src/validation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "primitives/block.h"
2424
#include "primitives/transaction.h"
2525
#include "random.h"
26+
#include "reverse_iterator.h"
2627
#include "script/script.h"
2728
#include "script/sigcache.h"
2829
#include "script/standard.h"

0 commit comments

Comments
 (0)