thrust::stable_partition_copy
Defined in thrust/partition.h
-
template<typename DerivedPolicy, typename InputIterator, typename OutputIterator1, typename OutputIterator2, typename Predicate>
thrust::pair<OutputIterator1, OutputIterator2> thrust::stable_partition_copy(const thrust::detail::execution_policy_base<DerivedPolicy> &exec, InputIterator first, InputIterator last, OutputIterator1 out_true, OutputIterator2 out_false, Predicate pred) stable_partition_copy
differs fromstable_partition
only in that the reordered sequence is written to different output sequences, rather than in place.stable_partition_copy
copies the elements[first, last)
based on the function objectpred
. All of the elements that satisfypred
are copied to the range beginning atout_true
and all the elements that fail to satisfy it are copied to the range beginning atout_false
.stable_partition_copy
differs frompartition_copy
in thatstable_partition_copy
is guaranteed to preserve relative order. That is, ifx
andy
are elements in[first, last)
, such thatpred(x) == pred(y)
, and ifx
precedesy
, then it will still be true afterstable_partition_copy
thatx
precedesy
in the output.The algorithm’s execution is parallelized as determined by
exec
.The following code snippet demonstrates how to use
stable_partition_copy
to reorder a sequence so that even numbers precede odd numbers using thethrust::host
execution policy for parallelization:#include <thrust/partition.h> #include <thrust/execution_policy.h> ... struct is_even { __host__ __device__ bool operator()(const int &x) { return (x % 2) == 0; } }; ... int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int result[10]; const int N = sizeof(A)/sizeof(int); int *evens = result; int *odds = result + 5; thrust::stable_partition_copy(thrust::host, A, A + N, evens, odds, is_even()); // A remains {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} // 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}
See also
partition_copy
See also
stable_partition
- Parameters
exec – The execution policy to use for parallelization.
first – The first element of the sequence to reorder.
last – One position past the last element of the sequence to reorder.
out_true – The destination of the resulting sequence of elements which satisfy
pred
.out_false – The destination of the resulting sequence of elements which fail to satisfy
pred
.pred – A function object which decides to which partition each element of the sequence
[first, last)
belongs.
- Template Parameters
DerivedPolicy – The name of the derived execution policy.
InputIterator – is a model of Input Iterator, and
InputIterator's
value_type
is convertible toPredicate's
argument_type
andInputIterator's
value_type
is convertible toOutputIterator1
andOutputIterator2's
value_types
.OutputIterator1 – is a model of Output Iterator.
OutputIterator2 – is a model of Output Iterator.
Predicate – is a model of Predicate.
- Returns
A
pair
p such thatp.first
is the end of the output range beginning atout_true
andp.second
is the end of the output range beginning atout_false
.- Pre
The input ranges shall not overlap with either output range.