Bolt  1.3
C++ template library with support for OpenCL
transform_iterator< UnaryFunction, Iterator, Reference, Value > Class Template Reference

#include <transform_iterator.h>

Detailed Description

template<class UnaryFunction, class Iterator, class Reference = use_default, class Value = use_default>
class transform_iterator< UnaryFunction, Iterator, Reference, Value >

transform iterator adapts an iterator by modifying the operator* to apply a function object to the result of dereferencing the iterator and returning the result..

The following example demonstrates how to use a transform_iterator.

#include <bolt/cl/iterator/transform_iterator.h>
//For OpenCL, Transform Iterator macro should be created using the macro BOLT_TEMPLATE_REGISTER_NEW_TRANSFORM_ITERATOR
//The example here uses a device_vector iterators.
struct UDD
{
int i;
float f;
UDD operator = (const int rhs)
{
UDD _result;
_result.i = i + rhs;
_result.f = f + (float)rhs;
return _result;
}
UDD operator + (const UDD &rhs) const
{
UDD _result;
_result.i = this->i + rhs.i;
_result.f = this->f + rhs.f;
return _result;
}
UDD()
: i(0), f(0) { }
UDD(int _in)
: i(_in), f((float)(_in+2) ){ }
};
);
BOLT_FUNCTOR(UDDadd_3,
struct UDDadd_3
{
UDD operator() (const UDD &x) const
{
UDD temp;
temp.i = x.i + 3;
temp.f = x.f + 3.0f;
return temp;
}
typedef UDD result_type;
//Note that the result_type needs to be defined and should be type-defined to the
//return type of operator () overload.
};
);
BOLT_TEMPLATE_REGISTER_NEW_TYPE(bolt::cl::plus, int, UDD );
int main() {
// Create device_vectors
bolt::cl::device_vector< UDD > dvDestVec( 5, 0 );
UDDadd_3 add3;
typedef bolt::BCKND::transform_iterator< UDDadd_3, bolt::BCKND::device_vector< UDD >::iterator> dv_trf_itr_add3;
dv_trf_itr_add3 dv_trf_begin (dvInVec1.begin(), add3), dv_trf_end (dvInVec1.end(), add3);
// Fill values
dvInVec1[ 0 ] = 10 ; dvInVec1[ 1 ] = 15 ; dvInVec1[ 2 ] = 20 ;
dvInVec1[ 3 ] = 25 ; dvInVec1[ 4 ] = 30 ;
dvInVec2[ 0 ] = 10 ; dvInVec2[ 1 ] = 15 ; dvInVec2[ 2 ] = 20 ;
dvInVec2[ 3 ] = 25 ; dvInVec2[ 4 ] = 30 ;
...
bolt::cl::transform(dv_trf_begin,
dv_trf_end,
dvInVec2.begin( ),
dvDestVec.begin( ),
bolt::cl::plus< int >( ) );
}

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