www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - 3D Arrays - Non-block Arrays possible?

reply AEon <aeon2001 lycos.de> writes:
I still do not fully understand how arrays in D really work. And that is 
becoming apparent with 3D arrays. I hope someone can help me better 
understand them and how to manipulate them.

E.g. char[3][5][] would be a 3x5 array, a 3x5 block no matter if element
char[0] ever requires [5] elements or not. This is the way I used to 
handle 3D arrays in C.

But in D I am starting to wonder if

   char[][][] d;

a dynamic array, would let me define

   char[0][5][]
   char[1][2][]
   char[2][3][]

meaning to be able to define how many elements char[0] will have (5), 
then a different number (2) for char[1] etc. Without having memory 
wasted by empty elements?

If this is possible how would one define them?

   char[][][] d;
   d.length = 3;
   d[0].length = 5;
   d[1].length = 2;
   d[2].length = 3;

Would that work?

AEon
Mar 26 2005
parent reply Derek Parnell <derek psych.ward> writes:
On Sun, 27 Mar 2005 00:22:08 +0100, AEon wrote:

 I still do not fully understand how arrays in D really work. And that is 
 becoming apparent with 3D arrays. I hope someone can help me better 
 understand them and how to manipulate them.
 
 E.g. char[3][5][] would be a 3x5 array, a 3x5 block no matter if element
 char[0] ever requires [5] elements or not. This is the way I used to 
 handle 3D arrays in C.
 
 But in D I am starting to wonder if
 
    char[][][] d;
 
 a dynamic array, would let me define
 
    char[0][5][]
    char[1][2][]
    char[2][3][]
 
 meaning to be able to define how many elements char[0] will have (5), 
 then a different number (2) for char[1] etc. Without having memory 
 wasted by empty elements?
 
 If this is possible how would one define them?
 
    char[][][] d;
    d.length = 3;
    d[0].length = 5;
    d[1].length = 2;
    d[2].length = 3;
 
 Would that work?
Yes. Another way of looking at this is ... alias char[] Line; alias Line[] Page; alias Page[] Chapter; void main() { Chapter d; d.length = 3; // This chapter has three pages. d[0].length = 6; // 1st page has 6 lines d[1].length = 7; // 2nd page has 7 lines d[2].length = 8; // 3rd page has 8 lines; d[0][0] = "Once upon a time, in land far, far, away"; d[0][1] = "there lived a crooked little man, named"; d[0][2] = "'Xyzzy'. One day he decided to clean up"; d[0][3] = "his cave. You see, he lived in a colossal"; d[0][4] = "cave, deep underground, with his two"; d[0][5] = "pets; a dwarf and a unicorn."; d[1][0] = "... etc ... etc ... etc "; } -- Derek Parnell Melbourne, Australia 27/03/2005 9:57:29 AM
Mar 26 2005
parent AEon <aeon2001 lycos.de> writes:
Derek Parnell wrote:

 On Sun, 27 Mar 2005 00:22:08 +0100, AEon wrote:
 
I still do not fully understand how arrays in D really work. And that is 
becoming apparent with 3D arrays. I hope someone can help me better 
understand them and how to manipulate them.

E.g. char[3][5][] would be a 3x5 array, a 3x5 block no matter if element
char[0] ever requires [5] elements or not. This is the way I used to 
handle 3D arrays in C.

But in D I am starting to wonder if

   char[][][] d;

a dynamic array, would let me define

   char[0][5][]
   char[1][2][]
   char[2][3][]

meaning to be able to define how many elements char[0] will have (5), 
then a different number (2) for char[1] etc. Without having memory 
wasted by empty elements?

If this is possible how would one define them?

   char[][][] d;
   d.length = 3;
   d[0].length = 5;
   d[1].length = 2;
   d[2].length = 3;

Would that work?
Yes. Another way of looking at this is ... alias char[] Line; alias Line[] Page; alias Page[] Chapter; void main() { Chapter d; d.length = 3; // This chapter has three pages. d[0].length = 6; // 1st page has 6 lines d[1].length = 7; // 2nd page has 7 lines d[2].length = 8; // 3rd page has 8 lines; d[0][0] = "Once upon a time, in land far, far, away"; d[0][1] = "there lived a crooked little man, named"; d[0][2] = "'Xyzzy'. One day he decided to clean up"; d[0][3] = "his cave. You see, he lived in a colossal"; d[0][4] = "cave, deep underground, with his two"; d[0][5] = "pets; a dwarf and a unicorn."; d[1][0] = "... etc ... etc ... etc "; }
Thanx for the nix example, will try that. Since I no longer have to use 3D blocks, this would *massively* reduce the amount of memory some of my temp stats calculations require, where (my guess) something like 90%+ of the arrays is never actually used. And another good reason to use D :) AEon
Mar 26 2005