transform_if#

Overloads#

transform_if(exec, first, last, result, op, pred)#

template<typename DerivedPolicy, typename InputIterator, typename ForwardIterator, typename UnaryFunction, typename Predicate>
ForwardIterator thrust::transform_if(
const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
InputIterator first,
InputIterator last,
ForwardIterator result,
UnaryFunction op,
Predicate pred,
)#

This version of transform_if conditionally applies a unary function to each element of an input sequence and stores the result in the corresponding position in an output sequence if the corresponding position in the input sequence satisfies a predicate. Otherwise, the corresponding position in the output sequence is not modified.

Specifically, for each iterator i in the range [first, last) the predicate pred(*i) is evaluated. If this predicate evaluates to true, the result of op(*i) is assigned to *o, where o is the corresponding output iterator in the range [result, result + (last - first) ). Otherwise, op(*i) is not evaluated and no assignment occurs. The input and output sequences may coincide, resulting in an in-place transformation.

The algorithm’s execution is parallelized as determined by exec.

The following code snippet demonstrates how to use transform_if to negate the odd-valued elements of a range using the thrust::host execution policy for parallelization:

#include <thrust/transform.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...

int data[10]    = {-5, 0, 2, -3, 2, 4, 0, -1, 2, 8};

struct is_odd
{
  __host__ __device__
  bool operator()(int x)
  {
    return x % 2;
  }
};

::cuda::std::negate<int> op;

// negate odd elements
thrust::transform_if(thrust::host, data, data + 10, data, op, is_odd()); // in-place transformation

// data is now {5, 0, 2, 3, 2, 4, 0, 1, 2, 8};

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the input sequence.

  • last – The end of the input sequence.

  • result – The beginning of the output sequence.

  • op – The transformation operation. Relying on the address of op’s arguments in its implementation is deprecated.

  • pred – The predicate operation. Relying on the address of pred’s arguments in its implementation is deprecated.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument type, and InputIterator's value_type is convertible to UnaryFunction's argument type.

  • ForwardIterator – is a model of Forward Iterator.

  • UnaryFunction – The function’s return type must be convertible to OutputIterator's value_type.

  • Predicate – is a model of Predicate.

Returns:

The end of the output sequence.

Pre:

first may equal result, but the range [first, last) shall not overlap the range [result, result + (last - first)) otherwise.

transform_if(first, last, result, op, pred)#

template<typename InputIterator, typename ForwardIterator, typename UnaryFunction, typename Predicate>
ForwardIterator thrust::transform_if(
InputIterator first,
InputIterator last,
ForwardIterator result,
UnaryFunction op,
Predicate pred,
)#

This version of transform_if conditionally applies a unary function to each element of an input sequence and stores the result in the corresponding position in an output sequence if the corresponding position in the input sequence satisfies a predicate. Otherwise, the corresponding position in the output sequence is not modified.

Specifically, for each iterator i in the range [first, last) the predicate pred(*i) is evaluated. If this predicate evaluates to true, the result of op(*i) is assigned to *o, where o is the corresponding output iterator in the range [result, result + (last - first) ). Otherwise, op(*i) 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:

#include <thrust/transform.h>
#include <thrust/functional.h>

int data[10]    = {-5, 0, 2, -3, 2, 4, 0, -1, 2, 8};

struct is_odd
{
  __host__ __device__
  bool operator()(int x)
  {
    return x % 2;
  }
};

::cuda::std::negate<int> op;

// negate odd elements
thrust::transform_if(data, data + 10, data, op, is_odd()); // in-place transformation

// data is now {5, 0, 2, 3, 2, 4, 0, 1, 2, 8};

Parameters:
  • first – The beginning of the input sequence.

  • last – The end of the input sequence.

  • result – The beginning of the output sequence.

  • op – The transformation operation. Relying on the address of op’s arguments in its implementation is deprecated.

  • pred – The predicate operation. Relying on the address of pred’s arguments in its implementation is deprecated.

Template Parameters:
  • InputIterator – is a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument type, and InputIterator's value_type is convertible to UnaryFunction's argument type.

  • ForwardIterator – is a model of Forward Iterator.

  • UnaryFunction – The function’s return type must be convertible to OutputIterator's value_type.

  • Predicate – is a model of Predicate.

Returns:

The end of the output sequence.

Pre:

first may equal result, but the range [first, last) shall not overlap the range [result, result + (last - first)) otherwise.

transform_if(exec, first, last, stencil, result, op, pred)#

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename ForwardIterator, typename UnaryFunction, typename Predicate>
ForwardIterator thrust::transform_if(
const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
InputIterator1 first,
InputIterator1 last,
InputIterator2 stencil,
ForwardIterator result,
UnaryFunction op,
Predicate pred,
)#

This version of transform_if conditionally applies a unary function to each element of an input sequence and stores the result in the corresponding position in an output sequence if the corresponding position in a stencil sequence satisfies a predicate. Otherwise, the corresponding position in the output sequence is not modified.

Specifically, for each iterator i in the range [first, last) the predicate pred(*s) is evaluated, where s is the corresponding input iterator in the range [stencil, stencil + (last - first) ). If this predicate evaluates to true, the result of op(*i) is assigned to *o, where o is the corresponding output iterator in the range [result, result + (last - first) ). Otherwise, op(*i) is not evaluated and no assignment occurs. The input and output sequences may coincide, resulting in an in-place transformation.

The algorithm’s execution is parallelized as determined by exec.

The following code snippet demonstrates how to use transform_if using the thrust::host execution policy for parallelization:

#include <thrust/transform.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
...

int data[10]    = {-5, 0, 2, -3, 2, 4, 0, -1, 2, 8};
int stencil[10] = { 1, 0, 1,  0, 1, 0, 1,  0, 1, 0};

::cuda::std::negate<int> op;
::cuda::std::identity identity;

thrust::transform_if(thrust::host, data, data + 10, stencil, data, op, identity); // in-place transformation

// data is now {5, 0, -2, -3, -2,  4, 0, -1, -2,  8};

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The beginning of the input sequence.

  • last – The end of the input sequence.

  • stencil – The beginning of the stencil sequence.

  • result – The beginning of the output sequence.

  • op – The transformation operation. Relying on the address of op’s arguments in its implementation is deprecated.

  • pred – The predicate operation. Relying on the address of pred’s arguments in its implementation is deprecated.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator and InputIterator1's value_type is convertible to UnaryFunction's argument type.

  • InputIterator2 – is a model of Input Iterator and InputIterator2's value_type is convertible to Predicate's argument type.

  • ForwardIterator – is a model of Forward Iterator.

  • UnaryFunction – The function’s return type must be convertible to OutputIterator's value_type.

  • Predicate – is a model of Predicate.

Returns:

The end of the output sequence.

Pre:

first may equal result, but the range [first, last) shall not overlap the range [result, result + (last - first)) otherwise.

Pre:

stencil may equal result, but the range [stencil, stencil + (last - first)) shall not overlap the range [result, result + (last - first)) otherwise.

transform_if(first, last, stencil, result, op, pred)#

template<typename InputIterator1, typename InputIterator2, typename ForwardIterator, typename UnaryFunction, typename Predicate>
ForwardIterator thrust::transform_if(
InputIterator1 first,
InputIterator1 last,
InputIterator2 stencil,
ForwardIterator result,
UnaryFunction op,
Predicate pred,
)#

This version of transform_if conditionally applies a unary function to each element of an input sequence and stores the result in the corresponding position in an output sequence if the corresponding position in a stencil sequence satisfies a predicate. Otherwise, the corresponding position in the output sequence is not modified.

Specifically, for each iterator i in the range [first, last) the predicate pred(*s) is evaluated, where s is the corresponding input iterator in the range [stencil, stencil + (last - first) ). If this predicate evaluates to true, the result of op(*i) is assigned to *o, where o is the corresponding output iterator in the range [result, result + (last - first) ). Otherwise, op(*i) 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:

#include <thrust/transform.h>
#include <thrust/functional.h>

int data[10]    = {-5, 0, 2, -3, 2, 4, 0, -1, 2, 8};
int stencil[10] = { 1, 0, 1,  0, 1, 0, 1,  0, 1, 0};

::cuda::std::negate<int> op;
::cuda::std::identity identity;

thrust::transform_if(data, data + 10, stencil, data, op, identity); // in-place transformation

// data is now {5, 0, -2, -3, -2,  4, 0, -1, -2,  8};

Parameters:
  • first – The beginning of the input sequence.

  • last – The end of the input sequence.

  • stencil – The beginning of the stencil sequence.

  • result – The beginning of the output sequence.

  • op – The transformation operation. Relying on the address of op’s arguments in its implementation is deprecated.

  • pred – The predicate operation. Relying on the address of pred’s arguments in its implementation is deprecated.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator and InputIterator1's value_type is convertible to UnaryFunction's argument type.

  • InputIterator2 – is a model of Input Iterator and InputIterator2's value_type is convertible to Predicate's argument type.

  • ForwardIterator – is a model of Forward Iterator.

  • UnaryFunction – The function’s return type must be convertible to OutputIterator's value_type.

  • Predicate – is a model of Predicate.

Returns:

The end of the output sequence.

Pre:

first may equal result, but the range [first, last) shall not overlap the range [result, result + (last - first)) otherwise.

Pre:

stencil may equal result, but the range [stencil, stencil + (last - first)) shall not overlap the range [result, result + (last - first)) otherwise.

transform_if(exec, first1, last1, first2, stencil, result, binary_op, pred)#

template<typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename InputIterator3, typename ForwardIterator, typename BinaryFunction, typename Predicate>
ForwardIterator thrust::transform_if(
const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
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 satisfies 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.

The algorithm’s execution is parallelized as determined by exec.

The following code snippet demonstrates how to use transform_if using the thrust::host execution policy for parallelization:

#include <thrust/transform.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.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];

::cuda::std::plus<int> op;
::cuda::std::identity identity;

thrust::transform_if(thrust::host, input1, input1 + 6, input2, stencil, output, op, identity);

// output is now {-2,  0,  0,  3,  4,  4};

Parameters:
  • exec – The execution policy to use for parallelization.

  • 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. Relying on the address of pred’s arguments in its implementation is deprecated.

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • InputIterator1 – is a model of Input Iterator and InputIterator1's value_type is convertible to BinaryFunction's first argument type.

  • InputIterator2 – is a model of Input Iterator and InputIterator2's value_type is convertible to BinaryFunction's second argument type.

  • ForwardIterator – is a model of Forward Iterator.

  • BinaryFunction – The function’s return type must be convertible to OutputIterator's value_type.

  • Predicate – is a model of Predicate.

Returns:

The end of the output sequence.

Pre:

first1 may equal result, but the range [first1, last1) shall not overlap the range [result, result + (last1 - first1)) otherwise.

Pre:

first2 may equal result, but the range [first2, first2 + (last1 - first1)) shall not overlap the range [result, result + (last1 - first1)) otherwise.

Pre:

stencil may equal result, but the range [stencil, stencil + (last1 - first1)) shall not overlap the range [result, result + (last1 - first1)) otherwise.

transform_if(first1, last1, first2, stencil, result, binary_op, pred)#

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 satisfies 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.

The following code snippet demonstrates how to use transform_if:

#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];

::cuda::std::plus<int> op;
::cuda::std::identity identity;

thrust::transform_if(input1, input1 + 6, input2, stencil, output, op, identity);

// output is now {-2,  0,  0,  3,  4,  4};

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. Relying on the address of binary_op’s arguments in its implementation is deprecated.

  • pred – The predicate operation. Relying on the address of pred’s arguments in its implementation is deprecated.

Template Parameters:
  • InputIterator1 – is a model of Input Iterator and InputIterator1's value_type is convertible to BinaryFunction's first argument type.

  • InputIterator2 – is a model of Input Iterator and InputIterator2's value_type is convertible to BinaryFunction's second argument type.

  • ForwardIterator – is a model of Forward Iterator.

  • BinaryFunction – The function’s return type must be convertible to OutputIterator's value_type.

  • Predicate – is a model of Predicate.

Returns:

The end of the output sequence.

Pre:

first1 may equal result, but the range [first1, last1) shall not overlap the range [result, result + (last1 - first1)) otherwise.

Pre:

first2 may equal result, but the range [first2, first2 + (last1 - first1)) shall not overlap the range [result, result + (last1 - first1)) otherwise.

Pre:

stencil may equal result, but the range [stencil, stencil + (last1 - first1)) shall not overlap the range [result, result + (last1 - first1)) otherwise.