RBTree delegates and types

Viktor viktor.ivanov at gmail.com
Sun Mar 18 12:34:38 UTC 2018


Hey,

I'm trying to convert an old legacy app to D and have a couple of 
questions. It has been a very fun weekend!

First, I could not make std.container.rbtree use a delegate for a 
comparator. The docs say it should be possible, but I got a weird 
error.

I tracked it down to RedBlackTreee.opEquals() using explicit 
function when calling equals():

return equal!(function(Elem a, Elem b) => !_less(a,b) && 
!_less(b,a))(thisRange, thatRange);

When I removed the "function" things started compiling (I have 
yet to test the code due to question #2 below). I've done a dub 
-b unittest without issues so it might be OK.

So, the first...several questions are: do you think this change 
is safe and should I raise an issue in the bugzilla or do a PR 
for it on github? Should it include a new unittest that makes 
sure rbtree can be instantiated with a delegate?

The other part I'm still struggling with is about auto types. Can 
I store the rbtree type in the following class so it can be used 
from another method?

class Indexer(T, alias pred)
{
    this(T storage)
    {
       this.storage = storage;
       auto index = new RedBlackTree!(uint, (a,b) => comp(a, b));
       // how to store index?
    }

    void dumpIndex()
    {
        // how to get my dirty hands on the index here?
    }

    bool comp(uint i1, uint i2)
    {
       auto rec1 = storage[i1];
       auto rec2 = storage[i2];
       return pred(rec1, rec2);
    }

    private T storage;
    ??? index;
}



More information about the Digitalmars-d-learn mailing list