www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Sets / associative arrays with void values?

reply "Vladimir Panteleev" <tehcybershadow gmail.com> writes:
I've been wondering whether D supported sets, or something similar. What I  
have in mind isn't unlike associative arrays, but without values: an  
unordered list with quick look-up and simple addition/removal of elements,  
yet still capable of enumeration, etc.

I followed my intuition and tried to declare an associative array with a  
"void" value type:

void[int] foo;

To my surprise, it compiled. And even the "in" operation and the .keys and  
.values properties worked fine (or as fine as they can work with an empty  
associative array).

Of course, the question comes: how would one add keys to such a construct?  
The practical answer is obvious: use a real "dummy" data type (bit, for  
example) as the value type, but I find it a shame that declaring such  
constructs is allowed - yet you can't really use them.

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Fix obvious typo in e-mail address to reply.
Jan 06 2007
next sibling parent reply Jeff M <jeff jeffrules.com> writes:
On 2007-01-06 16:05:06 -0800, "Vladimir Panteleev" 
<tehcybershadow gmail.com> said:

 I've been wondering whether D supported sets, or something similar. 
 What I  have in mind isn't unlike associative arrays, but without 
 values: an  unordered list with quick look-up and simple 
 addition/removal of elements,  yet still capable of enumeration, etc.
 
 I followed my intuition and tried to declare an associative array with 
 a  "void" value type:
 
 void[int] foo;
 
 To my surprise, it compiled. And even the "in" operation and the .keys 
 and  .values properties worked fine (or as fine as they can work with 
 an empty  associative array).
 
 Of course, the question comes: how would one add keys to such a 
 construct?  The practical answer is obvious: use a real "dummy" data 
 type (bit, for  example) as the value type, but I find it a shame that 
 declaring such  constructs is allowed - yet you can't really use them.
It would be nice if a "void[int]" array was usable. Associative arrays are create-on-access, so "foo[5];" should be a legal for creating an index in the array, but this throws an error. -- Jeff
Jan 06 2007
parent reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
Jeff M wrote:
 On 2007-01-06 16:05:06 -0800, "Vladimir Panteleev" 
 <tehcybershadow gmail.com> said:
 
 I've been wondering whether D supported sets, or something similar. 
 What I  have in mind isn't unlike associative arrays, but without 
 values: an  unordered list with quick look-up and simple 
 addition/removal of elements,  yet still capable of enumeration, etc.

 I followed my intuition and tried to declare an associative array with 
 a  "void" value type:

 void[int] foo;

 To my surprise, it compiled. And even the "in" operation and the .keys 
 and  .values properties worked fine (or as fine as they can work with 
 an empty  associative array).

 Of course, the question comes: how would one add keys to such a 
 construct?  The practical answer is obvious: use a real "dummy" data 
 type (bit, for  example) as the value type, but I find it a shame that 
 declaring such  constructs is allowed - yet you can't really use them.
It would be nice if a "void[int]" array was usable. Associative arrays are create-on-access, so "foo[5];" should be a legal for creating an index in the array, but this throws an error. -- Jeff
Actually I'm pretty sure create-on-access was removed several releases back? And I also thought the compiler no longer allowed a void value type for associative arrays, but I guess I was wrong on that one. -- Chris Nicholson-Sauls
Jan 06 2007
parent reply Carlos Santander <csantander619 gmail.com> writes:
Chris Nicholson-Sauls escribió:
 Jeff M wrote:
 On 2007-01-06 16:05:06 -0800, "Vladimir Panteleev" 
 <tehcybershadow gmail.com> said:

 I've been wondering whether D supported sets, or something similar. 
 What I  have in mind isn't unlike associative arrays, but without 
 values: an  unordered list with quick look-up and simple 
 addition/removal of elements,  yet still capable of enumeration, etc.

 I followed my intuition and tried to declare an associative array 
 with a  "void" value type:

 void[int] foo;

 To my surprise, it compiled. And even the "in" operation and the 
 .keys and  .values properties worked fine (or as fine as they can 
 work with an empty  associative array).

 Of course, the question comes: how would one add keys to such a 
 construct?  The practical answer is obvious: use a real "dummy" data 
 type (bit, for  example) as the value type, but I find it a shame 
 that declaring such  constructs is allowed - yet you can't really use 
 them.
