thrust
Public Member Functions | List of all members
thrust::transform_iterator< AdaptableUnaryFunction, Iterator, Reference, Value > Class Template Reference

#include <thrust/iterator/transform_iterator.h>

Inherits type< AdaptableUnaryFunction, Iterator, Reference, Value >.

Public Member Functions

__host__ __device__ transform_iterator ()
 
__host__ __device__ transform_iterator (Iterator const &x, AdaptableUnaryFunction f)
 
__host__ __device__ transform_iterator (Iterator const &x)
 
template<typename OtherAdaptableUnaryFunction , typename OtherIterator , typename OtherReference , typename OtherValue >
__host__ __device__ transform_iterator (const transform_iterator< OtherAdaptableUnaryFunction, OtherIterator, OtherReference, OtherValue > &other, typename thrust::detail::enable_if_convertible< OtherIterator, Iterator >::type *=0, typename thrust::detail::enable_if_convertible< OtherAdaptableUnaryFunction, AdaptableUnaryFunction >::type *=0)
 
__host__ __device__ transform_iteratoroperator= (const transform_iterator &other)
 
__host__ __device__ AdaptableUnaryFunction functor () const
 

Detailed Description

template<class AdaptableUnaryFunction, class Iterator, class Reference = use_default, class Value = use_default>
class thrust::transform_iterator< AdaptableUnaryFunction, Iterator, Reference, Value >

transform_iterator is an iterator which represents a pointer into a range of values after transformation by a function. This iterator is useful for creating a range filled with the result of applying an operation to another range without either explicitly storing it in memory, or explicitly executing the transformation. Using transform_iterator facilitates kernel fusion by deferring the execution of a transformation until the value is needed while saving both memory capacity and bandwidth.

The following code snippet demonstrates how to create a transform_iterator which represents the result of sqrtf applied to the contents of a device_vector.

// note: functor inherits from unary_function
struct square_root : public thrust::unary_function<float,float>
{
__host__ __device__
float operator()(float x) const
{
return sqrtf(x);
}
};
int main()
{
v[0] = 1.0f;
v[1] = 4.0f;
v[2] = 9.0f;
v[3] = 16.0f;
*iter; // returns 1.0f
iter[0]; // returns 1.0f;
iter[1]; // returns 2.0f;
iter[2]; // returns 3.0f;
iter[3]; // returns 4.0f;
// iter[4] is an out-of-bounds error
}

This next example demonstrates how to use a transform_iterator with the thrust::reduce function to compute the sum of squares of a sequence. We will create temporary transform_iterators with the make_transform_iterator function in order to avoid explicitly specifying their type:

#include <thrust/reduce.h>
#include <iostream>
// note: functor inherits from unary_function
struct square : public thrust::unary_function<float,float>
{
__host__ __device__
float operator()(float x) const
{
return x * x;
}
};
int main()
{
// initialize a device array
v[0] = 1.0f;
v[1] = 2.0f;
v[2] = 3.0f;
v[3] = 4.0f;
float sum_of_squares =
thrust::make_transform_iterator(v.end(), square()));
std::cout << "sum of squares: " << sum_of_squares << std::endl;
return 0;
}

Note that in the previous two examples the transform functor (namely square_root and square) inherits from thrust::unary_function. Inheriting from thrust::unary_function ensures that a functor is a valid AdaptableUnaryFunction and provides all the necessary typedef declarations. The transform_iterator can also be applied to a UnaryFunction that does not inherit from thrust::unary_function using an optional template argument. The following example illustrates how to use the third template argument to specify the result_type of the function.

// note: functor *does not* inherit from unary_function
struct square_root
{
__host__ __device__
float operator()(float x) const
{
return sqrtf(x);
}
};
int main()
{
v[0] = 1.0f;
v[1] = 4.0f;
v[2] = 9.0f;
v[3] = 16.0f;
// note: float result_type is specified explicitly
*iter; // returns 1.0f
iter[0]; // returns 1.0f;
iter[1]; // returns 2.0f;
iter[2]; // returns 3.0f;
iter[3]; // returns 4.0f;
// iter[4] is an out-of-bounds error
}
See also
make_transform_iterator

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