Files
cppdraft_translate/cppdraft/alg/partitions.md
2025-10-25 03:02:53 +03:00

27 KiB
Raw Blame History

[alg.partitions]

26 Algorithms library [algorithms]

26.8.5 Partitions [alg.partitions]

🔗

`template<class InputIterator, class Predicate> constexpr bool is_partitioned(InputIterator first, InputIterator last, Predicate pred); template<class ExecutionPolicy, class ForwardIterator, class Predicate> bool is_partitioned(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, Predicate pred);

template<input_iterator I, sentinel_for S, class Proj = identity, indirect_unary_predicate<projected<I, Proj>> Pred> constexpr bool ranges::is_partitioned(I first, S last, Pred pred, Proj proj = {}); template<input_range R, class Proj = identity, indirect_unary_predicate<projected<iterator_t, Proj>> Pred> constexpr bool ranges::is_partitioned(R&& r, Pred pred, Proj proj = {});

template<execution-policy Ep, random_access_iterator I, sized_sentinel_for S, class Proj = identity, indirect_unary_predicate<projected<I, Proj>> Pred> bool ranges::is_partitioned(Ep&& exec, I first, S last, Pred pred, Proj proj = {}); template<execution-policy Ep, sized-random-access-range R, class Proj = identity, indirect_unary_predicate<projected<iterator_t, Proj>> Pred> bool ranges::is_partitioned(Ep&& exec, R&& r, Pred pred, Proj proj = {}); `

1

#

Let proj be identity{} for the overloads with no parameter named proj.

2

#

Returns: true if and only if the elements e of [first, last) are partitioned with respect to the expressionbool(invoke(pred, invoke(proj, e))).

3

#

Complexity: Linear.

At most last - first applications of pred and proj.

🔗

`template<class ForwardIterator, class Predicate> constexpr ForwardIterator partition(ForwardIterator first, ForwardIterator last, Predicate pred); template<class ExecutionPolicy, class ForwardIterator, class Predicate> ForwardIterator partition(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, Predicate pred);

template<permutable I, sentinel_for S, class Proj = identity, indirect_unary_predicate<projected<I, Proj>> Pred> constexpr subrange ranges::partition(I first, S last, Pred pred, Proj proj = {}); template<forward_range R, class Proj = identity, indirect_unary_predicate<projected<iterator_t, Proj>> Pred> requires permutable<iterator_t> constexpr borrowed_subrange_t ranges::partition(R&& r, Pred pred, Proj proj = {});

template<execution-policy Ep, random_access_iterator I, sized_sentinel_for S, class Proj = identity, indirect_unary_predicate<projected<I, Proj>> Pred> subrange ranges::partition(Ep&& exec, I first, S last, Pred pred, Proj proj = {}); template<execution-policy Ep, sized-random-access-range R, class Proj = identity, indirect_unary_predicate<projected<iterator_t, Proj>> Pred> requires permutable<iterator_t> borrowed_subrange_t ranges::partition(Ep&& exec, R&& r, Pred pred, Proj proj = {}); `

4

#

Let proj be identity{} for the overloads with no parameter named proj and let E(x) be bool(invoke(pred, invoke(proj, x))).

5

#

Preconditions: For the overloads in namespace std,ForwardIterator meets the Cpp17ValueSwappable requirements ([swappable.requirements]).

6

#

Effects: Places all the elements e in [first, last) that satisfy E(e) before all the elements that do not.

7

#

Returns: Let i be an iterator such that E(*j) istrue for every iterator j in [first, i) andfalse for every iterator j in [i, last).

Returns:

  • (7.1)

    i for the overloads in namespace std.

  • (7.2)

    {i, last} for the overloads in namespace ranges.

8

#

Complexity: Let N=last - first:

  • (8.1)

    For the non-parallel algorithm overloads, exactly N applications of the predicate and projection. At most N/2 swaps if the type of first meets the Cpp17BidirectionalIterator requirements for the overloads in namespace std or models bidirectional_iterator for the overloads in namespace ranges, and at most N swaps otherwise.

  • (8.2)

    For the parallel algorithm overloads, O(NlogN) swaps and O(N) applications of the predicate.

