mirror of
https://github.com/dlang/phobos.git
synced 2025-05-01 23:50:31 +03:00
Issue 14294 - partialSort should also accept two ranges
This commit is contained in:
parent
19445fc71e
commit
0d0ca991a9
1 changed files with 31 additions and 3 deletions
|
@ -2889,18 +2889,46 @@ void partialSort(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable,
|
||||||
Range)(Range r, size_t n)
|
Range)(Range r, size_t n)
|
||||||
if (isRandomAccessRange!(Range) && hasLength!(Range) && hasSlicing!(Range))
|
if (isRandomAccessRange!(Range) && hasLength!(Range) && hasSlicing!(Range))
|
||||||
{
|
{
|
||||||
topN!(less, ss)(r, n);
|
partialSort!(less, ss)(r[0 .. n], r[n .. $]);
|
||||||
sort!(less, ss)(r[0 .. n]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
@safe unittest
|
unittest
|
||||||
{
|
{
|
||||||
int[] a = [ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ];
|
int[] a = [ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ];
|
||||||
partialSort(a, 5);
|
partialSort(a, 5);
|
||||||
assert(a[0 .. 5] == [ 0, 1, 2, 3, 4 ]);
|
assert(a[0 .. 5] == [ 0, 1, 2, 3, 4 ]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Stores the smallest elements of the two ranges in the left-hand range in sorted order.
|
||||||
|
|
||||||
|
Params:
|
||||||
|
less = The predicate to sort by.
|
||||||
|
ss = The swapping strategy to use.
|
||||||
|
r1 = The first range.
|
||||||
|
r2 = The second range.
|
||||||
|
*/
|
||||||
|
|
||||||
|
void partialSort(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable,
|
||||||
|
Range1, Range2)(Range1 r1, Range2 r2)
|
||||||
|
if (isRandomAccessRange!(Range1) && hasLength!Range1 &&
|
||||||
|
isInputRange!Range2 && is(ElementType!Range1 == ElementType!Range2) &&
|
||||||
|
hasLvalueElements!Range1 && hasLvalueElements!Range2)
|
||||||
|
{
|
||||||
|
topN!(less, ss)(r1, r2);
|
||||||
|
sort!(less, ss)(r1);
|
||||||
|
}
|
||||||
|
///
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
int[] a = [5, 7, 2, 6, 7];
|
||||||
|
int[] b = [2, 1, 5, 6, 7, 3, 0];
|
||||||
|
|
||||||
|
partialSort(a, b);
|
||||||
|
assert(a == [0, 1, 2, 2, 3]);
|
||||||
|
}
|
||||||
|
|
||||||
// topN
|
// topN
|
||||||
/**
|
/**
|
||||||
Reorders the range $(D r) using $(D swap) such that $(D r[nth]) refers
|
Reorders the range $(D r) using $(D swap) such that $(D r[nth]) refers
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue