diff options
| author | Jorge Timón <[email protected]> | 2017-04-13 00:24:40 +0200 |
|---|---|---|
| committer | Jorge Timón <[email protected]> | 2017-06-22 03:48:42 +0200 |
| commit | 300851ec1690e545485e154b1a4df55bec7621ae (patch) | |
| tree | 55acdf3fb3461030a5dde793c3064d5a27a079c8 /src/reverse_iterator.h | |
| parent | Merge #10502: scripted-diff: Remove BOOST_FOREACH, Q_FOREACH and PAIRTYPE (diff) | |
| download | discoin-300851ec1690e545485e154b1a4df55bec7621ae.tar.xz discoin-300851ec1690e545485e154b1a4df55bec7621ae.zip | |
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
Diffstat (limited to 'src/reverse_iterator.h')
| -rw-r--r-- | src/reverse_iterator.h | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/reverse_iterator.h b/src/reverse_iterator.h new file mode 100644 index 000000000..409f895ce --- /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 |