Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 10777 - multiSort returns SortedRange #4061

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
103 changes: 81 additions & 22 deletions std/algorithm/sorting.d
Expand Up @@ -774,8 +774,54 @@ private template validPredicates(E, less...)
&& validPredicates!(E, less[1 .. $]);
}

struct MultiPred( preds... )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually want this to be accessible? What would be other use cases for MultiPred?
I'd favor MultiSortPred btw, to emphasize how the multiple predicates are used, MultiPred should like an && combinator.

if( preds.length >= 1 )
{
private import std.functional : binaryFun;

static bool opCall( A, B )( A a, B b ) {
foreach( pred; preds[ 0 .. ( $ - 1 ) ] ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for parens around $ - 1, spacing is a bit "weird" as well.
I'd highly recommend dfmt and stop caring about syntax.

alias p = binaryFun!pred;
if( p( a, b ) ) return true;
if( p( b, a ) ) return false;
}
return binaryFun!( preds[ $ - 1 ] )( a, b );
}

template shrink( uint n )
if( preds.length > 1 && n >= 1 && n <= preds.length ) {
alias shrink = MultiPred!( preds[ 0 .. n ] );
}
}

@safe unittest {
struct Record {
string name;
int age;
}

alias mp = MultiPred!( ( a, b ) => a.name < b.name, "a.age < b.age" );

auto ra = Record( "Ewa", 10 );
auto rb = Record( "Ewa", 20 );

assert( !mp( ra, ra ) );
assert( mp( ra, rb ) );
assert( !mp( rb, ra ) );

assert( !mp.shrink!1( ra, ra ) );
assert( !mp.shrink!1( ra, rb ) );
assert( !mp.shrink!1( rb, ra ) );
}

@safe unittest {
auto es = [ 1, 10, 15, 22 ];
assert( es.isSorted!( MultiPred!( ( float a, float b ) => a < b ) ) );
assert( es.isSorted!( MultiPred!( "a < b" ) ) );
}

/**
$(D void multiSort(Range)(Range r)
$(D auto multiSort(Range)(Range r)
if (validPredicates!(ElementType!Range, less));)

Sorts a range by multiple keys. The call $(D multiSort!("a.id < b.id",
Expand All @@ -784,10 +830,13 @@ and sorts elements that have the same $(D id) by $(D date)
descending. Such a call is equivalent to $(D sort!"a.id != b.id ? a.id
< b.id : a.date > b.date"(r)), but $(D multiSort) is faster because it
does fewer comparisons (in addition to being more convenient).

Returns: The initial range wrapped as a $(D SortedRange) with the predicate
$(D MultiPred!(less) ).
*/
template multiSort(less...) //if (less.length > 1)
{
void multiSort(Range)(Range r)
auto multiSort(Range)(Range r)
if (validPredicates!(ElementType!Range, less))
{
static if (is(typeof(less[$ - 1]) == SwapStrategy))
Expand All @@ -800,32 +849,39 @@ template multiSort(less...) //if (less.length > 1)
alias ss = SwapStrategy.unstable;
alias funs = less;
}
alias lessFun = binaryFun!(funs[0]);

static if (funs.length > 1)
static void doMultiSort( funs... )( Range r )
{
while (r.length > 1)
alias fun = binaryFun!(funs[0]);

static if (funs.length > 1)
{
auto p = getPivot!lessFun(r);
auto t = partition3!(less[0], ss)(r, r[p]);
if (t[0].length <= t[2].length)
{
.multiSort!less(t[0]);
.multiSort!(less[1 .. $])(t[1]);
r = t[2];
}
else
while (r.length > 1)
{
.multiSort!(less[1 .. $])(t[1]);
.multiSort!less(t[2]);
r = t[0];
auto p = getPivot!fun(r);
auto t = partition3!(funs[0], ss)(r, r[p]);
if (t[0].length <= t[2].length)
{
doMultiSort!funs(t[0]);
doMultiSort!(funs[1 .. $])(t[1]);
r = t[2];
}
else
{
doMultiSort!(funs[1 .. $])(t[1]);
doMultiSort!funs(t[2]);
r = t[0];
}
}
}
else
{
sort!(fun, ss)(r);
}
}
else
{
sort!(lessFun, ss)(r);
}

doMultiSort!funs( r );
return assumeSorted!( MultiPred!funs )( r );
}
}

Expand All @@ -835,8 +891,11 @@ template multiSort(less...) //if (less.length > 1)
static struct Point { int x, y; }
auto pts1 = [ Point(0, 0), Point(5, 5), Point(0, 1), Point(0, 2) ];
auto pts2 = [ Point(0, 0), Point(0, 1), Point(0, 2), Point(5, 5) ];
multiSort!("a.x < b.x", "a.y < b.y", SwapStrategy.unstable)(pts1);
auto sr = multiSort!("a.x < b.x", "a.y < b.y", SwapStrategy.unstable)(pts1);
assert(pts1 == pts2);
assert( sr.contains( Point(0, 2) ) );
assert( !sr.contains( Point(0, 5) ) );
assert( sr.shrinkPred!1.contains( Point(0, 5) ) );
}

@safe unittest
Expand Down
7 changes: 7 additions & 0 deletions std/range/package.d
Expand Up @@ -7980,6 +7980,13 @@ sorting relation.
import std.algorithm.iteration : chunkBy;
return _input.chunkBy!((a, b) => !predFun(a, b) && !predFun(b, a));
}

import std.algorithm.sorting: MultiPred;

static if( is( pred == MultiPred!P, P... ) )
auto shrinkPred( uint n )() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think it would be nicer to have this as a freestanding UFCS function. Then you can also put it next to multiSort in std/algorithm/sorting.d.
You should be able to match shrinkPred(less...)(SortedRange!(MultiPred!less) sr).

return assumeSorted!( pred.shrink!n )( this );
}
}

///
Expand Down