thrust::transform_if
Defined in thrust/transform.h
-
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_ifconditionally 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
iin the range[first, last)the predicatepred(*i)is evaluated. If this predicate evaluates totrue, the result ofop(*i)is assigned to*o, whereois 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_ifto negate the odd-valued elements of a range using thethrust::hostexecution 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; } }; thrust::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};
See also
thrust::transform
- 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'svalue_typeis convertible toPredicate'sargument type, andInputIterator'svalue_typeis convertible toUnaryFunction'sargument type.ForwardIterator – is a model of Forward Iterator.
UnaryFunction – The function’s return type must be convertible to
OutputIterator'svalue_type.Predicate – is a model of Predicate.
- Returns
The end of the output sequence.
- Pre
firstmay equalresult, but the range[first, last)shall not overlap the range[result, result + (last - first))otherwise.