www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Bidimensional dynamic array problem

reply orgoton <orgoton mindless.com> writes:
I have this

struct Vertex{
(...)
float z;
}

then:
Vertex vData[][];

Somewhere later:
vData.length=sizeX;
foreach(Vertex strip[]; vData[]) strip.length=sizeY;

Here the array is initialized. So far, I think I haven't done anything illegal.
(SizeX and SizeY are runtime values, ushort vars). So first I say how many
strips I want, and next, I put in how many elements that strip will have.

ulong m;
foreach (Vertex[] strip; vData)
{
      foreach (Vertex vertex; strip)
      {
           file.read(vertex.height);
           m++;
      }
}

Here I read the values from a file. First, I select a strip, and then go
through all the elements of that array and read their values. "m" is a counter
to see how many elements where read.

The thing is, m=0 at the end of the loop. What did I do wrong?
Mar 06 2007
next sibling parent orgoton <orgoton mindless.com> writes:
Just a minor correction:
 struct Vertex{
 (...)
 float z;
 }
should be struct Vertex{ (...) float height; } (typo)
Mar 06 2007
prev sibling next sibling parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
orgoton wrote:
 I have this
 
 struct Vertex{
 (...)
 float z;
 }
 
 then:
 Vertex vData[][];
 
 Somewhere later:
 vData.length=sizeX;
 foreach(Vertex strip[]; vData[]) strip.length=sizeY;
foreach(inout Vertex[] strip; vData) strip.length = sizeY; or just: foreach(inout strip; vData) strip.length = sizeY; Note: only the inout is significant, the rest is just nitpicking :P. If you don't specify 'inout', you're just modifying a local copy of the element... And IIRC something like "vData = new Vertex[][](sizeX, sizeY)" should replace that entire code sequence, initializing the outer array to an array of sizeX arrays of length sizeY.
 Here the array is initialized. So far, I think I haven't done anything
illegal. (SizeX and SizeY are runtime values, ushort vars). So first I say how
many strips I want, and next, I put in how many elements that strip will have.
You haven't done anything illegal, just something wrong ;). You haven't initialized the array the way you thought you did: it's just an array of _empty_ dynamic arrays at this point.
 ulong m;
 foreach (Vertex[] strip; vData)
(this loop doesn't require 'inout' since the array itself isn't modified, just the data it references)
 {
       foreach (Vertex vertex; strip)
foreach(inout Vertex vertex; strip) (or: foreach(inout vertex; strip))
       {
            file.read(vertex.height);
            m++;
       }
 }
 
 Here I read the values from a file. First, I select a strip, and then go
through all the elements of that array and read their values. "m" is a counter
to see how many elements where read.
 
 The thing is, m=0 at the end of the loop. What did I do wrong?
Since you didn't initialize the elements of vData, their lengths are all still zero. So your inner loop is never executed.
Mar 06 2007
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Frits van Bommel wrote:
 orgoton wrote:
 
 ulong m;
 foreach (Vertex[] strip; vData)
(this loop doesn't require 'inout' since the array itself isn't modified, just the data it references)
Sorry, that's not right. Not even the data is modified, just the data referenced *by* the data...
 {
       foreach (Vertex vertex; strip)
foreach(inout Vertex vertex; strip) (or: foreach(inout vertex; strip))
       {
            file.read(vertex.height);
            m++;
       }
 }
Mar 06 2007
prev sibling parent orgoton <orgoton mindless.com> writes:
Frits van Bommel Wrote:

 orgoton wrote:
 I have this
 
 struct Vertex{
 (...)
 float z;
 }
 
 then:
 Vertex vData[][];
 
 Somewhere later:
 vData.length=sizeX;
 foreach(Vertex strip[]; vData[]) strip.length=sizeY;
foreach(inout Vertex[] strip; vData) strip.length = sizeY; or just: foreach(inout strip; vData) strip.length = sizeY; Note: only the inout is significant, the rest is just nitpicking :P. If you don't specify 'inout', you're just modifying a local copy of the element... And IIRC something like "vData = new Vertex[][](sizeX, sizeY)" should replace that entire code sequence, initializing the outer array to an array of sizeX arrays of length sizeY.
 Here the array is initialized. So far, I think I haven't done anything
illegal. (SizeX and SizeY are runtime values, ushort vars). So first I say how
many strips I want, and next, I put in how many elements that strip will have.
You haven't done anything illegal, just something wrong ;). You haven't initialized the array the way you thought you did: it's just an array of _empty_ dynamic arrays at this point.
 ulong m;
 foreach (Vertex[] strip; vData)
(this loop doesn't require 'inout' since the array itself isn't modified, just the data it references)
 {
       foreach (Vertex vertex; strip)
foreach(inout Vertex vertex; strip) (or: foreach(inout vertex; strip))
       {
            file.read(vertex.height);
            m++;
       }
 }
 
 Here I read the values from a file. First, I select a strip, and then go
through all the elements of that array and read their values. "m" is a counter
to see how many elements where read.
 
 The thing is, m=0 at the end of the loop. What did I do wrong?
Since you didn't initialize the elements of vData, their lengths are all still zero. So your inner loop is never executed.
That did it! Thanks a lot. I feel illuminated :P
Mar 06 2007