Skip to content

Commit e21f272

Browse files
committed
[Issue 16170] Seperate std.algorithm.sorting.partition into various overloads in order to facilitate further improvements
1 parent e15e7c0 commit e21f272

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

std/algorithm/sorting.d

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -385,31 +385,38 @@ See_Also:
385385
STL's $(HTTP sgi.com/tech/stl/_partition.html, _partition)$(BR)
386386
STL's $(HTTP sgi.com/tech/stl/stable_partition.html, stable_partition)
387387
*/
388-
Range partition(alias predicate,
389-
SwapStrategy ss = SwapStrategy.unstable, Range)(Range r)
390-
if ((ss == SwapStrategy.stable && isRandomAccessRange!(Range) && hasLength!Range && hasSlicing!Range)
391-
|| (ss != SwapStrategy.stable && isForwardRange!(Range)))
388+
Range partition(alias predicate, SwapStrategy ss, Range)(Range r)
389+
if (ss == SwapStrategy.stable && isRandomAccessRange!(Range) && hasLength!Range && hasSlicing!Range)
392390
{
391+
import std.algorithm.mutation : bringToFront;
392+
393393
alias pred = unaryFun!(predicate);
394394
if (r.empty) return r;
395-
static if (ss == SwapStrategy.stable)
395+
396+
if (r.length == 1)
396397
{
397-
import std.algorithm.mutation : bringToFront;
398-
if (r.length == 1)
399-
{
400-
if (pred(r.front)) r.popFront();
401-
return r;
402-
}
403-
const middle = r.length / 2;
404-
alias recurse = .partition!(pred, ss, Range);
405-
auto lower = recurse(r[0 .. middle]);
406-
auto upper = recurse(r[middle .. r.length]);
407-
bringToFront(lower, r[middle .. r.length - upper.length]);
408-
return r[r.length - lower.length - upper.length .. r.length];
398+
if (pred(r.front)) r.popFront();
399+
return r;
409400
}
410-
else static if (ss == SwapStrategy.semistable)
401+
const middle = r.length / 2;
402+
alias recurse = .partition!(pred, ss, Range);
403+
auto lower = recurse(r[0 .. middle]);
404+
auto upper = recurse(r[middle .. r.length]);
405+
bringToFront(lower, r[middle .. r.length - upper.length]);
406+
return r[r.length - lower.length - upper.length .. r.length];
407+
}
408+
409+
///ditto
410+
Range partition(alias predicate, SwapStrategy ss = SwapStrategy.unstable, Range)(Range r)
411+
if (ss != SwapStrategy.stable && isInputRange!Range && hasSwappableElements!Range)
412+
{
413+
import std.algorithm.mutation : swap;
414+
alias pred = unaryFun!(predicate);
415+
416+
static if (ss == SwapStrategy.semistable)
411417
{
412-
import std.algorithm.mutation : swap;
418+
if (r.empty) return r;
419+
413420
for (; !r.empty; r.popFront())
414421
{
415422
// skip the initial portion of "correct" elements
@@ -424,9 +431,10 @@ Range partition(alias predicate,
424431
}
425432
return result;
426433
}
434+
427435
return r;
428436
}
429-
else // ss == SwapStrategy.unstable
437+
else
430438
{
431439
// Inspired from www.stepanovpapers.com/PAM3-partition_notes.pdf,
432440
// section "Bidirectional Partition Algorithm (Hoare)"

0 commit comments

Comments
 (0)