Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

HashSet -> ??

Moderators: larsivi kris

Posted: 05/10/09 01:53:08

Looks like HashSet? is deprecated. What is the preferred alternative?

Author Message

Posted: 05/10/09 02:07:30 -- Modified: 05/10/09 02:08:04 by
torhu

Probably tango.util.container.HashSet? instead of tango.util.collection. HashSet?.

Posted: 05/10/09 10:55:55

Ahh, I see. That works.

But now I have another problem (although it might be completely obvious and I'm just too tired right now to think ;)):

I have a bunch of objects of type Foo. And I want to flag certain ones by sticking them in a HashSet? (or at least something similar). I want inclusion in the set to be based purely on the object's reference, not the object's values. Ie, I want this:

Foo fooA = new Foo();
Foo fooB = fooA.makeADuplicate();
assert(fooA !is fooB);
assert(fooA == fooB);
auto set = new someSortOfHashSetIshThing!(Foo)();
set.add(fooA);
assert(!set.contains(fooB));

Problem is, HashSet? seems to want to operate on values instead, so that last assert will fail.

Using "HashSet?!(Foo*)" doesn't give me what I want either (Heh, it's been much longer than I thought since I last did much with pointers):

Foo fooA = new Foo();
Foo stillFooA = fooA;
assert(fooA is stillFooA);
auto set = new HashSet!(Foo*)();
set.add(&fooA);

// Obviously fails because &fooA != &stillFooA
assert(set.contains(&stillFooA));

Sooo...I my very well be missing something obvious here, but any suggestions on how to get what I'm after?

Posted: 05/10/09 18:13:35 -- Modified: 05/10/09 18:14:05 by
torhu

This was discussed in ticket #1271, but I think you need to do it yourself for now. Maybe it's enough just to override opEquals, but I don't know if the default hash uses the reference or the value.

Or you can make your own IdentityHashSet? by just delegating all calls to the real HashSet?, after wrapping the reference in a struct. Kind of like I suggest in the ticket.

Posted: 05/10/09 19:35:00

Ahh, thanks.

torhu wrote:

Maybe it's enough just to override opEquals, but I don't know if the default hash uses the reference or the value.

Ticket #1641 seems to indicate that the default hash uses the reference (which really had me confused for awhile).