🔗

`template<class BidirectionalIterator, class Predicate> BidirectionalIterator constexpr stable_partition(BidirectionalIterator first, BidirectionalIterator last, Predicate pred); template<class ExecutionPolicy, class BidirectionalIterator, class Predicate> BidirectionalIterator stable_partition(ExecutionPolicy&& exec, BidirectionalIterator first, BidirectionalIterator last, Predicate pred);

template<bidirectional_iterator I, sentinel_for S, class Proj = identity, indirect_unary_predicate<projected<I, Proj>> Pred> requires permutable constexpr subrange ranges::stable_partition(I first, S last, Pred pred, Proj proj = {}); template<bidirectional_range R, class Proj = identity, indirect_unary_predicate<projected<iterator_t, Proj>> Pred> requires permutable<iterator_t> constexpr borrowed_subrange_t ranges::stable_partition(R&& r, Pred pred, Proj proj = {});

template<execution-policy Ep, random_access_iterator I, sized_sentinel_for S, class Proj = identity, indirect_unary_predicate<projected<I, Proj>> Pred> requires permutable subrange ranges::stable_partition(Ep&& exec, I first, S last, Pred pred, Proj proj = {}); template<execution-policy Ep, sized-random-access-range R, class Proj = identity, indirect_unary_predicate<projected<iterator_t, Proj>> Pred> requires permutable<iterator_t> borrowed_subrange_t ranges::stable_partition(Ep&& exec, R&& r, Pred pred, Proj proj = {}); `

9

#

Let proj be identity{} for the overloads with no parameter named proj and let E(x) be bool(invoke(pred, invoke(proj, x))).

10

#

Preconditions: For the overloads in namespace std,BidirectionalIterator meets the Cpp17ValueSwappable requirements ([swappable.requirements]) and the type of *first meets the Cpp17MoveConstructible (Table 31) andCpp17MoveAssignable (Table 33) requirements.

11

#

Effects: Places all the elements e in [first, last) that satisfy E(e) before all the elements that do not.

The relative order of the elements in both groups is preserved.

12

#

Returns: Let i be an iterator such that for every iterator j in [first, i),E(*j) is true, and for every iterator j in the range [i, last),E(*j) is false.

Returns:

  • (12.1)

    i for the overloads in namespace std.

  • (12.2)

    {i, last} for the overloads in namespace ranges.

13

#

Complexity: Let N = last - first:

  • (13.1)

    For the non-parallel algorithm overloads, at most Nlog2N swaps, but only O(N) swaps if there is enough extra memory. Exactly N applications of the predicate and projection.

  • (13.2)

    For the parallel algorithm overloads, O(NlogN) swaps and O(N) applications of the predicate.

🔗

`template<class InputIterator, class OutputIterator1, class OutputIterator2, class Predicate> constexpr pair<OutputIterator1, OutputIterator2> partition_copy(InputIterator first, InputIterator last, OutputIterator1 out_true, OutputIterator2 out_false, Predicate pred); template<class ExecutionPolicy, class ForwardIterator, class ForwardIterator1, class ForwardIterator2, class Predicate> pair<ForwardIterator1, ForwardIterator2> partition_copy(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, ForwardIterator1 out_true, ForwardIterator2 out_false, Predicate pred);

template<input_iterator I, sentinel_for S, weakly_incrementable O1, weakly_incrementable O2, class Proj = identity, indirect_unary_predicate<projected<I, Proj>> Pred> requires indirectly_copyable<I, O1> && indirectly_copyable<I, O2> constexpr ranges::partition_copy_result<I, O1, O2> ranges::partition_copy(I first, S last, O1 out_true, O2 out_false, Pred pred, Proj proj = {}); template<input_range R, weakly_incrementable O1, weakly_incrementable O2, class Proj = identity, indirect_unary_predicate<projected<iterator_t, Proj>> Pred> requires indirectly_copyable<iterator_t, O1> && indirectly_copyable<iterator_t, O2> constexpr ranges::partition_copy_result<borrowed_iterator_t, O1, O2> ranges::partition_copy(R&& r, O1 out_true, O2 out_false, Pred pred, Proj proj = {});

template<execution-policy Ep, random_access_iterator I, sized_sentinel_for S, random_access_iterator O1, sized_sentinel_for OutS1, random_access_iterator O2, sized_sentinel_for OutS2, class Proj = identity, indirect_unary_predicate<projected<I, Proj>> Pred> requires indirectly_copyable<I, O1> && indirectly_copyable<I, O2> ranges::partition_copy_result<I, O1, O2> ranges::partition_copy(Ep&& exec, I first, S last, O1 out_true, OutS1 last_true, O2 out_false, OutS2 last_false, Pred pred, Proj proj = {}); template<execution-policy Ep, sized-random-access-range R, sized-random-access-range OutR1, sized-random-access-range OutR2, class Proj = identity, indirect_unary_predicate<projected<iterator_t, Proj>> Pred> requires indirectly_copyable<iterator_t, iterator_t> && indirectly_copyable<iterator_t, iterator_t> ranges::partition_copy_result<borrowed_iterator_t, borrowed_iterator_t, borrowed_iterator_t> ranges::partition_copy(Ep&& exec, R&& r, OutR1&& out_true_r, OutR2&& out_false_r, Pred pred, Proj proj = {}); `

