www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Re: Rectangular Arrays

reply Orgoton <orgoton mindless.com> writes:
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.
Feb 25 2007
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
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); ??
Feb 25 2007
parent reply Orgoton <orgoton mindless.com> writes:
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).
Feb 25 2007
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
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.

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"?
Feb 25 2007