thrust::reverse_iterator

Defined in thrust/iterator/reverse_iterator.h

template<typename BidirectionalIterator>
class reverse_iterator : public detail::reverse_iterator_base::type<BidirectionalIterator>

reverse_iterator is an iterator which represents a pointer into a reversed view of a given range. In this way, reverse_iterator allows backwards iteration through a bidirectional input range.

It is important to note that although reverse_iterator is constructed from a given iterator, it points to the element preceding it. In this way, the past-the-end reverse_iterator of a given range points to the element preceding the first element of the input range. By the same token, the first reverse_iterator of a given range is constructed from a past-the-end iterator of the original range yet points to the last element of the input.

The following code snippet demonstrates how to create a reverse_iterator which represents a reversed view of the contents of a device_vector.

#include <thrust/iterator/reverse_iterator.h>
#include <thrust/device_vector.h>
...
thrust::device_vector<float> v(4);
v[0] = 0.0f;
v[1] = 1.0f;
v[2] = 2.0f;
v[3] = 3.0f;

using Iterator = thrust::device_vector<float>::iterator;

// note that we point the iterator to the *end* of the device_vector
thrust::reverse_iterator<Iterator> iter(values.end());

*iter;   // returns 3.0f;
iter[0]; // returns 3.0f;
iter[1]; // returns 2.0f;
iter[2]; // returns 1.0f;
iter[3]; // returns 0.0f;

// iter[4] is an out-of-bounds error

Since reversing a range is a common operation, containers like device_vector have nested aliases for declaration shorthand and methods for constructing reverse_iterators. The following code snippet is equivalent to the previous:

#include <thrust/device_vector.h>
...
thrust::device_vector<float> v(4);
v[0] = 0.0f;
v[1] = 1.0f;
v[2] = 2.0f;
v[3] = 3.0f;

// we use the nested type reverse_iterator to refer to a reversed view of
// a device_vector and the method rbegin() to create a reverse_iterator pointing
// to the beginning of the reversed device_vector
thrust::device_iterator<float>::reverse_iterator iter = values.rbegin();

*iter;   // returns 3.0f;
iter[0]; // returns 3.0f;
iter[1]; // returns 2.0f;
iter[2]; // returns 1.0f;
iter[3]; // returns 0.0f;

// iter[4] is an out-of-bounds error

// similarly, rend() points to the end of the reversed sequence:
assert(values.rend() == (iter + 4));

Finally, the following code snippet demonstrates how to use reverse_iterator to perform a reversed prefix sum operation on the contents of a device_vector:

#include <thrust/device_vector.h>
#include <thrust/scan.h>
...
thrust::device_vector<int> v(5);
v[0] = 0;
v[1] = 1;
v[2] = 2;
v[3] = 3;
v[4] = 4;

thrust::device_vector<int> result(5);

// exclusive scan v into result in reverse
thrust::exclusive_scan(v.rbegin(), v.rend(), result.begin());

// result is now {0, 4, 7, 9, 10}

See also

make_reverse_iterator

Public Functions

reverse_iterator() = default

Default constructor does nothing.

inline explicit reverse_iterator(BidirectionalIterator x)

Constructor accepts a BidirectionalIterator pointing to a range for this reverse_iterator to reverse.

Parameters

x – A BidirectionalIterator pointing to a range to reverse.

template<typename OtherBidirectionalIterator, detail::enable_if_convertible_t<OtherBidirectionalIterator, BidirectionalIterator, int> = 0>
inline reverse_iterator(reverse_iterator<OtherBidirectionalIterator> const &rhs)

Copy constructor allows construction from a related compatible reverse_iterator.

Parameters

r – A reverse_iterator to copy from.