Red-Black tree with customized sorting

Ali Çehreli acehreli at yahoo.com
Sun May 12 14:48:16 PDT 2013


On 05/12/2013 02:09 PM, Paolo Bolzoni wrote:

 > 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 think this is an issue with the template constraint of RedBlackTree. 
It passes the alias template parameter through binaryFun and I think 
that fails the check:

final class RedBlackTree(T, alias less = "a < b", bool allowDuplicates = 
false)
     if(is(typeof(binaryFun!less(T.init, T.init))))

A workaround:

     auto t = new RedBlackTree!(CC, (a, b) => less(a, b));

Ali



More information about the Digitalmars-d-learn mailing list