Thread overview
Rectangular Arrays
Feb 23, 2007
Bill Baxter
Feb 23, 2007
Bill Baxter
Feb 23, 2007
mike
Feb 23, 2007
Bill Baxter
Feb 23, 2007
mike
Feb 24, 2007
Bill Baxter
February 23, 2007
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
February 23, 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

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
February 23, 2007
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

February 23, 2007
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
> 
February 23, 2007
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.

-mike

-- 
Erstellt mit Operas revolutionärem E-Mail-Modul: http://www.opera.com/mail/
February 23, 2007
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
February 23, 2007
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.

-- 
Erstellt mit Operas revolutionärem E-Mail-Modul: http://www.opera.com/mail/
February 24, 2007
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