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>
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) ){ }
};
);
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;
};
);
int main() {
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);
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 ;
...
dv_trf_end,
dvInVec2.begin( ),
dvDestVec.begin( ),
bolt::cl::plus< int >( ) );
}