mirror of
https://github.com/dlang/phobos.git
synced 2025-05-02 08:00:48 +03:00
[Issue 16170] Seperate std.algorithm.sorting.partition into various overloads in order to facilitate further improvements
This commit is contained in:
parent
e15e7c08a5
commit
e21f2729ba
1 changed files with 28 additions and 20 deletions
|
@ -385,16 +385,14 @@ See_Also:
|
|||
STL's $(HTTP sgi.com/tech/stl/_partition.html, _partition)$(BR)
|
||||
STL's $(HTTP sgi.com/tech/stl/stable_partition.html, stable_partition)
|
||||
*/
|
||||
Range partition(alias predicate,
|
||||
SwapStrategy ss = SwapStrategy.unstable, Range)(Range r)
|
||||
if ((ss == SwapStrategy.stable && isRandomAccessRange!(Range) && hasLength!Range && hasSlicing!Range)
|
||||
|| (ss != SwapStrategy.stable && isForwardRange!(Range)))
|
||||
Range partition(alias predicate, SwapStrategy ss, Range)(Range r)
|
||||
if (ss == SwapStrategy.stable && isRandomAccessRange!(Range) && hasLength!Range && hasSlicing!Range)
|
||||
{
|
||||
import std.algorithm.mutation : bringToFront;
|
||||
|
||||
alias pred = unaryFun!(predicate);
|
||||
if (r.empty) return r;
|
||||
static if (ss == SwapStrategy.stable)
|
||||
{
|
||||
import std.algorithm.mutation : bringToFront;
|
||||
|
||||
if (r.length == 1)
|
||||
{
|
||||
if (pred(r.front)) r.popFront();
|
||||
|
@ -406,10 +404,19 @@ Range partition(alias predicate,
|
|||
auto upper = recurse(r[middle .. r.length]);
|
||||
bringToFront(lower, r[middle .. r.length - upper.length]);
|
||||
return r[r.length - lower.length - upper.length .. r.length];
|
||||
}
|
||||
else static if (ss == SwapStrategy.semistable)
|
||||
{
|
||||
}
|
||||
|
||||
///ditto
|
||||
Range partition(alias predicate, SwapStrategy ss = SwapStrategy.unstable, Range)(Range r)
|
||||
if (ss != SwapStrategy.stable && isInputRange!Range && hasSwappableElements!Range)
|
||||
{
|
||||
import std.algorithm.mutation : swap;
|
||||
alias pred = unaryFun!(predicate);
|
||||
|
||||
static if (ss == SwapStrategy.semistable)
|
||||
{
|
||||
if (r.empty) return r;
|
||||
|
||||
for (; !r.empty; r.popFront())
|
||||
{
|
||||
// skip the initial portion of "correct" elements
|
||||
|
@ -424,9 +431,10 @@ Range partition(alias predicate,
|
|||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
else // ss == SwapStrategy.unstable
|
||||
else
|
||||
{
|
||||
// Inspired from www.stepanovpapers.com/PAM3-partition_notes.pdf,
|
||||
// section "Bidirectional Partition Algorithm (Hoare)"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue