Thread overview
allocate an array of AA?
Mar 08, 2008
sa
Mar 08, 2008
bearophile
Mar 08, 2008
sa
Mar 08, 2008
bearophile
March 08, 2008
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
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
== 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
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