Mir Slice.shape is not consistent with the actual array shape

Pavel Shkadzko p.shkadzko at gmail.com
Sun May 24 14:17:33 UTC 2020


I am confused by the return value of Mir shape.
Consider the following example.

///////////////////////////////////////////////////////////////////////////
import std.stdio;
import std.conv;
import std.array: array;
import std.range: chunks;
import mir.ndslice;

int[] getShape(T : int)(T obj, int[] dims = null)
{
     return dims;
}

// return arr shape
int[] getShape(T)(T obj, int[] dims = null)
{
     dims ~= obj.length.to!int;
     return getShape!(typeof(obj[0]))(obj[0], dims);
}

void main() {
     int[] arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 
15, 16];
     int[][][] a = arr.chunks(4).array.chunks(2).array;

     writeln(arr);
     writeln(arr.shape);

     auto arrSlice = arr.sliced;
     writeln(arrSlice);
     writeln(arrSlice.shape);

}
///////////////////////////////////////////////////////////////////////////

[[[1, 2, 3, 4], [5, 6, 7, 8]], [[9, 10, 11, 12], [13, 14, 15, 
16]]]
[2, 2, 4] <-- correct shape
[[[1, 2, 3, 4], [5, 6, 7, 8]], [[9, 10, 11, 12], [13, 14, 15, 
16]]]
[2] <-- which shape is that?

I would expect sliced to create a Slice with the same dims. Well, 
sliced returns a shell over the array, but why does it return its 
own shape instead of the shape of the array it provides view 
into? This makes it even more confusing once you print both 
representations.
What's the rationale here?


More information about the Digitalmars-d-learn mailing list