www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - multidimensional array

reply "Joel" <joelcnz gmail.com> writes:
I'm trying to make a multidimensional array. I feel I've tried 
every thing. Is there a good guide explaining it?

struct Spot { bool dot; }
spots = new Spot[][](800,600);
	assert(spots[800-1][600-1].dot, "Out of bounds");
Sep 27 2014
next sibling parent reply "JKPdouble" <JKPdouble nowhere.uk> writes:
On Sunday, 28 September 2014 at 04:24:25 UTC, Joel wrote:
 I'm trying to make a multidimensional array. I feel I've tried 
 every thing. Is there a good guide explaining it?

 struct Spot { bool dot; }
 spots = new Spot[][](800,600);
 	assert(spots[800-1][600-1].dot, "Out of bounds");
dot is initialized to false, then the assertion fails. But your assertion message leads to think that there is a bound error, which is not the case, the assertion fails because you're expecting dot to be true: ---- import std.stdio; void main(string args[]) { struct Spot { bool dot; } auto spots = new Spot[][](800,600); writeln(spots); assert(!spots[800-1][600-1].dot, "Out of bounds"); } ---- passes without failure. Actually the array size is OK.
Sep 27 2014
parent reply "JKPdouble" <JKPdouble nowhere.uk> writes:
On Sunday, 28 September 2014 at 04:38:56 UTC, JKPdouble wrote:
 On Sunday, 28 September 2014 at 04:24:25 UTC, Joel wrote:
 I'm trying to make a multidimensional array. I feel I've tried 
 every thing. Is there a good guide explaining it?

 struct Spot { bool dot; }
 spots = new Spot[][](800,600);
 	assert(spots[800-1][600-1].dot, "Out of bounds");
dot is initialized to false, then the assertion fails. But your assertion message leads to think that there is a bound error, which is not the case, the assertion fails because you're expecting dot to be true: ---- import std.stdio; void main(string args[]) { struct Spot { bool dot; } auto spots = new Spot[][](800,600); writeln(spots); assert(!spots[800-1][600-1].dot, "Out of bounds"); } ---- passes without failure. Actually the array size is OK.
I meant: ---- void main(string args[]) { struct Spot { bool dot; } auto spots = new Spot[][](800,600); assert(!spots[800-1][600-1].dot, "element dot is true"); } ----
Sep 27 2014
parent "Joel" <joelcnz gmail.com> writes:
Thanks JKPdouble. I was hoping for a clear way to work 
multidimensional arrays out.
Sep 29 2014
prev sibling next sibling parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sun, 28 Sep 2014 04:24:19 +0000
Joel via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:

 struct Spot { bool dot; }
 spots =3D new Spot[][](800,600);
btw, does anybody know why i can do `new ubyte[256];` but not `new ubyte[256][256];`? hate that.
Sep 28 2014
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
On 28/09/2014 08:48, ketmar via Digitalmars-d-learn wrote:
 On Sun, 28 Sep 2014 04:24:19 +0000
 Joel via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:

 struct Spot { bool dot; }
 spots = new Spot[][](800,600);
btw, does anybody know why i can do `new ubyte[256];` but not `new ubyte[256][256];`? hate that.
You can do `new ubyte[256][256]`, if the destination type is a ubyte[256][]. The reason is that you are performing an allocation of the form `new T[n]`, which means allocate an array of n instances of type T. In this case, T is ubyte[256], which is a static array type. Stewart. -- My email address is valid but not my primary mailbox and not checked regularly. Please keep replies on the 'group where everybody may benefit.
Sep 28 2014
parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sun, 28 Sep 2014 22:33:40 +0100
Stewart Gordon via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:

 You can do `new ubyte[256][256]`, if the destination type is a
 ubyte[256][].  The reason is that you are performing an allocation of
 the form `new T[n]`, which means allocate an array of n instances of
 type T.  In this case, T is ubyte[256], which is a static array type.
it's completely counterintuitive. either `new ubyte[256];` should be disallowed, or `new ubyte[256][256];` should work as i expect it to work. it's the same thing as with const methods: `const A foo ()`. yes, 'const' is a method attribute here, but it's counterintuitive.
Sep 28 2014
prev sibling next sibling parent "Stefan Frijters" <sfrijters gmail.com> writes:
On Sunday, 28 September 2014 at 04:24:25 UTC, Joel wrote:
 I'm trying to make a multidimensional array. I feel I've tried 
 every thing. Is there a good guide explaining it?

 struct Spot { bool dot; }
 spots = new Spot[][](800,600);
 	assert(spots[800-1][600-1].dot, "Out of bounds");
You could also take a look at unstd.multidimarray (not my work, but I'm using it extensively at the moment)[1]. [1] http://denis-sh.bitbucket.org/unstandard/unstd.multidimarray.html
Sep 28 2014
prev sibling next sibling parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Sun, Sep 28, 2014 at 10:48:45AM +0300, ketmar via Digitalmars-d-learn wrote:
 On Sun, 28 Sep 2014 04:24:19 +0000
 Joel via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:
 
 struct Spot { bool dot; }
 spots = new Spot[][](800,600);
btw, does anybody know why i can do `new ubyte[256];` but not `new ubyte[256][256];`? hate that.
File a bug. T -- I see that you JS got Bach.
Sep 28 2014
prev sibling parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sun, 28 Sep 2014 07:40:23 -0700
"H. S. Teoh via Digitalmars-d-learn"
<digitalmars-d-learn puremagic.com> wrote:

 File a bug.
https://issues.dlang.org/show_bug.cgi?id=3D13556
Sep 28 2014