std.array suggestion

Oskar Linde oskar.lindeREM at OVEgmail.com
Fri Mar 10 05:56:07 PST 2006


Stewart Gordon wrote:
> Oskar Linde wrote:
>> It is also in many cases simpler to define a ordering predicate than a 
>> 3-valued ordering. The name wrongOrder just helps as a reminder of 
>> what the predicate should return.
> <snip>
> 
> If that's so, I wonder why so many things have stuck with the 
> three-valued ordering.

A comparison operator is not the same thing as an ordering predicate.
Consider sorting phone-book entries (Sorted by first city, then surname 
and finally first name):

bool phoneBookOrder(Record a, Record b) {
	return a.city > b.city ||
                a.surname > b.surname ||
                a.name > b.name;
}

Forcing the user to write three-valued ordering functions for this is 
both unnecessary (the sorthing function will not use all 3 values), 
harder to get right (try it yourself) and probably less efficient.

Instead of wrongOrder, I could name the predicate lessThan and swap its 
arguments. This is what C++ STL uses and may be more logical.

/Oskar



More information about the Digitalmars-d mailing list