Re: [boost] compare

2003-03-27 Thread David Abrahams
[EMAIL PROTECTED] writes:

 Hey Jan -

 First off, let me say: excellent idea!  Several times I've wanted just such
 a class, and I would be happy to see this added to Boost.

 I have a few suggestions/things to consider:
   1) Instead of operator bool, use the unspecified-bool-type discussed
 elsewhere on this list; it's safer.

Yup.

   2) I'm not sure that the choice of the name is ideal.  OTOH, I can't think
 of a better one...

lexicographic?

   3) I'd like to see a general solution for this problem using real
 (late-bound) function objects as well, if you know what I mean.  This would
 be a lot harder, though, so maybe it should be put on a wish-list.  Have you
 given any thought to this approach?

return compare.less(p1.x, p2.x)
  .greater(p1.y, p2.y)
  .call(f, p1.z, p2.z);

??

Just one dumb idea for a comparison DSL...
- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

___
Unsubscribe  other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


RE: [boost] compare

2003-03-27 Thread scleary
 -Original Message-
 From: David Abrahams [mailto:[EMAIL PROTECTED]
 
 [EMAIL PROTECTED] writes:
 
3) I'd like to see a general solution for this problem using real
  (late-bound) function objects as well, if you know what I mean.  This
would
  be a lot harder, though, so maybe it should be put on a wish-list.  Have
you
  given any thought to this approach?
 
   return compare.less(p1.x, p2.x)
   .greater(p1.y, p2.y)
   .call(f, p1.z, p2.z);
 
 ??

Sorry, I wasn't very clear.  I mean defining a compare_t that could be used
something like:

compare_tposition::x, position::y, position::z

that would result in a function object type that produced the same behaviour
when given two arguments of type 'position'.  (Usable, e.g., as a template
argument to std::set).  I don't see any way of supporting that simple of a
syntax, though; the nicest I can see is:

compare_typeposition, compare_tdouble, position::x,
compare_tdouble, position::y, compare_tdouble, position::z 

which is pretty hideous. :(

-Steve
___
Unsubscribe  other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


RE: [boost] compare

2003-03-27 Thread scleary
 -Original Message-
 From: Jan Langer [mailto:[EMAIL PROTECTED]

Just one other thing - instead of:
  enum result { minus, zero, plus };
I would do:
  enum result { minus = -1, zero, plus };
just for the sake of code readability and ease of understanding.  It doesn't
change the logic at all.

-Steve
___
Unsubscribe  other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


[boost] compare

2003-03-26 Thread Jan Langer
hi,
for several times i used a small utility class, so i thought it might be 
interesting for boost too.
it takes pairs of arguments and compares them pairwise beginning with 
the first one.
an example usage would be a special sorting operators:

struct position
{
double x, y, z;
};
bool operator  (position const p1, position const p2)
{
return compare (p1.x, p2.x)
   (p1.y, p2.y)
   (p1.z, p2.z);
}
is equal to

bool operator  (position const p1, position const p2)
{
if (p1.x == p2.x)
if (p1.y == p2.y)
return p1.z  p2.z;
else
return p1.y  p2.y;
else
return p1.x  p2.x;
}
or

struct person
{
std::string firstname, lastname;
};
bool operator  (person const p1, person const p2)
{
return compare
(p1.lastname, p2.lastname, cmp_case_insensitive)
(p1.firstname, p2.firstname, cmp_case_insensitive);
}
where cmp_case_insensitive is a comparision function which is used 
instead of operator  to compare the arguments.

the class itself is quite short:

class compare
{
enum result { minus, zero, plus };
  public:

compare () : v_ (zero) {}

template typename T
compare (T const a, T const b)
: v_ (compare () (a, b).v_)
{}
template typename T, typename Cmp
compare (T const a, T const b, Cmp cmp)
: v_ (compare () (a, b, cmp).v_)
{}
template typename T
compare operator () (T const a, T const b)
{
if (v_ == zero)
{
if (a  b)
v_ = plus;
else if (b  a)
v_ = minus;
else
v_ = zero;
}
return *this;
}
template typename T, typename Cmp
compare operator () (T const a, T const b, Cmp cmp)
{
if (v_ == zero)
{
if (cmp (a, b))
v_ = plus;
else if (cmp (b, a))
v_ = minus;
else
v_ = zero;
}
return *this;
}
operator bool ()
{
return v_ == plus;
}
  private:
result v_;
};
--
jan langer ... [EMAIL PROTECTED]
pi ist genau drei
___
Unsubscribe  other changes: http://lists.boost.org/mailman/listinfo.cgi/boost