View mode: basic / threaded / horizontal-split · Log in · Help
March 08, 2008
allocate an array of AA?
How to allocate an array of AA?

I tried two ways (new, and setting array length), in both cases the code
compiles, but the executable seems run into infinite loop, and never finish.

void allocAA() {
 int[int][] maps;
 maps = new int[int][3];
 maps.length = 3;
}

int main() {
 allocAA();

 return 0;
}
March 08, 2008
Re: allocate an array of AA?
sa Wrote:
> How to allocate an array of AA?
> I tried two ways (new, and setting array length), in both cases the code
> compiles, but the executable seems run into infinite loop, and never finish.

It's a bug of DMD. I think you have to use a loop with a creation followed by an append (~=) to the array.

Bye,
bearophile
March 08, 2008
Re: allocate an array of AA?
== Quote from bearophile (bearophileHUGS@lycos.com)'s article
> sa Wrote:
> > How to allocate an array of AA?
> > I tried two ways (new, and setting array length), in both cases the code
> > compiles, but the executable seems run into infinite loop, and never finish.
> It's a bug of DMD. I think you have to use a loop with a creation followed by an
append (~=) to the array.


But how to write such 'a creation'? i.e. how to new an AA? what's the syntax?

tried:

 int[int]* mapsp;
 mapsp = new int[int];  // this line

The compiler complains: need size of rightmost array, not type int
March 08, 2008
Re: allocate an array of AA?
sa:
> But how to write such 'a creation'? i.e. how to new an AA? what's the syntax?

I think some sugar can be added to the language for this. In the meantime you can use this from my D libs:

/***************************************
Return a new empty associative array of the given KeyType,ValueType.

Example: AA!(int, string);
*/
template AA(KeyType, ValueType) {
   const ValueType[KeyType] AA = AA_impl!(KeyType, ValueType).res;
}

private template AA_impl(KeyType, ValueType) {
   ValueType[KeyType] result;
   const ValueType[KeyType] res = result;
}

to be used as something as:

aaa char[int][];
foreach(_; xrange(10))
   aaa ~= AA!(int, char);

Bye,
bearophile
Top | Discussion index | About this forum | D home