Red-Black tree with customized sorting

Paolo Bolzoni paolo.bolzoni at g.invalid
Sun May 12 14:09:56 PDT 2013


I need to user Red-Black trees with non-default sorting.
Here is a minimal example
---- >8
import std.container;
struct CC {
     int a;
     int b; }

bool less(const ref CC lhs, const ref CC rhs) {
     if (lhs.a != rhs.a)
         return lhs.a < rhs.a;
     else
         return lhs.b < rhs.b; }

void main() {
     auto t = new RedBlackTree!(CC, "a.a != b.a ? a.a < b.a : a.b 
< b.b"); }
8< ----

It works, but I would like to pass the function "less" as 
comparison operator since in my real problem the comparison is a 
more complex.

I tried this ways:
     auto t = new RedBlackTree!(CC, less);
     auto t = new RedBlackTree!(CC, CC);
     auto t = new RedBlackTree!(CC, &less);
     auto t = new RedBlackTree!(CC, "less(a,b)");
     auto t = new RedBlackTree!(CC, "what the hell!? just call 
less! Is it too much to ask?");

None of them works, I am stuck. The documentation states:
(quote from: 
http://dlang.org/phobos/std_container.html#.RedBlackTree )
"""
To use a different comparison than "a < b", pass a different 
operator string that can be used by std.functional.binaryFun, or 
pass in a function, delegate, functor, or any type where less(a, 
b) results in a bool value.
"""

What is wrong? How I am supposed to pass less address to do the 
comparison?

Thanks


More information about the Digitalmars-d-learn mailing list