www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Associative arrays question

reply Robby <robby.lansaw gmail.com> writes:
It's probably quite simple, but for some reason I seem to be in a blank.

I'm trying to write an associative array where a enum is the key and a 
array of classes is the value.

So given a simple example
enum T {Zero,One,Two,Three,Four,Five}
class L {}

How would I write the declaration for an associative array?

I thought it may be  L[T[]][] but it's clearly not, help? 
Dec 31 2007
next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Mon, 31 Dec 2007 04:20:34 -0500, Robby wrote:

 It's probably quite simple, but for some reason I seem to be in a blank.
 
 I'm trying to write an associative array where a enum is the key and a 
 array of classes is the value.
 
 So given a simple example
 enum T {Zero,One,Two,Three,Four,Five}
 class L {}
 
 How would I write the declaration for an associative array?
 
 I thought it may be  L[T[]][] but it's clearly not, help?
I would have thought that L[][T] myAA; myAA[One] = new L; would be it. An array of classes ... L[] The key is an enum (T) ... [T] But did you mean that each element in the AA is an array of classes? In that case ... L[][][T] myAA; L[] classarray; classarray ~= new L; classarray ~= new L; classarray ~= new L; myAA[One] = classarray; -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Dec 31 2007
parent reply Robby <robby.lansaw gmail.com> writes:
Derek Parnell wrote:
 On Mon, 31 Dec 2007 04:20:34 -0500, Robby wrote:
 
 It's probably quite simple, but for some reason I seem to be in a blank.

 I'm trying to write an associative array where a enum is the key and a 
 array of classes is the value.

 So given a simple example
 enum T {Zero,One,Two,Three,Four,Five}
 class L {}

 How would I write the declaration for an associative array?

 I thought it may be  L[T[]][] but it's clearly not, help?
I would have thought that L[][T] myAA; myAA[One] = new L; would be it. An array of classes ... L[] The key is an enum (T) ... [T] But did you mean that each element in the AA is an array of classes? In that case ... L[][][T] myAA; L[] classarray; classarray ~= new L; classarray ~= new L; classarray ~= new L; myAA[One] = classarray;
Thanks!. Dunno what I was seeing, took the documentation one way too literal, I think. First one worked a treat. I'm assuming that .rehash isn't really going to do much considering I'm using ints as the keys? Any real performance pitfalls when it comes to associative arrays that's not been documented to watch for? ~Robby class L {} enum T {Zero,One,Two,Three,Four,Five} //.... L y = new L(); L x = new L(); L z = new L(); L[][T] tested; { tested[T.One] = [y, x, z]; tested[T.One] ~= y; tested[T.One] ~= x; tested[T.One] ~= z; tested[T.Two] ~= [x, y]; tested[T.Five] ~= x; } L[] curious = tested[T.One]; { assert ((T.Four in tested) is null); assert (curious.length == 6); assert (tested[T.Five].length == 1); assert (tested.keys.length == 3); }
Dec 31 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Robby" <robby.lansaw gmail.com> wrote in message 
news:flahhv$ibd$1 digitalmars.com...

 I'm assuming that .rehash isn't really going to do much considering I'm 
 using ints as the keys?
The AA implementation in D uses a form of separate chaining where each slot in the hashtable points to a data structure used to resolve collisions. In the case of the D AAs, it uses a binary tree at each slot. What rehash does is makes the hash table bigger and reinserts all the keys into it. If it's bigger in one direction, it'll be smaller in the other -- that is, the average depth of each tree will get smaller, maybe even as small as 1 node for each tree, making lookups faster.
 Any real performance pitfalls when it comes
to
 associative arrays that's not been documented to watch for?
As long as you don't need to _duplicate_ them they should be quite performant.
Dec 31 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message 
news:flavc0$1f0e$1 digitalmars.com...

 I'm assuming that .rehash isn't really going to do much considering I'm 
 using ints as the keys?
The AA implementation in D uses a form of separate chaining where each slot in the hashtable points to a data structure used to resolve collisions. In the case of the D AAs, it uses a binary tree at each slot. What rehash does is makes the hash table bigger and reinserts all the keys into it. If it's bigger in one direction, it'll be smaller in the other -- that is, the average depth of each tree will get smaller, maybe even as small as 1 node for each tree, making lookups faster.
I forgot where I was headed with this :) the thing is that it doesn't really matter what type your keys are, .rehash should yield about the same speedup.
Dec 31 2007
prev sibling parent reply =?ISO-8859-1?Q?=22J=E9r=F4me_M=2E_Berger=22?= <jeberger free.fr> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Robby wrote:
 It's probably quite simple, but for some reason I seem to be in a blank.
 
 I'm trying to write an associative array where a enum is the key and a
 array of classes is the value.
 
 So given a simple example
 enum T {Zero,One,Two,Three,Four,Five}
 class L {}
 
 How would I write the declaration for an associative array?
 
 I thought it may be  L[T[]][] but it's clearly not, help?
Wouldn't it be L[][T]? Jerome - -- +------------------------- Jerome M. BERGER ---------------------+ | mailto:jeberger free.fr | ICQ: 238062172 | | http://jeberger.free.fr/ | Jabber: jeberger jabber.fr | +---------------------------------+------------------------------+ -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQFHeLzUd0kWM4JG3k8RAtXFAJ9g6lHfhjg9YHZx3BEedlvs2oOc+QCghKVY JKj2AwDY+DTaGMJQ8PS8qNU= =+tA2 -----END PGP SIGNATURE-----
Dec 31 2007
parent Robby <robby.lansaw gmail.com> writes:
ta!
~Robby
Jérôme M. Berger wrote:
 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1
 
 Robby wrote:
 It's probably quite simple, but for some reason I seem to be in a blank.

 I'm trying to write an associative array where a enum is the key and a
 array of classes is the value.

 So given a simple example
 enum T {Zero,One,Two,Three,Four,Five}
 class L {}

 How would I write the declaration for an associative array?

 I thought it may be  L[T[]][] but it's clearly not, help?
Wouldn't it be L[][T]? Jerome - -- +------------------------- Jerome M. BERGER ---------------------+ | mailto:jeberger free.fr | ICQ: 238062172 | | http://jeberger.free.fr/ | Jabber: jeberger jabber.fr | +---------------------------------+------------------------------+ -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQFHeLzUd0kWM4JG3k8RAtXFAJ9g6lHfhjg9YHZx3BEedlvs2oOc+QCghKVY JKj2AwDY+DTaGMJQ8PS8qNU= =+tA2 -----END PGP SIGNATURE-----
Dec 31 2007