thrust

◆ transform_if() [6/6]

template<typename InputIterator1 , typename InputIterator2 , typename InputIterator3 , typename ForwardIterator , typename BinaryFunction , typename Predicate >
ForwardIterator thrust::transform_if ( InputIterator1  first1,
InputIterator1  last1,
InputIterator2  first2,
InputIterator3  stencil,
ForwardIterator  result,
BinaryFunction  binary_op,
Predicate  pred 
)

This version of transform_if conditionally 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 i in the range [first1, last1) and j = first2 + (i - first1) in the range [first2, first2 + (last1 - first1) ), the predicate pred(*s) is evaluated, where s is the corresponding input iterator in the range [stencil, stencil + (last1 - first1) ). If this predicate evaluates to true, the result of binary_op(*i,*j) is assigned to *o, where o is 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.

Parameters
first1The beginning of the first input sequence.
last1The end of the first input sequence.
first2The beginning of the second input sequence.
stencilThe beginning of the stencil sequence.
resultThe beginning of the output sequence.
binary_opThe transformation operation.
predThe predicate operation.
Returns
The end of the output sequence.
Template Parameters
InputIterator1is a model of Input Iterator and InputIterator1's value_type is convertible to BinaryFunction's first_argument_type.
InputIterator2is a model of Input Iterator and InputIterator2's value_type is convertible to BinaryFunction's second_argument_type.
ForwardIteratoris a model of Forward Iterator.
BinaryFunctionis a model of Binary Function and BinaryFunction's result_type is convertible to OutputIterator's value_type.
Predicateis a model of Predicate.
Precondition
first1 may equal result, but the range [first1, last1) shall not overlap the range [result, result + (last1 - first1)) otherwise.
first2 may equal result, but the range [first2, first2 + (last1 - first1)) shall not overlap the range [result, result + (last1 - first1)) otherwise.
stencil may equal result, but the range [stencil, stencil + (last1 - first1)) shall not overlap the range [result, result + (last1 - first1)) otherwise.

The following code snippet demonstrates how to use transform_if:

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::transform_if(input1, input1 + 6, input2, stencil, output, op, identity);
// output is now {-2, 0, 0, 3, 4, 4};
See also
thrust::transform