14

#

Let proj be identity{} for the overloads with no parameter named proj and let E(x) be bool(invoke(pred, invoke(proj, x))).

15

#

For the overloads with no parameterslast_true, last_false, out_true_r, or out_false_r, let

M be the number of iterators i in [first, last) for which E(*i) is true, and K be last - first - M;

last_true be out_true + M, and last_false be out_false + K.

16

#

For the overloads with parameterslast_true, last_false, out_true_r, or out_false_r, let M be last_true - out_true and K be last_false - out_false.

17

#

Let:

i1 be the iterator in [first, last) for which E(*i1) is true and there are exactly M iterators j in [first, i1) for which E(*j) is true, or last if no such iterator exists;

i2 be the iterator in [first, last) for which E(*i2) is false and there are exactly K iterators j in [first, i2) for which E(*j) is false, or last if no such iterator exists;

N be min(i1 - first, i2 - first).

18

#

Mandates: For the overloads in namespace std, the expression *first is writable ([iterator.requirements.general]) to out_true and out_false.

19

#

Preconditions: The input range and output ranges do not overlap.

[Note 1:

For the parallel algorithm overload in namespace std, there can be a performance cost if first's value type does not meet the Cpp17CopyConstructible requirements.

For the parallel algorithm overloads in namespace ranges, there can be a performance cost if first's value type does not model copy_constructible.

— end note]

20

#

Effects: For each iterator i in [first, first + N), copies *i to the output range [out_true, last_true) if E(*i) is true, or to the output range [out_false, last_false) otherwise.

21

#

Returns: Let o1 be the iterator past the last copied element in the output range [out_true, last_true), and o2 be the iterator past the last copied element in the output range [out_false, last_false).

Returns:

  • (21.1)

    {o1, o2} for the overloads in namespace std.

  • (21.2)

    {first + N, o1, o2} for the overloads in namespace ranges.

22

#

Complexity: At most last - first applications of pred and proj.

🔗

`template<class ForwardIterator, class Predicate> constexpr ForwardIterator partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);

template<forward_iterator I, sentinel_for S, class Proj = identity, indirect_unary_predicate<projected<I, Proj>> Pred> constexpr I ranges::partition_point(I first, S last, Pred pred, Proj proj = {}); template<forward_range R, class Proj = identity, indirect_unary_predicate<projected<iterator_t, Proj>> Pred> constexpr borrowed_iterator_t ranges::partition_point(R&& r, Pred pred, Proj proj = {}); `

23

#

Let proj be identity{} for the overloads with no parameter named proj and let E(x) be bool(invoke(pred, invoke(proj, x))).

24

#

Preconditions: The elements e of [first, last) are partitioned with respect to E(e).

25

#

Returns: An iterator mid such that E(*i) is true for all iterators i in [first, mid), andfalse for all iterators i in [mid, last).

26

#

Complexity: O(log(last - first)) applications of pred and proj.