Equivilent of STL Set in D ? ...

clayasaurus clayasaurus at gmail.com
Mon Oct 23 14:37:22 PDT 2006


KlausO wrote:
> KlausO wrote:
>>
>> Seems to be an interesting task to get used to the new foreach
>> features. I'll give it a try.
>>
>> Klaus
> 
> Ok, I rewrote the intrusive version to a nonintrusive one.
> It has almost the interface you mentioned.
> 
> Please note: IMHO, there are some points where the template
> needs some refinement.
> 
> 1. A third template parameter "bool duplicatesAllowed" should be
>    added and handled in insert/remove via "static if". Then you
>    have multisets at hand !
> 
> 2. Sometimes it exposes an interface to the internally used nodes
>    to be able to access the key/value pairs. A special iterator
>    class which could encapsulate the "not found/not inserted"
>    state would be better.
> 
> 3. The insert via opIndexAssign/find via opIndex is IMHO not
>    optimal because you could not return the "not found/not inserted"
>    state. There are some possibilities to resolve this:
> 
>     a) Throw an exception (IMHO, ugly)

This is a lot better than returning an empty node, though. If you can 
tell me why it crashed, then do it :)

> 
>    Using insert like
>      if (foo.insert("Jill", 13))
>    looks better to me.

You could implement both and let the programmer decide.

It also might be neat to implement opIn_r, so you can use...

if (!("Jill" in tree))
    tree.add("Jill");



More information about the Digitalmars-d-learn mailing list