www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Rectangular Arrays

reply Another Roadside Attraction <me me.com> writes:
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
Feb 22 2007
next sibling parent reply Bill Baxter <dnewsgroup billbaxter.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
D doesn't have rectangular arrays at the moment. But you can init a multi-dim ragged array. http://www.digitalmars.com/d/expression.html#NewExpression int[][][] bar = new int[][][](5,20,30); or auto bar = new int[][][](5,20,30); I have some nd rectangular array at http://www.dsource.org/projects/multiarray but it sounds like for your purposes the ragged array will be sufficient. --bb
Feb 22 2007
next sibling parent reply Another Roadside Attraction <me me.com> writes:
Thanks, Bill!!

I hadn't seen the array initialization syntax with the parenthetized arguments.
It's kind of odd-looking. Do know know anything about its etymology? Since
arrays already have bracketized declaration syntax, the parenthetized syntax
seems like an unnecessary kludge.

Anyhow, the parenthetized syntax doesn't seem to be anywhere on the digital
mars website or in any of the dsource tutorials.

http://digitalmars.com/d/arrays.html
http://www.dsource.org/projects/tutorials/wiki/ArraysCategory

Unfortunately, your multi-array library won't work for me, since it relies on
templates. I won't know the size of the grid until runtime, so templates are a
no-go.

But the ragged arrays should be fine for now.

Thanks again,

--ARA


Bill Baxter Wrote:

 D doesn't have rectangular arrays at the moment.  But you can init a 
 multi-dim ragged array.
 
 http://www.digitalmars.com/d/expression.html#NewExpression
 
 int[][][] bar = new int[][][](5,20,30);
 or
 auto bar = new int[][][](5,20,30);
 
 I have some nd rectangular array at 
 http://www.dsource.org/projects/multiarray
 but it sounds like for your purposes the ragged array will be sufficient.
 
 --bb
Feb 22 2007
parent Bill Baxter <dnewsgroup billbaxter.com> writes:
Another Roadside Attraction wrote:
 Thanks, Bill!!
 
 I hadn't seen the array initialization syntax with the parenthetized
arguments. It's kind of odd-looking. Do know know anything about its etymology?
Since arrays already have bracketized declaration syntax, the parenthetized
syntax seems like an unnecessary kludge.
Yeh, I have to look it up every time I need it. But if you think of int[][][] as the complete type, it kind of makes sense. It's just Type(arguments) where Type is int[][][] and arguments are (5,10,20). So it's actually very consistent with standard constructor syntax. In fact I'd guess you can probably do: alias int[][][] Type; Type x = new Type(5,10,20); and it will probably work. ...And now that I've realized that, I probably won't forget that syntax ever again. :-)
 Anyhow, the parenthetized syntax doesn't seem to be anywhere on the digital
mars website or in any of the dsource tutorials.
 
 http://digitalmars.com/d/arrays.html
 http://www.dsource.org/projects/tutorials/wiki/ArraysCategory
Yeh, it should be added to arrays, or at least the wiki comments page. Could you add it if you have a sec?
 Unfortunately, your multi-array library won't work for me, since it relies on
templates. I won't know the size of the grid until runtime, so templates are a
no-go.
Nope that's not an issue, at least with my multi-array lib. You only need to know the _type_ of the elements at compile time. int[] sz = [5,20,30]; auto array1 = new ndarray!(int)(sz); auto array2 = new ndarray!(int)(sz[0],sz[1],sz[2]); But really you don't need it if you're just trying to make a grid of numbers that's accessibly by index.
 But the ragged arrays should be fine for now.
Yep, I suspect so as well.
 Thanks again,
No prob.
 --ARA
 
 
 Bill Baxter Wrote:
 
 D doesn't have rectangular arrays at the moment.  But you can init a 
 multi-dim ragged array.

 http://www.digitalmars.com/d/expression.html#NewExpression

 int[][][] bar = new int[][][](5,20,30);
 or
 auto bar = new int[][][](5,20,30);

 I have some nd rectangular array at 
 http://www.dsource.org/projects/multiarray
 but it sounds like for your purposes the ragged array will be sufficient.

 --bb
Feb 22 2007
prev sibling parent reply mike <vertex gmx.at> writes:
Am 23.02.2007, 07:37 Uhr, schrieb Bill Baxter <dnewsgroup billbaxter.com=
:
 D doesn't have rectangular arrays at the moment.  But you can init a  =
 multi-dim ragged array.

 http://www.digitalmars.com/d/expression.html#NewExpression

 int[][][] bar =3D new int[][][](5,20,30);
 or
 auto bar =3D new int[][][](5,20,30);
That's interesting ... I use two-dim arrays, like ' byte[bufferCount][bufferLength] and didn't even know this syntax, but apparently it works. Need to take = a = look at what exactly I did, I can't remember and don't have my code on = this machine here. -mike -- = Erstellt mit Operas revolution=E4rem E-Mail-Modul: http://www.opera.com/= mail/
Feb 23 2007
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
mike wrote:
 Am 23.02.2007, 07:37 Uhr, schrieb Bill Baxter <dnewsgroup billbaxter.com>:
 
 D doesn't have rectangular arrays at the moment.  But you can init a 
 multi-dim ragged array.

 http://www.digitalmars.com/d/expression.html#NewExpression

 int[][][] bar = new int[][][](5,20,30);
 or
 auto bar = new int[][][](5,20,30);
That's interesting ... I use two-dim arrays, like ' byte[bufferCount][bufferLength] and didn't even know this syntax, but apparently it works. Need to take a look at what exactly I did, I can't remember and don't have my code on this machine here.
Static arrays work that way. byte[bufferCount][bufferLength] foo; as long as bufferCount and bufferLength are compile-time constants. --bb
Feb 23 2007
parent reply mike <vertex gmx.at> writes:
Am 23.02.2007, 19:23 Uhr, schrieb Bill Baxter <dnewsgroup billbaxter.com=
:
 Static arrays work that way.
     byte[bufferCount][bufferLength] foo;

 as long as bufferCount and bufferLength are compile-time constants.

 --bb
Yeah, I'm still using static arrays ... thought I had switched to dynami= c = arrays already. One more thing on my todo-list. -- = Erstellt mit Operas revolution=E4rem E-Mail-Modul: http://www.opera.com/= mail/
Feb 23 2007
parent Bill Baxter <dnewsgroup billbaxter.com> writes:
mike wrote:
 Am 23.02.2007, 19:23 Uhr, schrieb Bill Baxter <dnewsgroup billbaxter.com>:
 
 Static arrays work that way.
     byte[bufferCount][bufferLength] foo;

 as long as bufferCount and bufferLength are compile-time constants.

 --bb
Yeah, I'm still using static arrays ... thought I had switched to dynamic arrays already. One more thing on my todo-list.
Actually, though, looking at the names of the arguments it's probably really: byte[bufferLength][bufferCount] foo; or C-style byte foo[bufferCount][bufferLength]; ;-) --bb
Feb 23 2007
prev sibling parent 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.
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"?
Feb 25 2007