New set class
Michiel
nomail at please.com
Sun Feb 25 03:29:22 PST 2007
Bill Baxter wrote:
>> I have written a new class to represent sets, with several useful
>> features. Uploaded for your review and free use. I don't know much about
>> open source and licensing, but I request that if you use the class, that
>> you keep my name in the author section of the documentation.
>>
>> http://files.michiel.eu/set.d
>>
>> Tell me what you think.
>
> If you add an opApply with the signature
> int opApply(int delegate(uint i, inout Type) dg) { ... }
>
> it will allow this kind of foreach too:
> foreach(i, e; myset) {
> ...
> }
That's why I didn't do it. What meaning would it have for a set, since
they have no index?
> Also I would just leave out opApplyReverse. That way using it is a
> compile-time error. Or if you're dead set on it, at least have it throw
> an exception. I believe assert() is a no-op in release builds, meaning
> your program will fail in strange ways in release. Actually I'm
> surprised that compiles without a return statement.
assert(0) is evaluated by D as a point in the code you can't ever reach.
I got the idea from the opCmp example:
--------------------------------------
For some objects, testing for less or greater makes no sense. For these,
override opCmp() with:
class A
{
int opCmp(Object o)
{
assert(0); // comparison makes no sense
return 0;
}
}
--------------------------------------
I see that that has a return statement, but I don't see why it would
need one. assert(0) always exists the function. I think it's a feature
of the compiler that it recognizes this.
> Finally
> Set!(T[0]) set(T...)(T elements)
> why not just
> Set!(T) set(T)(T elements...) ??
You only use that syntax if T is an aggregate type. So the resulting set
would actually be Set!(T[0]) anyway. I think I tried this before and had
some problems, but I'm not certain now.
> Have you looked at the HashSet implementation in Tango for comparison?
I had not. Is this the one?
http://www.dsource.org/projects/tango/docs/current/tango.util.collection.HashSet.html
Though I haven't looked at the source code, I see it exposes many of its
internal workings to the outside. When you are working with a set you
don't want to know if it's using a hash table or a balanced tree. Just
that it implements a set.
Though I'm sure that they put more work into it than I and that it's
more efficient. I just sneakily used an associative array on the inside.
Which uses its own hash table, if I'm not mistaken.
--
Michiel
More information about the Digitalmars-d
mailing list