digitalmars.D.learn - Initialization of dynamic multidimensional array
- berni (4/8) Feb 05 2017 Is there anything better for this? I mean, the program will fill
- Daniel Kozak via Digitalmars-d-learn (2/10) Feb 05 2017
- berni (6/18) Feb 06 2017 Thanks for the link. Looks a little bit hacky... I hoped for
- Daniel Kozak via Digitalmars-d-learn (10/35) Feb 06 2017 One another way is use something like this:
- berni (4/6) Feb 07 2017 This looks like what I was looking for. At least I think I
- berni (3/11) Feb 10 2017 And it doesn't work anymore. I've no clue, why... Can you help me?
- Daniel Kozak via Digitalmars-d-learn (4/16) Feb 10 2017 Because it does not see VALUE, you need to use delegate insted of string...
- berni (3/13) Feb 10 2017 Unfortunately this leeds to the same error... (Or something very
- berni (3/17) Feb 10 2017 Opps, sorry. Taking back everything. It was a typo, that caused
- Mike Parker (13/18) Feb 10 2017 each is a template. As per the template documentation [1], your
- =?UTF-8?Q?Ali_=c3=87ehreli?= (35/43) Feb 06 2017 Here is something that returns the tail as dynamic array. (Otherwise,
- Ilya Yaroshenko (3/11) Feb 07 2017 You may want to used new ndslice for multidimensional algorithms
With X not known at compile time:auto arr = new int[][](X,X); for (int i=0;i<X;i++) for (int j=0;j<X;j++) arr[i][j] = -1;Is there anything better for this? I mean, the program will fill the array with zeroes, just to overwrite all of them with -1. That's wasted execution time and doesn't feel D-ish to me.
Feb 05 2017
Dne 5.2.2017 v 21:33 berni via Digitalmars-d-learn napsal(a):With X not known at compile time:auto arr = new int[][](X,X); for (int i=0;i<X;i++) for (int j=0;j<X;j++) arr[i][j] = -1;Is there anything better for this? I mean, the program will fill the array with zeroes, just to overwrite all of them with -1. That's wasted execution time and doesn't feel D-ish to me.
Feb 05 2017
On Sunday, 5 February 2017 at 21:14:33 UTC, Daniel Kozak wrote:Dne 5.2.2017 v 21:33 berni via Digitalmars-d-learn napsal(a):Thanks for the link. Looks a little bit hacky... I hoped for something more straightforward, or at least, if direct initialisation is not possible something like "arr[][] = -1;" or "arr[0..X][0..X] = -1;" But as far as I can see now, this does not exist.With X not known at compile time:auto arr = new int[][](X,X); for (int i=0;i<X;i++) for (int j=0;j<X;j++) arr[i][j] = -1;Is there anything better for this? I mean, the program will fill the array with zeroes, just to overwrite all of them with -1. That's wasted execution time and doesn't feel D-ish to me.
Feb 06 2017
One another way is use something like this: import std.array, std.algorithm, std.stdio; auto arr =3D uninitializedArray!(int[][])(ROWS,COLS); arr.each!"a[]=3D-1"; writeln(arr); Dne 6. 2. 2017 8:21 PM napsal u=C5=BEivatel "berni via Digitalmars-d-learn"= < digitalmars-d-learn puremagic.com>:On Sunday, 5 February 2017 at 21:14:33 UTC, Daniel Kozak wrote:lehttp://stackoverflow.com/questions/24600796/d-set-default- value-for-a-struct-member-which-is-a-multidimensional- Dne 5.2.2017 v 21:33 berni via Digitalmars-d-learn napsal(a):Thanks for the link. Looks a little bit hacky... I hoped for something more straightforward, or at least, if direct initialisation is not possib=With X not known at compile time: auto arr =3D new int[][](X,X);for (int i=3D0;i<X;i++) for (int j=3D0;j<X;j++) arr[i][j] =3D -1;Is there anything better for this? I mean, the program will fill the array with zeroes, just to overwrite all of them with -1. That's wasted execution time and doesn't feel D-ish to me.something like "arr[][] =3D -1;" or "arr[0..X][0..X] =3D -1;" But as far =as Ican see now, this does not exist.
Feb 06 2017
auto arr = uninitializedArray!(int[][])(ROWS,COLS); arr.each!"a[]=-1";This looks like what I was looking for. At least I think I understand what's going on here. The other two suggestions are beyond my scope yet, but I'll come back, when I improved on my D skills. Thanks for your replies.
Feb 07 2017
On Tuesday, 7 February 2017 at 19:06:22 UTC, berni wrote:Now I tried this with a named instead of a magic constant e.g.auto arr = uninitializedArray!(int[][])(ROWS,COLS); arr.each!"a[]=-1";This looks like what I was looking for. At least I think I understand what's going on here. The other two suggestions are beyond my scope yet, but I'll come back, when I improved on my D skills. Thanks for your replies.immutable VALUE=-1; arr.each!"a[]=VALUE";And it doesn't work anymore. I've no clue, why... Can you help me?
Feb 10 2017
Dne 10.2.2017 v 10:03 berni via Digitalmars-d-learn napsal(a):On Tuesday, 7 February 2017 at 19:06:22 UTC, berni wrote:Because it does not see VALUE, you need to use delegate insted of string something like this: arr.each!(a=>a[]=VALUE);Now I tried this with a named instead of a magic constant e.g.auto arr = uninitializedArray!(int[][])(ROWS,COLS); arr.each!"a[]=-1";This looks like what I was looking for. At least I think I understand what's going on here. The other two suggestions are beyond my scope yet, but I'll come back, when I improved on my D skills. Thanks for your replies.immutable VALUE=-1; arr.each!"a[]=VALUE";And it doesn't work anymore. I've no clue, why... Can you help me?
Feb 10 2017
On Friday, 10 February 2017 at 09:25:04 UTC, Daniel Kozak wrote:Unfortunately this leeds to the same error... (Or something very simmilar.)Now I tried this with a named instead of a magic constant e.g.Because it does not see VALUE, you need to use delegate insted of string something like this: arr.each!(a=>a[]=VALUE);immutable VALUE=-1; arr.each!"a[]=VALUE";And it doesn't work anymore. I've no clue, why... Can you help me?
Feb 10 2017
On Friday, 10 February 2017 at 09:34:39 UTC, berni wrote:On Friday, 10 February 2017 at 09:25:04 UTC, Daniel Kozak wrote:Opps, sorry. Taking back everything. It was a typo, that caused the error.Unfortunately this leeds to the same error... (Or something very simmilar.)Now I tried this with a named instead of a magic constant e.g.Because it does not see VALUE, you need to use delegate insted of string something like this: arr.each!(a=>a[]=VALUE);immutable VALUE=-1; arr.each!"a[]=VALUE";And it doesn't work anymore. I've no clue, why... Can you help me?
Feb 10 2017
On Friday, 10 February 2017 at 09:03:16 UTC, berni wrote:Now I tried this with a named instead of a magic constant e.g.each is a template. As per the template documentation [1], your instantiation of each knows nothing about VALUE, because VALUE declared in the scope in which the template is instantiated. Template instantiations only have the scope in which they are implemented, not where they are instantiated, so here each cannot see VALUE. On an side note (unrelated to your error), when declaring constants that are intended to be symbolic (i.e. you never need to take their address), it's more idiomatic to use manifest constants (via enum) rather than immutable. enum value = -1; [1] https://dlang.org/spec/template.html#instantiation_scopeimmutable VALUE=-1; arr.each!"a[]=VALUE";And it doesn't work anymore. I've no clue, why... Can you help me?
Feb 10 2017
On 02/05/2017 12:33 PM, berni wrote:With X not known at compile time:Here is something that returns the tail as dynamic array. (Otherwise, being a value type, the whole static array would be copied out of the function): template makeMulDim(dims...) { template MultDimType(T, dims...) { static if (dims.length == 0) { alias MultDimType = T; } else static if (dims.length == 1) { alias MultDimType = T[]; } else { alias MultDimType = MultDimType!(T[dims[0]], dims[1..$]); } } auto makeMulDim(T)(T initValue) { import std.algorithm : fold; const elementCount = [ dims ].fold!((a, b) => a * b); auto storage = new T[](elementCount); storage[] = initValue; return cast(MultDimType!(T, dims))storage; } } unittest { auto arr = makeMulDim!(2, 3, 4)(1.1); static assert (is (typeof(arr) == double[2][3][])); } void main() { import std.stdio; auto arr = makeMulDim!(2, 3, 4)(-1); pragma(msg, typeof(arr)); arr[3][2][1] = 42; writefln("%(%s\n%)", arr); } So, the returned type in main is int[2][3][]. Aliauto arr = new int[][](X,X); for (int i=0;i<X;i++) for (int j=0;j<X;j++) arr[i][j] = -1;Is there anything better for this? I mean, the program will fill the array with zeroes, just to overwrite all of them with -1. That's wasted execution time and doesn't feel D-ish to me.
Feb 06 2017
On Sunday, 5 February 2017 at 20:33:06 UTC, berni wrote:With X not known at compile time:You may want to used new ndslice for multidimensional algorithms http://docs.algorithm.dlang.io/latest/mir_ndslice.htmlauto arr = new int[][](X,X); for (int i=0;i<X;i++) for (int j=0;j<X;j++) arr[i][j] = -1;Is there anything better for this? I mean, the program will fill the array with zeroes, just to overwrite all of them with -1. That's wasted execution time and doesn't feel D-ish to me.
Feb 07 2017