Thread overview
Re: Rectangular Arrays
Feb 25, 2007
Orgoton
Feb 25, 2007
Bill Baxter
Feb 25, 2007
Orgoton
Feb 25, 2007
Frits van Bommel
February 25, 2007
Another Roadside Attraction Wrote:

> I'm learning D by writing a logic game with a grid-shaped board. If possible, I'd like to use a rectangular array for the board, but I'd like to define the width of the board at runtime when I instantiate the class. Ideally, I'd like to do something like this:
> 
> public class Board {
> 
> 	private Piece[,] grid;
> 
> 	public this(uint width) {
> 		grid = new Piece[width,width];
> 	}
> 
> }
> 
> But that doesn't compile. It looks like the dimensions of rectangular arrays has to be known at compile-time. Is that right, or am I missing something?
> 
> I've also tried declaring the array as an ordinary multi-dimensional array, but this doesn't compile either:
> 
> public class Board {
> 
> 	private Piece[][] grid;
> 
> 	public this(uint width) {
> 		grid = new Piece[width][width];
> 	}
> 
> }
> 
> What's the best way to write this code? As I expand on this project, I plan on incorporating some time-consuming algorithms (think, n-Queens problem), so I'd prefer to use the most efficent data structure possible for the board.
> 
> Thanks!!
> 
> --ARA


You can declare a variable length array by doing something like

int numbers[];

//in runtime on the program:::
numbers.length=size;
//for bidimensionality:
int table[][]; //or matrix
table.length=HowManyCollumns;
foreache (int[] row, table[]) row.length=HowManyRows; //or the size of the collumn

See also:
http://www.digitalmars.com/d/arrays.html

Specially the dynamic array thingy. This is the best solution I have for you and I belive this one to be the simplest you got so far.
February 25, 2007
Orgoton wrote:
> Another Roadside Attraction Wrote:
> 
>> I'm learning D by writing a logic game with a grid-shaped board. If possible, I'd like to use a rectangular array for the board, but I'd like to define the width of the board at runtime when I instantiate the class. Ideally, I'd like to do something like this:
>>
>> public class Board {
>> 	
>> 	private Piece[,] grid;
>> 	
>> 	public this(uint width) {
>> 		grid = new Piece[width,width];
>> 	}
>>
>> }
>>
>> But that doesn't compile. It looks like the dimensions of rectangular arrays has to be known at compile-time. Is that right, or am I missing something?
>>
>> I've also tried declaring the array as an ordinary multi-dimensional array, but this doesn't compile either:
>>
>> public class Board {
>> 	
>> 	private Piece[][] grid;
>> 	
>> 	public this(uint width) {
>> 		grid = new Piece[width][width];
>> 	}
>>
>> }
>>
>> What's the best way to write this code? As I expand on this project, I plan on incorporating some time-consuming algorithms (think, n-Queens problem), so I'd prefer to use the most efficent data structure possible for the board.
>>
>> Thanks!!
>>
>> --ARA
> 
> 
> You can declare a variable length array by doing something like
> 
> int numbers[];
> 
> //in runtime on the program:::
> numbers.length=size;
> //for bidimensionality:
> int table[][]; //or matrix
> table.length=HowManyCollumns;
> foreache (int[] row, table[]) row.length=HowManyRows; //or the size of the collumn
> 
> See also:
> http://www.digitalmars.com/d/arrays.html
> 
> Specially the dynamic array thingy. This is the best solution I have for you and I belive this one to be the simplest you got so far.

How is that simpler than
    auto table = new int[][](HowManyRows,HowManyColumns);
??
February 25, 2007
Bill Baxter Wrote:

> Orgoton wrote:
> > Another Roadside Attraction Wrote:
> > 
> >> I'm learning D by writing a logic game with a grid-shaped board. If possible, I'd like to use a rectangular array for the board, but I'd like to define the width of the board at runtime when I instantiate the class. Ideally, I'd like to do something like this:
> >>
> >> public class Board {
> >> 
> >> 	private Piece[,] grid;
> >> 
> >> 	public this(uint width) {
> >> 		grid = new Piece[width,width];
> >> 	}
> >>
> >> }
> >>
> >> But that doesn't compile. It looks like the dimensions of rectangular arrays has to be known at compile-time. Is that right, or am I missing something?
> >>
> >> I've also tried declaring the array as an ordinary multi-dimensional array, but this doesn't compile either:
> >>
> >> public class Board {
> >> 
> >> 	private Piece[][] grid;
> >> 
> >> 	public this(uint width) {
> >> 		grid = new Piece[width][width];
> >> 	}
> >>
> >> }
> >>
> >> What's the best way to write this code? As I expand on this project, I plan on incorporating some time-consuming algorithms (think, n-Queens problem), so I'd prefer to use the most efficent data structure possible for the board.
> >>
> >> Thanks!!
> >>
> >> --ARA
> > 
> > 
> > You can declare a variable length array by doing something like
> > 
> > int numbers[];
> > 
> > //in runtime on the program:::
> > numbers.length=size;
> > //for bidimensionality:
> > int table[][]; //or matrix
> > table.length=HowManyCollumns;
> > foreache (int[] row, table[]) row.length=HowManyRows; //or the size of the collumn
> > 
> > See also:
> > http://www.digitalmars.com/d/arrays.html
> > 
> > Specially the dynamic array thingy. This is the best solution I have for you and I belive this one to be the simplest you got so far.
> 
> How is that simpler than
>      auto table = new int[][](HowManyRows,HowManyColumns);
> ??

To my opinion. Yours may differ. Creating buffers using .length is much more immediate than using new. Also, resizing the same buffers is much simpler. Sure the "foreach" stuff is cumbersome, but the OP mentioned he is learning D, so it'd be best if he gets the properties of (dynamic) arrays and the foreach (which is nothing more than a simplification of common for loops).
February 25, 2007
Orgoton wrote:
> Bill Baxter Wrote:
> 
>> Orgoton wrote:
>>> You can declare a variable length array by doing something like
>>>
>>> int numbers[];
>>>
>>> //in runtime on the program:::
>>> numbers.length=size;
>>> //for bidimensionality:
>>> int table[][]; //or matrix
>>> table.length=HowManyCollumns;
>>> foreache (int[] row, table[]) row.length=HowManyRows; //or the size of the collumn
>>>
>>> See also:
>>> http://www.digitalmars.com/d/arrays.html
>>>
>>> Specially the dynamic array thingy. This is the best solution I have for you and I belive this one to be the simplest you got so far.
>> How is that simpler than
>>      auto table = new int[][](HowManyRows,HowManyColumns);
>> ??
> 
> To my opinion. Yours may differ. Creating buffers using .length is much more immediate than using new. Also, resizing the same buffers is much simpler. Sure the "foreach" stuff is cumbersome, but the OP mentioned he is learning D, so it'd be best if he gets the properties of (dynamic) arrays and the foreach (which is nothing more than a simplification of common for loops).

IIRC it's also pretty much exactly what 'new' will do internally (except it does it more generically). Do you always consider re-implementing the standard library "simpler"?