RBTree delegates and types
Steven Schveighoffer
schveiguy at yahoo.com
Sun Mar 18 17:21:58 UTC 2018
On 3/18/18 8:34 AM, Viktor wrote:
> 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?
Yes, seems like an oversight. RedBlackTree.opEquals was added almost 6
years ago, and it's possible this wouldn't have worked back then
(https://github.com/dlang/phobos/pull/900)
> Should it include a new unittest that makes sure rbtree can be
> instantiated with a delegate?
Definitely. Thanks for thinking of this!
>
> 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)
> {
alias RBType = RedBlackTree!(uint, (a, b) => comp(a, b));
> this(T storage)
> {
> this.storage = storage;
this.index = new RBType();
> }
>
> void dumpIndex()
> {
> // how to get my dirty hands on the index here?
// answer: just use it?
> }
>
> bool comp(uint i1, uint i2)
> {
> auto rec1 = storage[i1];
> auto rec2 = storage[i2];
> return pred(rec1, rec2);
> }
>
> private T storage;
RBType index;
> }
>
-Steve
More information about the Digitalmars-d-learn
mailing list