www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Initialising multidimensional dynamic arrays

reply "Mike James" <foo bar.com> writes:
Hi,

How do I initialise a dynamic array of dynamic arrays?

struct MyData {
   SysTime stamp;
   short[] data;

   this(size_t size) {
     data = new short[size];
   }
}

MyDataArray mda;

how to initialise mda?

mda = new MyDataArray ?

Thanks.

Regards, -=mike=-
Sep 30 2014
next sibling parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Tue, 30 Sep 2014 15:57:57 +0000
Mike James via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 How do I initialise a dynamic array of dynamic arrays?
do you mean something like this: `int[][] a`? if yes, do this: auto a =3D new int[][](42, 69); and you'll get `int[42][69] a`. heh, people again confused by `new Type[amount]` syntax. that is concrete sign that this syntax will live forever.
Sep 30 2014
parent reply "Mike James" <foo bar.com> writes:
On Tuesday, 30 September 2014 at 16:07:28 UTC, ketmar via 
Digitalmars-d-learn wrote:
 On Tue, 30 Sep 2014 15:57:57 +0000
 Mike James via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com>
 wrote:

 How do I initialise a dynamic array of dynamic arrays?
do you mean something like this: `int[][] a`? if yes, do this: auto a = new int[][](42, 69); and you'll get `int[42][69] a`. heh, people again confused by `new Type[amount]` syntax. that is concrete sign that this syntax will live forever.
Thanks ketmar, You'll notice that it's actually a dynamic array of structs containing dynamic arrays - does this change your initializing? Regards, -=mike=-
Sep 30 2014
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 9/30/14 12:40 PM, Mike James wrote:
 On Tuesday, 30 September 2014 at 16:07:28 UTC, ketmar via
 Digitalmars-d-learn wrote:
   auto a = new int[][](42, 69);
...
 You'll notice that it's actually a dynamic array of structs containing
 dynamic arrays - does this change your initializing?
That is what his code does. -Steve
Sep 30 2014
parent reply "Mike James" <foo bar.com> writes:
On Tuesday, 30 September 2014 at 17:22:32 UTC, Steven 
Schveighoffer wrote:
 On 9/30/14 12:40 PM, Mike James wrote:
 On Tuesday, 30 September 2014 at 16:07:28 UTC, ketmar via
 Digitalmars-d-learn wrote:
  auto a = new int[][](42, 69);
...
 You'll notice that it's actually a dynamic array of structs 
 containing
 dynamic arrays - does this change your initializing?
That is what his code does. -Steve
Hi Steve, It's true that his code initialises an array of arrays - but my array is an array of structs containing a dynamic array. Regards, -=mike=-
Oct 01 2014
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 10/1/14 3:13 AM, Mike James wrote:
 Hi Steve,

 It's true that his code initialises an array of arrays - but my array is
 an array of structs containing a dynamic array.

 Regards, -=mike=-
Ah, ok. There is no trivial way to do it. Unlike C++, struct default ctors cannot be overridden. I see your question has been answered, but I would just tweak it a bit: foreach(ref m; mda) m.data = new short[y]; Only reason for that is, setting length will call some specialized function which eventually ends up doing exactly the same thing. The above is clearer and more correct IMO. -Steve
Oct 01 2014
prev sibling parent reply "Mike James" <foo bar.com> writes:
On Tuesday, 30 September 2014 at 15:57:58 UTC, Mike James wrote:
 Hi,

 How do I initialise a dynamic array of dynamic arrays?

 struct MyData {
   SysTime stamp;
   short[] data;

   this(size_t size) {
     data = new short[size];
   }
 }

 MyDataArray mda;

 how to initialise mda?

 mda = new MyDataArray ?

 Thanks.

 Regards, -=mike=-
I think I've found a way... struct MyData { SysTime stamp; short[] data; this(size_t size) { data = new short[size]; } } MyDataArray[] mda; <--- sorry, missing the []s in the original question... so in the constructor... this(size_t x, size_t y) { mda = new MyDataArray[](x); foreach(n, _; mda) mda[n].data.length = y; } Is there a simpler way? Regards, -=mike=-
Oct 01 2014
parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Wed, 01 Oct 2014 07:45:48 +0000
Mike James via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 so in the constructor...
=20
 this(size_t x, size_t y) {
    mda =3D new MyDataArray[](x);
          foreach(n, _; mda) mda[n].data.length =3D y;
 }
=20
 Is there a simpler way?
sorry, but no. btw, if MyDataArray is struct, you should do this: foreach (ref m; mda) m.data.length =3D y; or even this: foreach (ref m; mda =3D new MyDataArray[](x)) m.data.length =3D x; the thing is that without 'ref' you operates on the local copy, not on the real array element.
Oct 01 2014
parent "Mike James" <foo bar.com> writes:
On Wednesday, 1 October 2014 at 08:08:06 UTC, ketmar via 
Digitalmars-d-learn wrote:
 On Wed, 01 Oct 2014 07:45:48 +0000
 Mike James via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com>
 wrote:

 so in the constructor...
 
 this(size_t x, size_t y) {
    mda = new MyDataArray[](x);
          foreach(n, _; mda) mda[n].data.length = y;
 }
 
 Is there a simpler way?
sorry, but no. btw, if MyDataArray is struct, you should do this: foreach (ref m; mda) m.data.length = y; or even this: foreach (ref m; mda = new MyDataArray[](x)) m.data.length = x; the thing is that without 'ref' you operates on the local copy, not on the real array element.
Thanks ketmar, that did the trick. Regards, -=mike=-
Oct 01 2014