Associative arrays question

Robby robby.lansaw at gmail.com
Mon Dec 31 02:50:34 PST 2007


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);
	}


More information about the Digitalmars-d-learn mailing list