2017-04-13 00:24:40 +02:00
|
|
|
// Taken from https://gist.github.com/arvidsson/7231973
|
|
|
|
|
2017-08-09 12:42:04 +02:00
|
|
|
#ifndef BITCOIN_REVERSE_ITERATOR_H
|
|
|
|
#define BITCOIN_REVERSE_ITERATOR_H
|
2017-04-13 00:24:40 +02:00
|
|
|
|
|
|
|
/**
|
2023-01-12 16:49:25 +01:00
|
|
|
* Template used for reverse iteration in range-based for loops.
|
2020-07-29 03:23:12 +02:00
|
|
|
*
|
2017-04-13 00:24:40 +02:00
|
|
|
* std::vector<int> v = {1, 2, 3, 4, 5};
|
|
|
|
* for (auto x : reverse_iterate(v))
|
|
|
|
* std::cout << x << " ";
|
|
|
|
*/
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class reverse_range
|
|
|
|
{
|
2017-08-09 12:42:04 +02:00
|
|
|
T &m_x;
|
2020-07-29 03:23:12 +02:00
|
|
|
|
2017-04-13 00:24:40 +02:00
|
|
|
public:
|
2017-08-17 22:59:56 +02:00
|
|
|
explicit reverse_range(T &x) : m_x(x) {}
|
2020-07-29 03:23:12 +02:00
|
|
|
|
2017-08-09 12:42:04 +02:00
|
|
|
auto begin() const -> decltype(this->m_x.rbegin())
|
2017-04-13 00:24:40 +02:00
|
|
|
{
|
2017-08-09 12:42:04 +02:00
|
|
|
return m_x.rbegin();
|
2017-04-13 00:24:40 +02:00
|
|
|
}
|
2020-07-29 03:23:12 +02:00
|
|
|
|
2017-08-09 12:42:04 +02:00
|
|
|
auto end() const -> decltype(this->m_x.rend())
|
2017-04-13 00:24:40 +02:00
|
|
|
{
|
2017-08-09 12:42:04 +02:00
|
|
|
return m_x.rend();
|
2017-04-13 00:24:40 +02:00
|
|
|
}
|
|
|
|
};
|
2020-07-29 03:23:12 +02:00
|
|
|
|
2017-04-13 00:24:40 +02:00
|
|
|
template <typename T>
|
|
|
|
reverse_range<T> reverse_iterate(T &x)
|
|
|
|
{
|
|
|
|
return reverse_range<T>(x);
|
|
|
|
}
|
|
|
|
|
2017-08-09 12:42:04 +02:00
|
|
|
#endif // BITCOIN_REVERSE_ITERATOR_H
|