On 4/18/18 3:15 AM, Simen Kjærås wrote:
On Wednesday, 18 April 2018 at 06:54:29 UTC, Chris Katko wrote:
I need to rotate an array by 90 degrees, or have writefln figure that out.

I need, say:

0 4 5 6
0 0 0 0
0 0 0 0
0 0 0 0

But it's outputting:

0 0 0 0
4 0 0 0
5 0 0 0
6 0 0 0

int [4][4] data;
file.writeln(format("%(%-(%d %)\n%)", data));

Generally, the solution would be std.range.transposed. However, since you're using a int[4][4], that's not a range-of-ranges, and transposed don't work out of the box. This helper function should help:

T[][] ror(T, size_t N1, size_t N2)(ref T[N1][N2] arr)
{
     T[][] result = new T[][N2];
     foreach (i, e; arr) {
         result[i] = e.dup;
     }
     return result;
}

unittest
{
     import std.stdio;
     import std.range;

     int [4][4] data;
     data[2][3] = 4;
     writefln("%(%-(%d %)\n%)", data);
     writefln("%(%-(%d %)\n%)", data.ror.transposed);
}


A version without allocating:

T[][N2] ror(T, size_t N1, size_t N2)(ref T[N1][N2] arr)
{
    T[][N2] result;
    foreach (i, ref e; arr) {
        result[i] = e[];
    }
    return result;
}

...

writefln("%(%-(%d %)\n%)" data.ror[].transposed); // need the slice operator here

Keep in mind, you can't simply assign a variable to data.ror[], as the backing goes away immediately (OK to use as an rvalue though). And you must keep data in scope as long as you are using the result of data.ror.

-Steve

Reply via email to