digitalmars.D.learn - Rectangular Arrays
- Another Roadside Attraction (22/22) Feb 22 2007 I'm learning D by writing a logic game with a grid-shaped board. If poss...
- Bill Baxter (11/42) Feb 22 2007 D doesn't have rectangular arrays at the moment. But you can init a
- Another Roadside Attraction (10/24) Feb 22 2007 Thanks, Bill!!
- Bill Baxter (22/52) Feb 22 2007 Yeh, I have to look it up every time I need it.
- mike (11/18) Feb 23 2007 That's interesting ... I use two-dim arrays, like
- Bill Baxter (5/24) Feb 23 2007 Static arrays work that way.
- mike (7/12) Feb 23 2007 Yeah, I'm still using static arrays ... thought I had switched to dynami...
- Bill Baxter (8/19) Feb 23 2007 Actually, though, looking at the names of the arguments it's probably
- Orgoton (12/43) Feb 25 2007 You can declare a variable length array by doing something like
- Bill Baxter (4/54) Feb 25 2007 How is that simpler than
- Orgoton (2/57) Feb 25 2007 To my opinion. Yours may differ. Creating buffers using .length is much ...
- Frits van Bommel (4/27) Feb 25 2007 IIRC it's also pretty much exactly what 'new' will do internally (except...
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
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!! --ARAD 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
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
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/ArraysCategoryYeh, 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
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
mike wrote:Am 23.02.2007, 07:37 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. --bbD 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.
Feb 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. --bbYeah, 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
mike wrote:Am 23.02.2007, 19:23 Uhr, schrieb Bill Baxter <dnewsgroup billbaxter.com>:Actually, though, looking at the names of the arguments it's probably really: byte[bufferLength][bufferCount] foo; or C-style byte foo[bufferCount][bufferLength]; ;-) --bbStatic arrays work that way. byte[bufferCount][bufferLength] foo; as long as bufferCount and bufferLength are compile-time constants. --bbYeah, I'm still using static arrays ... thought I had switched to dynamic arrays already. One more thing on my todo-list.
Feb 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!! --ARAYou 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
Orgoton wrote:Another Roadside Attraction Wrote:How is that simpler than auto table = new int[][](HowManyRows,HowManyColumns); ??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!! --ARAYou 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
Bill Baxter Wrote:Orgoton wrote: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).Another Roadside Attraction Wrote:How is that simpler than auto table = new int[][](HowManyRows,HowManyColumns); ??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!! --ARAYou 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
Orgoton wrote:Bill Baxter Wrote: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"?Orgoton wrote: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).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