Set class for D

Benji Smith dlanguage at benjismith.net
Thu Apr 26 11:57:19 PDT 2007


Bill Baxter wrote:
> Bill Baxter wrote:
>> There was some discussion a while back about a Set class.
>> I think Tango has one, but unfortunately I'm not on Tango yet.
>> Anyone have a link for a Phobos-compatible set class?
>> I checked dsource and scrapple, and tried searching the NG, but "set" 
>> gets a lot of bogus hits :-(.
> 
> BTW.  -- the thin wrapper around an AA (a.k.a. bool[T]) implementation 
> is ok with me, though one without the bool baggage would naturally be 
> better.
> 
> --bb

This is one of those areas where the awkwardness of built-in AA's (as 
opposed to a library implementation) becomes evident. Ideally, calling 
the .keys property of an AA should return a Set, and calling .values 
should return a Bag.

The fact that D essentially has Map and List as built-in types means 
that any collection API will have a lot of weird boundaries and edge-cases.

Inevitably, people will want Map implementations that preserve 
key-ordering and/or value-ordering, using custom comparators. At that 
point, it'll be painful to draw the line between AA's and other Map 
implementations.

The same thing is true for dynamic arrays and other sequentially-ordered 
data structures (queues, linked lists, etc).

Anyhow, to get back to the OP's point, I think the reason why there's no 
good implementation of Set is that D makes it difficult to design a good 
collections API by putting some things into the language (and making 
their implementations opaque) that should have been in libraries.

For example, I have a pretty mature Map, Set, and Bag implementation 
that I wrote in Java and ported to C# for a few other projects. I'd be 
happy to port it to D as well, but I'd want to publish a well-designed 
API for all collections, and it's not obvious to me how that should be 
done in D.

--benji



More information about the Digitalmars-d mailing list