Boost logo

Boost :

From: Gennaro Prota (gennaro_prota_at_[hidden])
Date: 2002-11-14 14:42:01


On 14 Nov 2002 12:40:38 +0000, Anthony Williams
<anthony.williamsNOSPAM_at_[hidden]> wrote:

>I have uploaded lexical_compare.hpp to the yahoo files area. It contains
>implementations of the function template lexicalCompare.

Just a little comment and a couple of questions. The former is
actually just a matter of personal preference, so don't feel qualms
about ignoring it (well, not that the same advice isn't valid for the
questions too :-)

I would write the body as:

    for (...) {

      if(*first1 < *first2)
        return -1;

      else if(*first2 < *first1)
        return +1;
    }

    if(first2 != last2)
        return -1;

    else if(first1 != last1)
        return +1;

    else
        return 0;

The difference is in the order of the out-of-loop tests. Since we test
what is *not* exhausted (or empty since the beginning) I prefer to
check the second sequence first, so that the order of the tests that
give -1 is the same inside and out of the loop. In other words I
prefer to see first whether the first sequence is lexicographically
less. BTW once you rewrite it as above you realize that you can
further simplify it as:

    for (...) {
      if(*first1 < *first2)
        return -1;

      else if(*first2 < *first1)
        return +1;
    }
    
    if(first2 != last2) // (*)
      return -1;

    return first1 != last1;

The questions are: a) as you know the SGI STL has such a template for
long time (lexicographical_compare_3way). Did you deliberately choose
a different name? Why? b) Did you really have problems without the
casts?

Genny.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk