thrust
Public Member Functions | List of all members
thrust::reverse_iterator< BidirectionalIterator > Class Template Reference

#include <thrust/iterator/reverse_iterator.h>

Inherits type< BidirectionalIterator >.

Public Member Functions

__host__ __device__ reverse_iterator ()
 
__host__ __device__ reverse_iterator (BidirectionalIterator x)
 
template<typename OtherBidirectionalIterator >
__host__ __device__ reverse_iterator (reverse_iterator< OtherBidirectionalIterator > const &r, typename thrust::detail::enable_if< thrust::detail::is_convertible< OtherBidirectionalIterator, BidirectionalIterator >::value >::type *=0)
 

Detailed Description

template<typename BidirectionalIterator>
class thrust::reverse_iterator< 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.

...
thrust::device_vector<float> v(4);
v[0] = 0.0f;
v[1] = 1.0f;
v[2] = 2.0f;
v[3] = 3.0f;
// note that we point the iterator to the *end* of the device_vector
*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 typedefs for declaration shorthand and methods for constructing reverse_iterators. The following code snippet is equivalent to the previous:

...
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/scan.h>
...
thrust::device_vector<int> v(5);
v[0] = 0;
v[1] = 1;
v[2] = 2;
v[3] = 3;
v[4] = 4;
// 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

The documentation for this class was generated from the following file: