A set type implemented as an AA wrapper

Ferhat Kurtulmuş aferust at gmail.com
Thu Mar 12 10:36:33 UTC 2020


On Thursday, 12 March 2020 at 08:51:24 UTC, mark wrote:
> I use sets a lot and since I believe that D's rbtree is O(lg n) 
> for add/remove/in and that D's AA is O(1) for these, I want to 
> implement a set in terms of an AA.

> XXX: I need to use an if on the struct to restrict T to be a 
> type that supports toHash and opEquals (i.e., to be a valid AA 
> key)
>

Maybe there is another way for this. But I would consider using:
std.traits.hasMember

> YYY: The range() method is clearly not good D style but I don't
opApply can be used for it.

> ZZZ: I can't figure out how to support the in operator.

V* opBinaryRight(string op) with in should return a pointer for 
valid entries and null for non-existing entries. Thus, "if(key in 
AA){...}" works as expected. That is how AA implementation of 
druntime works.

Recently I ve ported it for -betterC. You can take a look at:
https://github.com/aferust/bcaa


More information about the Digitalmars-d-learn mailing list