ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> boost
boost
[boost] Re: lexicographic: review request?
by Daryle Walker other posts by this author
Aug 24 2003 5:09AM messages near this date
Re: [boost] Re: Problem on the CVS version of the top web page | [boost] Re: lexicographic: review request?
On Saturday, August 23, 2003, at 11:21 AM, Daniel Frey wrote:

>  Jan Langer 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
>  b) Unnamed functions
> 
> > bool operator < (person const &p1, person const &p2)
> > {
> >     return boost::lexicographic
> >         (p1.lastname, p2.lastname, cmp_lower)
> >         (p1.firstname, p2.firstname, cmp_lower)
> >         (p2.age, p1.age);
> > }
> 
>  The two points listed above are both visible here: Even if there are 
>  no persons with the same name, age is accessed. Even worse, it may be 
>  a costly method age() and the unaware user creates much worse code 
>  than using nested if-else-structures.

Short-circuiting can only be done via compiler magic with the built-in 
operator&& and operator||, so there's no way it can be added to 
user-defined routines.  Note that each step of the lexicographic 
comparison has a result, and that later steps are only evaluated if the 
previous step didn't give a result of equivalence (well, the comparison 
is skipped; each argument still has to be evaluated).  You can also 
manually check each intermediate result and code out other checks (with 
"if" statements), which would skip unnecessary evaluations of later 
components.

>  Also, you might not want to define operator<. Sometimes, you just need 
>  it in one place, e.g. std::sort( v.begin(), v.end(), /*HERE*/ );

Maybe Boost.Lamda or Boost.MPL can be used?

>  I don't know how to solve these problems, though. The only idea I have 
>  removes the comparator and uses macros. It does provide 
>  short-circuiting, but I fear I might get stoned now. Anyway:
> 
>  #define false(a,b) (!((a)==(b)))?((a)<(b)):!true
>  #define true(a,b) ((a)==(b))?((a)<(b)):false
>  #define BOOST_LEXICOGRAPHIC false

I don't think you can (re#)define a keyword.

>  bool operator<( const person& p1, const person& p2 )
>  {
>      return BOOST_LEXICOGRAPHIC
>         ( p1.lastname, p2.lastname )
>         ( p1.firstname, p2.firstname )
>         ( p1.age, p2.age );
>  }

Daryle

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Thread:
Daryle Walker
Daniel Frey
Brian McNamara
Jan Langer
Jan Langer
Daniel Frey

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved