Boost logo

Boost :

From: Jan Langer (jan_at_[hidden])
Date: 2003-08-24 11:35:11


Daniel Frey wrote:
>> what is needed for the lexicographic class to be included into boost?
>
> I don't know, but I think there are several ideas which haven't been
> addressed and that we should at least have a look at:
>
> a) Short-circuiting

i'm currently thinking about this issue. the only case where this will
be relevant is, when the argument is a complicated one. the simple
p1.firstname case will have IMHO no overhead if it is properly inlined.
but in case of an expensive argument its will be evaluated.

it is also a problem for every function which takes a complicated
argument which will probaly not be needed.

unfortunately i cannot see a possiblity to prevent it. except in user
code where one can write the following (as daryle walker already mentioned):

   int a = 17;
   int b = 3;
   boost::lexicographic l (a, b);
   if (l.result () == boost::lexicographic::equivalent)
   {
     l (a + b, a - b);
     if (l.result () == boost::lexicographic::equivalent)
       l (a * b, a / b);
   }

which is still nicer than the according conventional approach:

   bool result = false;
   if (a == b)
   {
     int a_plus_b = a + b;
     int a_minus_b = a - b;
     if (a_plus_b == a_minus_b)
       result = (a * b) < (a / b);
     else
       result = a_plus_b < a_minus_b;
   }
   else
     result = a < b;

> b) Unnamed functions

i think this would solve not the same problems as my class. my main
interest when writing this class was simplicity in user code for
comparing relativly arbitrary arguments. and as seen in brians example
is gets more complicated. the use of this utility should lay in case
when one needs a functor. then he can implement it with the help of my
class.

some of the examples i needed in a real code would be quite difficult to
write with an lambda expression.

struct packet
{
        int timeout;
        int length;
        int remaining_length;
};

bool running (packet const &p);

bool operator < (packet const &p1, packet const &p2)
{
        bool p1_rem_time = (p1.timeout - p1.remaining_length);
        bool p2_rem_time = (p2.timeout - p2.remaining_length);

        return boost::lexicographic
                (running (p2), running (p1))
                (p1_rem_time, p2.rem_time)
                (p1.length, p2.length);
}

and the nice thing was that i could easily change the order (i had to
try different sortings) or add or remove certain criteria by commenting
out that line.

jan

-- 
jan langer ... jan_at_[hidden]
"pi ist genau drei"

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