Thrust
List of all members
thrust::counting_iterator< Incrementable, System, Traversal, Difference > Class Template Reference

#include <thrust/iterator/counting_iterator.h>

Detailed Description

template<typename Incrementable, typename System = use_default, typename Traversal = use_default, typename Difference = use_default>
class thrust::counting_iterator< Incrementable, System, Traversal, Difference >

counting_iterator is an iterator which represents a pointer into a range of sequentially changing values. This iterator is useful for creating a range filled with a sequence without explicitly storing it in memory. Using counting_iterator saves memory capacity and bandwidth.

The following code snippet demonstrates how to create a counting_iterator whose value_type is int and which sequentially increments by 1.

...
// create iterators
thrust::counting_iterator<int> first(10);
first[0] // returns 10
first[1] // returns 11
first[100] // returns 110
// sum of [first, last)
thrust::reduce(first, last); // returns 33 (i.e. 10 + 11 + 12)
// initialize vector to [0,1,2,..]
thrust::copy(iter, iter + vec.size(), vec.begin());

This next example demonstrates how to use a counting_iterator with the thrust::copy_if function to compute the indices of the non-zero elements of a device_vector. In this example, we use the make_counting_iterator function to avoid specifying the type of the counting_iterator.

#include <thrust/copy.h>
int main()
{
// this example computes indices for all the nonzero values in a sequence
// sequence of zero and nonzero values
stencil[0] = 0;
stencil[1] = 1;
stencil[2] = 1;
stencil[3] = 0;
stencil[4] = 0;
stencil[5] = 1;
stencil[6] = 0;
stencil[7] = 1;
// storage for the nonzero indices
// compute indices of nonzero elements
typedef thrust::device_vector<int>::iterator IndexIterator;
// use make_counting_iterator to define the sequence [0, 8)
IndexIterator indices_end = thrust::copy_if(thrust::make_counting_iterator(0),
stencil.begin(),
indices.begin(),
// indices now contains [1,2,5,7]
return 0;
}
See also
make_counting_iterator

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