std.array suggestion

Kevin Bealer Kevin_member at pathlink.com
Fri Mar 10 14:51:09 PST 2006


In article <dus0hn$2tb0$1 at digitaldaemon.com>, Oskar Linde says...
>
>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

This contains a bug.  It should be like this:

>bool phoneBookOrder(Record a, Record b) {
>    if (a.city < b.city) return false;
>    if (a.city > b.city) return true;
>
>    if (a.surname < b.surname) return false;
>    if (a.surname > b.surname) return true;
>
>    return a.name > b.name;
>}

.. Otherwise, (a < b) && (b < a) is possible, i.e. when the cities are equal.

Kevin





More information about the Digitalmars-d mailing list