July 22, 2002 Re: bah! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | On Sun, 21 Jul 2002 18:51:06 -0700 "Walter" <walter@digitalmars.com> wrote:
> D already supports rectangular arrays - just supply a dimension in the brackets. No need for a separate syntax.
That'd be only partially dynamic:
int[][5] array;
If I get it right, it is stored in the memory in the following way:
4 bytes - length (L)
L*5 bytes - data
What I suppose is, when you use the [,] syntax, the array uses _two_ ints to store _two_ dimensions:
int[,] array;
The memory layout would be:
4 bytes - width (W)
4 bytes - height (H)
W*H*5 bytes - data
So, _both_ width and height can change at run-time. This solves another problem - remember the complaints about the fact that static multidimensional arrays cannot be converted to dynamic ones?
int det(double[][] matrix) { ... }
double matrix[3][3];
det(matrix); // error!
Well, with the syntax I propose, you can:
int det(double[,] matrix) { ... }
double matrix[3,3];
det(matrix); // okay!
Converting such static array to dynamic is as simple as converting multidimensional arrays: just prepend width and height.
|
July 22, 2002 Re: bah! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | Ok, I see what you mean. "Pavel Minayev" <evilone@omen.ru> wrote in message news:CFN374594251942477@news.digitalmars.com... On Sun, 21 Jul 2002 18:51:06 -0700 "Walter" <walter@digitalmars.com> wrote: > D already supports rectangular arrays - just supply a dimension in the brackets. No need for a separate syntax. That'd be only partially dynamic: int[][5] array; If I get it right, it is stored in the memory in the following way: 4 bytes - length (L) L*5 bytes - data What I suppose is, when you use the [,] syntax, the array uses _two_ ints to store _two_ dimensions: int[,] array; The memory layout would be: 4 bytes - width (W) 4 bytes - height (H) W*H*5 bytes - data So, _both_ width and height can change at run-time. This solves another problem - remember the complaints about the fact that static multidimensional arrays cannot be converted to dynamic ones? int det(double[][] matrix) { ... } double matrix[3][3]; det(matrix); // error! Well, with the syntax I propose, you can: int det(double[,] matrix) { ... } double matrix[3,3]; det(matrix); // okay! Converting such static array to dynamic is as simple as converting multidimensional arrays: just prepend width and height. |
July 22, 2002 Re: bah! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | On Mon, 22 Jul 2002 11:50:31 -0700 "Walter" <walter@digitalmars.com> wrote:
> Ok, I see what you mean.
By the way, I've seen it in C#.
|
July 23, 2002 Re: bah! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Of course you should go further then just 2D (not to be confused with matrix 2d). int[w,h,l] array; ect... "Walter" <walter@digitalmars.com> wrote in message news:ahhm2a$244l$1@digitaldaemon.com... > Ok, I see what you mean. > > "Pavel Minayev" <evilone@omen.ru> wrote in message > news:CFN374594251942477@news.digitalmars.com... > On Sun, 21 Jul 2002 18:51:06 -0700 "Walter" <walter@digitalmars.com> wrote: > > > D already supports rectangular arrays - just supply a dimension in the brackets. No need for a separate syntax. > > That'd be only partially dynamic: > > int[][5] array; > > If I get it right, it is stored in the memory in the following way: > > 4 bytes - length (L) > L*5 bytes - data > > What I suppose is, when you use the [,] syntax, the array uses _two_ ints to store _two_ dimensions: > > int[,] array; > > The memory layout would be: > > 4 bytes - width (W) > 4 bytes - height (H) > W*H*5 bytes - data > > So, _both_ width and height can change at run-time. This solves another > problem - remember the complaints about the fact that static > multidimensional > arrays cannot be converted to dynamic ones? > > int det(double[][] matrix) { ... } > > double matrix[3][3]; > det(matrix); // error! > > Well, with the syntax I propose, you can: > > int det(double[,] matrix) { ... } > > double matrix[3,3]; > det(matrix); // okay! > > Converting such static array to dynamic is as simple as converting multidimensional arrays: just prepend width and height. > > |
July 23, 2002 Re: bah! | ||||
---|---|---|---|---|
| ||||
Posted in reply to C.R.Chafer | > [][] is used to designate a ragged array. > [,] could be implemented for rectangular arrays. > > so. > int[4][7] array1; // is 4 ptrs to arrays of int length 7 named array1 > // (accesses required at least 1 pointer dereference) > int[4,7] array2; // is array of width 4, length 7 named array2 > // (accesses do not require a pointer dereference - > // but do required a multiply / shift and add - though > // these may be optimised away some times) > Man, this is way cool. |
July 23, 2002 Re: bah! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sandor Hojtsy | In article <ahj122$k31$1@digitaldaemon.com>, Sandor Hojtsy says... > >> [][] is used to designate a ragged array. >> [,] could be implemented for rectangular arrays. >> >> so. >> int[4][7] array1; // is 4 ptrs to arrays of int length 7 >named array1 >> // (accesses required at least 1 pointer >dereference) >> int[4,7] array2; // is array of width 4, length 7 named >array2 >> // (accesses do not require a pointer >dereference - >> // but do required a multiply / shift and add - >though >> // these may be optimised away some times) >> But then the only difference is that the [][] arrays are created by pointers and that the [,] are now just arrays that has data that is closer together! Now there is no point! It's like the difference between a struct and a class for god's sake! |
July 23, 2002 Re: bah! | ||||
---|---|---|---|---|
| ||||
Posted in reply to andrew n | On Tue, 23 Jul 2002 18:27:12 +0000 (UTC) andrew n <andrew_member@pathlink.com> wrote: > In article <ahj122$k31$1@digitaldaemon.com>, Sandor Hojtsy says... > But then the only difference is that the [][] arrays are created by pointers and > that the [,] are now just arrays that has data that is closer together! Now There IS a difference. [,] array is _guaranteed_ to be rectangle (that is, each row has the same size). There is no such guarantee for [][] arrays. So these are two _fundamentally_ different data types. Also, [,] arrays can be converted from static to dynamic and vice versa easily. This is not true for [][] arrays. For example, try the following: void calc(double[][] matrix) { // do something inside } double[3][3] matrix; calc(matrix); It won't work, for obvious reasons. And [,] arrays don't have such problem. |
July 23, 2002 Re: bah! | ||||
---|---|---|---|---|
| ||||
Posted in reply to andrew n | "andrew n" <andrew_member@pathlink.com> wrote in message news:ahk760$iaa$1@digitaldaemon.com... > <SNIP> > > But then the only difference is that the [][] arrays are created by pointers and > that the [,] are now just arrays that has data that is closer together! Now > there is no point! It's like the difference between a struct and a class for > god's sake! > > Sometimes you need the data to be contiguous in memory, such as with screen buffers. The ragged arrays cannot be used then. The normal solution is to use a 1d array or a pointer, and calculate the index or offset yourself: int[1024*768] screen; void drawPixel (in int x, in int y, in int color) { int index = y * 1024 + x; screen[index] = color; } It would be cool if D could provide syntax sugar for this kind of thing, because it is quitte common. -- Stijn OddesE_XYZ@hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail |
July 25, 2002 Re: bah! | ||||
---|---|---|---|---|
| ||||
Posted in reply to OddesE | "OddesE" <OddesE_XYZ@hotmail.com> wrote in message news:ahkor9$12dn$1@digitaldaemon.com... > Sometimes you need the data to be contiguous in memory, > such as with screen buffers. The ragged arrays cannot be > used then. The normal solution is to use a 1d array or > a pointer, and calculate the index or offset yourself: > > int[1024*768] screen; > > void drawPixel (in int x, in int y, in int color) > { > int index = y * 1024 + x; > screen[index] = color; > } > > It would be cool if D could provide syntax sugar for > this kind of thing, because it is quitte common. It already does: int[1024][768] screen; |
July 25, 2002 Re: bah! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | On Wed, 24 Jul 2002 22:41:53 -0700 "Walter" <walter@digitalmars.com> wrote:
>> It would be cool if D could provide syntax sugar for
>> this kind of thing, because it is quitte common.
>
> It already does:
> int[1024][768] screen;
It is so if you know the exact resolution when you write the program. But what if you want the user to be able to change it at run-time? Then, you'll need dynamic allocation anyhow...
|
Copyright © 1999-2021 by the D Language Foundation