mirror of
https://github.com/dlang/phobos.git
synced 2025-05-06 02:45:12 +03:00
std.algorithm.sorting: Enable binary transform function for schwartzSort
Receiving the element's index in the original range as 2nd parameter.
This commit is contained in:
parent
d15d568e30
commit
5497b666e4
2 changed files with 40 additions and 4 deletions
9
changelog/schwartzSort.dd
Normal file
9
changelog/schwartzSort.dd
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
`std.algorithm.sorting.schwartzSort` supports binary transform functions too
|
||||||
|
|
||||||
|
The `transform` template parameter of $(REF schwartzSort, std, algorithm, sorting)
|
||||||
|
isn't restricted to unary functions taking an element anymore, but can
|
||||||
|
also be a binary function taking an element and its index in the
|
||||||
|
original range.
|
||||||
|
|
||||||
|
The original index can therefore be used when computing the transformed
|
||||||
|
value to sort by.
|
|
@ -2941,8 +2941,10 @@ because the effect can be achieved by calling $(D
|
||||||
isSorted!less(map!transform(r))).
|
isSorted!less(map!transform(r))).
|
||||||
|
|
||||||
Params:
|
Params:
|
||||||
transform = The transformation to apply.
|
transform = The transformation to apply. Either a unary function
|
||||||
less = The predicate to sort by.
|
(`unaryFun!transform(element)`), or a binary function
|
||||||
|
(`binaryFun!transform(element, index)`).
|
||||||
|
less = The predicate to sort the transformed elements by.
|
||||||
ss = The swapping strategy to use.
|
ss = The swapping strategy to use.
|
||||||
r = The range to sort.
|
r = The range to sort.
|
||||||
|
|
||||||
|
@ -2960,7 +2962,21 @@ if (isRandomAccessRange!R && hasLength!R && hasSwappableElements!R &&
|
||||||
import std.range : zip, SortedRange;
|
import std.range : zip, SortedRange;
|
||||||
import std.string : representation;
|
import std.string : representation;
|
||||||
|
|
||||||
alias T = typeof(unaryFun!transform(r.front));
|
static if (is(typeof(unaryFun!transform(r.front))))
|
||||||
|
{
|
||||||
|
alias transformFun = unaryFun!transform;
|
||||||
|
alias T = typeof(transformFun(r.front));
|
||||||
|
enum isBinary = false;
|
||||||
|
}
|
||||||
|
else static if (is(typeof(binaryFun!transform(r.front, 0))))
|
||||||
|
{
|
||||||
|
alias transformFun = binaryFun!transform;
|
||||||
|
alias T = typeof(transformFun(r.front, 0));
|
||||||
|
enum isBinary = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
static assert(false, "unsupported `transform` alias");
|
||||||
|
|
||||||
static trustedMalloc(size_t len) @trusted
|
static trustedMalloc(size_t len) @trusted
|
||||||
{
|
{
|
||||||
import core.checkedint : mulu;
|
import core.checkedint : mulu;
|
||||||
|
@ -2988,7 +3004,10 @@ if (isRandomAccessRange!R && hasLength!R && hasSwappableElements!R &&
|
||||||
}
|
}
|
||||||
for (; length != r.length; ++length)
|
for (; length != r.length; ++length)
|
||||||
{
|
{
|
||||||
emplace(&xform1[length], unaryFun!transform(r[length]));
|
static if (isBinary)
|
||||||
|
emplace(&xform1[length], transformFun(r[length], length));
|
||||||
|
else
|
||||||
|
emplace(&xform1[length], transformFun(r[length]));
|
||||||
}
|
}
|
||||||
// Make sure we use ubyte[] and ushort[], not char[] and wchar[]
|
// Make sure we use ubyte[] and ushort[], not char[] and wchar[]
|
||||||
// for the intermediate array, lest zip gets confused.
|
// for the intermediate array, lest zip gets confused.
|
||||||
|
@ -3054,6 +3073,14 @@ if (isRandomAccessRange!R && hasLength!R && hasSwappableElements!R)
|
||||||
assert(isSorted!("a < b")(map!(entropy)(arr)));
|
assert(isSorted!("a < b")(map!(entropy)(arr)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@safe unittest
|
||||||
|
{
|
||||||
|
// binary transform function
|
||||||
|
string[] strings = [ "one", "two", "three" ];
|
||||||
|
schwartzSort!((element, index) => size_t.max - index)(strings);
|
||||||
|
assert(strings == [ "three", "two", "one" ]);
|
||||||
|
}
|
||||||
|
|
||||||
@safe unittest
|
@safe unittest
|
||||||
{
|
{
|
||||||
// issue 4909
|
// issue 4909
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue