Re: to! converting 1D to 2D array

2014-03-22 Thread Marc Schütz
On Friday, 14 March 2014 at 19:24:21 UTC, Chris Williams wrote: In D, an array is a struct (struct Array), with an address and a length value. A multi-dimensional array is an Array with an address pointing to an array of Arrays. So with an int[2][2] array, you have a layout like: @1000 Array(

Re: to! converting 1D to 2D array

2014-03-14 Thread ed
On Friday, 14 March 2014 at 19:24:21 UTC, Chris Williams wrote: It looks like you might be right after all about the code being invalid D. It could be a bug when it compiles without the cast. I filed a bug report about it, see where it leads :D Thanks, ed

Re: to! converting 1D to 2D array

2014-03-14 Thread ed
On Friday, 14 March 2014 at 19:24:21 UTC, Chris Williams wrote: [snip] address pointing to an array of Arrays. So with an int[2][2] array, you have a layout like: @1000 Array(address=1016, length=2) @1016 [Array(address=1048, length=2),Array(address=1056, length=2)] @1048 [1,2] @1056 [3,4]

Re: to! converting 1D to 2D array

2014-03-14 Thread Chris Williams
On Friday, 14 March 2014 at 04:36:27 UTC, ed wrote: As to whether or not this should work: int[4] a=[1,2,3,4]; int[2][2] b; b=a; is up to the D language gurus. I think it should... but I'm no language developer, there may be other side-effects I haven't thought about. Cheers, ed In C, any

Re: to! converting 1D to 2D array

2014-03-13 Thread ed
On Friday, 14 March 2014 at 04:18:18 UTC, ed wrote: On Thursday, 13 March 2014 at 18:17:03 UTC, Chris Williams wrote: On Thursday, 13 March 2014 at 03:31:09 UTC, ed wrote: On Thursday, 13 March 2014 at 00:15:19 UTC, Chris Williams wrote: [snip] It shouldn't and probably isn't working. It is

Re: to! converting 1D to 2D array

2014-03-13 Thread ed
On Thursday, 13 March 2014 at 18:17:03 UTC, Chris Williams wrote: On Thursday, 13 March 2014 at 03:31:09 UTC, ed wrote: On Thursday, 13 March 2014 at 00:15:19 UTC, Chris Williams wrote: [snip] It shouldn't and probably isn't working. It is working and in fact it is in a "const pure @safe" f

Re: to! converting 1D to 2D array

2014-03-13 Thread Marc Schütz
On Wednesday, 12 March 2014 at 06:53:19 UTC, monarch_dodra wrote: I *believe* it's really just that you are allowed to *initialize* a static array (of any depth) from a dynamic array. However, for assignment, it doesn't work that way: int[] a = [0, 1, 2, 3]; int[2][1][2][1] b = a; //OK

Re: to! converting 1D to 2D array

2014-03-13 Thread Chris Williams
On Thursday, 13 March 2014 at 03:31:09 UTC, ed wrote: On Thursday, 13 March 2014 at 00:15:19 UTC, Chris Williams wrote: [snip] It shouldn't and probably isn't working. It is working and in fact it is in a "const pure @safe" function. So I will trust it :-) Well it's like a broken watch be

Re: to! converting 1D to 2D array

2014-03-12 Thread ed
On Wednesday, 12 March 2014 at 07:43:46 UTC, bearophile wrote: monarch_dodra: int[] a = [0, 1, 2, 3]; int[2][1][2][1] b; *cast(int[4]*)&b = a; assert(b == 0, 1]], [[2, 3); Those pointers are not needed: cast(int[4])b = a; Bye, bearoophile Thanks for this, the casts wor

Re: to! converting 1D to 2D array

2014-03-12 Thread ed
On Thursday, 13 March 2014 at 00:15:19 UTC, Chris Williams wrote: [snip] It shouldn't and probably isn't working. It is working and in fact it is in a "const pure @safe" function. So I will trust it :-) If nothing else, when you use to!(x)(y), "x" should be the type that you're trying to c

Re: to! converting 1D to 2D array

2014-03-12 Thread Chris Williams
On Wednesday, 12 March 2014 at 03:37:49 UTC, ed wrote: My understanding of your explanation is that it shouldn't work. It shouldn't and probably isn't working. If nothing else, when you use to!(x)(y), "x" should be the type that you're trying to convert into. So I would expect your code to be

Re: to! converting 1D to 2D array

2014-03-12 Thread bearophile
monarch_dodra: int[] a = [0, 1, 2, 3]; int[2][1][2][1] b; *cast(int[4]*)&b = a; assert(b == 0, 1]], [[2, 3); Those pointers are not needed: cast(int[4])b = a; Bye, bearoophile

Re: to! converting 1D to 2D array

2014-03-12 Thread monarch_dodra
On Wednesday, 12 March 2014 at 07:43:46 UTC, bearophile wrote: monarch_dodra: int[] a = [0, 1, 2, 3]; int[2][1][2][1] b; *cast(int[4]*)&b = a; assert(b == 0, 1]], [[2, 3); Those pointers are not needed: cast(int[4])b = a; Bye, bearoophile I'm *never* actually sure if t

Re: to! converting 1D to 2D array

2014-03-11 Thread monarch_dodra
On Wednesday, 12 March 2014 at 03:37:49 UTC, ed wrote: Thanks for explaining this, it makes sense what you said. But I'm still not sure why my original Example 1 worked. ~~~ // This works OK and converts long[4] to int[] then implicitly to int[2][2] long[4] a=[1,2,3,4]; int[2][2] b = to!(int[

Re: to! converting 1D to 2D array

2014-03-11 Thread ed
On Wednesday, 12 March 2014 at 02:14:45 UTC, bearophile wrote: ed: I am trying to convert a 1D array to a 2D array If you have a dynamic array (1D), you can convert it to a dynamic array of dynamic arrays (2D) using chunks: void main() { import std.stdio, std.range, std.algorithm;

Re: to! converting 1D to 2D array

2014-03-11 Thread bearophile
ed: I am trying to convert a 1D array to a 2D array If you have a dynamic array (1D), you can convert it to a dynamic array of dynamic arrays (2D) using chunks: void main() { import std.stdio, std.range, std.algorithm; int[] a = [1, 2, 3, 4, 5, 6]; int[][] b = a.chunks(2).arra