thrust

◆ partition_copy() [3/4]

template<typename DerivedPolicy , typename InputIterator1 , typename InputIterator2 , typename OutputIterator1 , typename OutputIterator2 , typename Predicate >
__host__ __device__ thrust::pair<OutputIterator1,OutputIterator2> thrust::partition_copy ( const thrust::detail::execution_policy_base< DerivedPolicy > &  exec,
InputIterator1  first,
InputIterator1  last,
InputIterator2  stencil,
OutputIterator1  out_true,
OutputIterator2  out_false,
Predicate  pred 
)

partition_copy differs from partition only in that the reordered sequence is written to difference output sequences, rather than in place.

partition_copy copies the elements [first, last) based on the function object pred which is applied to a range of stencil elements. All of the elements whose corresponding stencil element satisfies pred are copied to the range beginning at out_true and all the elements whose stencil element fails to satisfy it are copied to the range beginning at out_false.

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

Parameters
execThe execution policy to use for parallelization.
firstThe beginning of the sequence to reorder.
lastThe end of the sequence to reorder.
stencilThe beginning of the stencil sequence.
out_trueThe destination of the resulting sequence of elements which satisfy pred.
out_falseThe destination of the resulting sequence of elements which fail to satisfy pred.
predA function object which decides to which partition each element of the sequence [first, last) belongs.
Returns
A pair p such that p.first is the end of the output range beginning at out_true and p.second is the end of the output range beginning at out_false.
Template Parameters
DerivedPolicyThe name of the derived execution policy.
InputIterator1is a model of Input Iterator, and InputIterator's value_type is convertible to OutputIterator1 and OutputIterator2's value_types.
InputIterator2is a model of Input Iterator, and InputIterator2's value_type is convertible to Predicate's argument_type.
OutputIterator1is a model of Output Iterator.
OutputIterator2is a model of Output Iterator.
Predicateis a model of Predicate.
Precondition
The input ranges shall not overlap with either output range.

The following code snippet demonstrates how to use partition_copy to separate a sequence into two output sequences of even and odd numbers using the thrust::host execution policy for parallelization.

...
int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int S[] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
int result[10];
const int N = sizeof(A)/sizeof(int);
int *evens = result;
int *odds = result + 5;
// A remains {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// S remains {0, 1, 0, 1, 0, 1, 0, 1, 0, 1}
// result is now {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}
// evens points to {2, 4, 6, 8, 10}
// odds points to {1, 3, 5, 7, 9}
Note
The relative order of elements in the two reordered sequences is not necessarily the same as it was in the original sequence. A different algorithm, stable_partition_copy, does guarantee to preserve the relative order.
See also
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2569.pdf
stable_partition_copy
partition