Template Function thrust::transform_if(InputIterator1, InputIterator1, InputIterator2, InputIterator3, ForwardIterator, BinaryFunction, Predicate)¶
Function Documentation¶
-
template<typename
InputIterator1, typenameInputIterator2, typenameInputIterator3, typenameForwardIterator, typenameBinaryFunction, typenamePredicate>
ForwardIteratorthrust::transform_if(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator3 stencil, ForwardIterator result, BinaryFunction binary_op, Predicate pred) This version of
transform_ifconditionally applies a binary function to each pair of elements from two input sequences and stores the result in the corresponding position in an output sequence if the corresponding position in a stencil sequence satifies a predicate. Otherwise, the corresponding position in the output sequence is not modified.Specifically, for each iterator
iin the range[first1, last1)andj = first2 + (i - first1)in the range[first2, first2 + (last1 - first1) ), the predicatepred(*s)is evaluated, wheresis the corresponding input iterator in the range[stencil, stencil + (last1 - first1) ). If this predicate evaluates totrue, the result ofbinary_op(*i,*j)is assigned to*o, whereois the corresponding output iterator in the range[result, result + (last1 - first1) ). Otherwise,binary_op(*i,*j)is not evaluated and no assignment occurs. The input and output sequences may coincide, resulting in an in-place transformation.The following code snippet demonstrates how to use
transform_if:- Return
The end of the output sequence.
- Pre
first1may equalresult, but the range[first1, last1)shall not overlap the range[result, result + (last1 - first1))otherwise.- Pre
first2may equalresult, but the range[first2, first2 + (last1 - first1))shall not overlap the range[result, result + (last1 - first1))otherwise.- Pre
stencilmay equalresult, but the range[stencil, stencil + (last1 - first1))shall not overlap the range[result, result + (last1 - first1))otherwise.- Parameters
first1: The beginning of the first input sequence.last1: The end of the first input sequence.first2: The beginning of the second input sequence.stencil: The beginning of the stencil sequence.result: The beginning of the output sequence.binary_op: The transformation operation.pred: The predicate operation.
- Template Parameters
InputIterator1: is a model of Input Iterator andInputIterator1'svalue_typeis convertible toBinaryFunction'sfirst_argument_type.InputIterator2: is a model of Input Iterator andInputIterator2'svalue_typeis convertible toBinaryFunction'ssecond_argument_type.ForwardIterator: is a model of Forward Iterator.BinaryFunction: is a model of Binary Function andBinaryFunction'sresult_typeis convertible toOutputIterator'svalue_type.Predicate: is a model of Predicate.
#include <thrust/transform.h> #include <thrust/functional.h> int input1[6] = {-5, 0, 2, 3, 2, 4}; int input2[6] = { 3, 6, -2, 1, 2, 3}; int stencil[8] = { 1, 0, 1, 0, 1, 0}; int output[6]; thrust::plus<int> op; thrust::identity<int> identity; thrust::transform_if(input1, input1 + 6, input2, stencil, output, op, identity); // output is now {-2, 0, 0, 3, 4, 4};
- See
thrust::transform