Template Function thrust::stable_partition(ForwardIterator, ForwardIterator, InputIterator, Predicate)¶
Function Documentation¶
-
template<typename
ForwardIterator, typenameInputIterator, typenamePredicate>
ForwardIteratorthrust::stable_partition(ForwardIterator first, ForwardIterator last, InputIterator stencil, Predicate pred) stable_partitionis much likepartition:it reorders the elements in the range[first, last)based on the function objectpredapplied to a stencil range[stencil, stencil + (last - first)), such that all of the elements whose corresponding stencil element satisfiespredprecede all of the elements whose corresponding stencil element fails to satisfy it. The postcondition is that, for some iteratormiddlein the range[first, last),pred(*stencil_i)istruefor every iteratorstencil_iin the range[stencil,stencil + (middle - first))andfalsefor every iteratorstencil_iin the range[stencil + (middle - first), stencil + (last - first)). The return value ofstable_partitionismiddle.stable_partitiondiffers from partition in thatstable_partitionis guaranteed to preserve relative order. That is, ifxandyare elements in[first, last), such thatpred(x) == pred(y), and ifxprecedesy, then it will still be true afterstable_partitionthatxprecedesy.The following code snippet demonstrates how to use
stable_partitionto reorder a sequence so that even numbers precede odd numbers.- Return
An iterator referring to the first element of the second partition, that is, the sequence of the elements whose stencil elements do not satisfy
pred.- Pre
The range
[first, last)shall not overlap with the range[stencil, stencil + (last - first)).- Parameters
first: The first element of the sequence to reorder.last: One position past the last element of the sequence to reorder.stencil: The beginning of the stencil sequence.pred: A function object which decides to which partition each element of the sequence[first, last)belongs.
- Template Parameters
ForwardIterator: is a model of Forward Iterator, andForwardIteratoris mutable.InputIterator: is a model of Input Iterator, andInputIterator'svalue_typeis convertible toPredicate'sargument_type.Predicate: is a model of Predicate.
#include <thrust/partition.h> ... struct is_even { __host__ __device__ bool operator()(const int &x) { return (x % 2) == 0; } }; ... int A[] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1}; int S[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; const int N = sizeof(A)/sizeof(int); thrust::stable_partition(A, A + N, S, is_even()); // A is now {1, 1, 1, 1, 1, 0, 0, 0, 0, 0} // S is unmodified
- See
- See
partition- See
stable_partition_copy