It would be nice if a "void[int]" array was usable. Associative arrays are create-on-access, so "foo[5];" should be a legal for creating an index in the array, but this throws an error. -- Jeff
Actually I'm pretty sure create-on-access was removed several releases back? And I also thought the compiler no longer allowed a void value type for associative arrays, but I guess I was wrong on that one. -- Chris Nicholson-Sauls
That's true. Back then, I used "void[T]" as a set, but since the change, I use "T[T]". BTW, since "void[T]" is mostly useless (for the reasons stated above), would it be reasonable for the compiler to flag such a declaration as an error? -- Carlos Santander Bernal
Jan 07 2007
parent reply Georg Wrede <georg nospam.org> writes:
Carlos Santander wrote:
 Chris Nicholson-Sauls escribió:
 
 Jeff M wrote:

 On 2007-01-06 16:05:06 -0800, "Vladimir Panteleev" 
 <tehcybershadow gmail.com> said:

 I've been wondering whether D supported sets, or something similar. 
 What I  have in mind isn't unlike associative arrays, but without 
 values: an  unordered list with quick look-up and simple 
 addition/removal of elements,  yet still capable of enumeration, etc.

 I followed my intuition and tried to declare an associative array 
 with a  "void" value type:

 void[int] foo;

 To my surprise, it compiled. And even the "in" operation and the 
 .keys and  .values properties worked fine (or as fine as they can 
 work with an empty  associative array).

 Of course, the question comes: how would one add keys to such a 
 construct?  The practical answer is obvious: use a real "dummy" data 
 type (bit, for  example) as the value type, but I find it a shame 
 that declaring such  constructs is allowed - yet you can't really 
 use them.
It would be nice if a "void[int]" array was usable. Associative arrays are create-on-access, so "foo[5];" should be a legal for creating an index in the array, but this throws an error. -- Jeff
Actually I'm pretty sure create-on-access was removed several releases back? And I also thought the compiler no longer allowed a void value type for associative arrays, but I guess I was wrong on that one. -- Chris Nicholson-Sauls
That's true. Back then, I used "void[T]" as a set, but since the change, I use "T[T]". BTW, since "void[T]" is mostly useless (for the reasons stated above), would it be reasonable for the compiler to flag such a declaration as an error?
Or defined as a declaration of a set.
Jan 07 2007
parent Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
Georg Wrede wrote:
 Carlos Santander wrote:
 Chris Nicholson-Sauls escribió:

 Jeff M wrote:

 On 2007-01-06 16:05:06 -0800, "Vladimir Panteleev" 
 <tehcybershadow gmail.com> said:

 I've been wondering whether D supported sets, or something similar. 
 What I  have in mind isn't unlike associative arrays, but without 
 values: an  unordered list with quick look-up and simple 
 addition/removal of elements,  yet still capable of enumeration, etc.

 I followed my intuition and tried to declare an associative array 
 with a  "void" value type:

 void[int] foo;

 To my surprise, it compiled. And even the "in" operation and the 
 .keys and  .values properties worked fine (or as fine as they can 
 work with an empty  associative array).

 Of course, the question comes: how would one add keys to such a 
 construct?  The practical answer is obvious: use a real "dummy" 
 data type (bit, for  example) as the value type, but I find it a 
 shame that declaring such  constructs is allowed - yet you can't 
 really use them.
It would be nice if a "void[int]" array was usable. Associative arrays are create-on-access, so "foo[5];" should be a legal for creating an index in the array, but this throws an error. -- Jeff
Actually I'm pretty sure create-on-access was removed several releases back? And I also thought the compiler no longer allowed a void value type for associative arrays, but I guess I was wrong on that one. -- Chris Nicholson-Sauls
That's true. Back then, I used "void[T]" as a set, but since the change, I use "T[T]". BTW, since "void[T]" is mostly useless (for the reasons stated above), would it be reasonable for the compiler to flag such a declaration as an error?
Or defined as a declaration of a set.
That would suit me just fine. It would need a usable syntax though... perhaps as simple as adding a .add property counter to the existing .remove? -- Chris Nicholson-Sauls
Jan 07 2007
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Vladimir Panteleev wrote:
<snip>
 void[int] foo;
<snip>
 Of course, the question comes: how would one add keys to such a 
 construct? The practical answer is obvious: use a real "dummy" data type 
 (bit, for example) as the value type, but I find it a shame that 
 declaring such constructs is allowed - yet you can't really use them.
Strange indeed. Sounds like a bug. But do have a look at hashset in my utility library: http://pr.stewartsplace.org.uk/d/sutil/ Stewart.
Jan 07 2007