www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - a sets implementation

reply Stephan Diehl <stephan.diehl gmx.net> writes:
being new to D (I have a Python background), the first thing to try out 
was a set implementation. Since there seems to be quite an interest in 
this topic, I thought to share this implementation even though it's 
probably quite horrible for the seasoned D programmer :-)
The code can be found here:
http://codespeak.net/svn/user/stephan/d-hacks/sets.d
It tries to capture the look and feel of python sets.
Any comment/feedback is quite welcome.

Cheers

Stephan
Feb 07 2007
parent reply Michiel <nomail hotmail.com> writes:
Not bad. I like how you have integrated some set operations. I posted my own
implementation earlier. And while it is much shorter, it doesn't overload any
operators and is of course not nicely encapsulated like yours.

I would do some things differently, though. For example:

* You copy array-elements over one by one. You can also use array.dup.
* You use a try-catch block for contains(). It's better if you reserve that for
exceptional circumstances. You can also use the 'in' operator.
* I would overload the 'in' operator anyway.
Feb 07 2007
parent reply Stephan Diehl <stephan.diehl gmx.net> writes:
Michiel wrote:
 Not bad. I like how you have integrated some set operations. I posted my own
 implementation earlier. And while it is much shorter, it doesn't overload any
 operators and is of course not nicely encapsulated like yours.
 
 I would do some things differently, though. For example:
 
 * You copy array-elements over one by one. You can also use array.dup.
 * You use a try-catch block for contains(). It's better if you reserve that for
 exceptional circumstances. You can also use the 'in' operator.

that must be a pythonism as try ... except is a common idiom :-)
 * I would overload the 'in' operator anyway.

I tried. But for some reason, the compiler insists on associative arrays when using the 'in' operator. And when used with associative arrays, it will test the values, not the keys. (it's just the other way round in python: the 'in' operator checks for the keys, which actually does make some sense).
Feb 07 2007
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Stephan Diehl wrote:
 Michiel wrote:
 * I would overload the 'in' operator anyway.

I tried. But for some reason, the compiler insists on associative arrays when using the 'in' operator. And when used with associative arrays, it will test the values, not the keys. (it's just the other way round in python: the 'in' operator checks for the keys, which actually does make some sense).

?? I think you're a bit confused here. When used with associative arrays the 'in' operator definitely tests keys, not values[1]. For associative arrays it may return a pointer to the value, but it checks if the key is in the AA. Or did you mean you expect it to return the key? Why would you want that? You already know what the key is or you couldn't have determined it was in the AA in the first place. A common idiom for associative array member testing in D is: --- if (auto p = key in aa) { // 'aa' contains 'key', and associates it with value '*p' } else { // 'aa' does not contain key } --- The idea is that after determining a key is present in an associative array the next thing you'll probably want to have is the value associated with it. A note about overloading: If you want to overload the 'in' operator you'll probably want to use opIn_r (so that it's used for "key in set" expressions), not opIn (which would give you "set in key" syntax, which doesn't make much sense).
Feb 07 2007
parent Stephan Diehl <stephan.diehl gmx.net> writes:
Frits van Bommel wrote:
 Stephan Diehl wrote:
 Michiel wrote:
 * I would overload the 'in' operator anyway.

I tried. But for some reason, the compiler insists on associative arrays when using the 'in' operator. And when used with associative arrays, it will test the values, not the keys. (it's just the other way round in python: the 'in' operator checks for the keys, which actually does make some sense).

?? I think you're a bit confused here.

yes, that's certainly true.
 When used with associative arrays 
 the 'in' operator definitely tests keys, not values[1]. For associative 
 arrays it may return a pointer to the value, but it checks if the key is 
 in the AA.

 
 A note about overloading: If you want to overload the 'in' operator 
 you'll probably want to use opIn_r (so that it's used for "key in set" 
 expressions), not opIn (which would give you "set in key" syntax, which 
 doesn't make much sense).

Ah, yes. It now works as expected. Thanks a lot
Feb 07 2007