ndslice: convert a sliced object to T[]

data pulverizer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 14 20:11:23 PDT 2016


On Wednesday, 15 June 2016 at 02:50:30 UTC, Seb wrote:
> On Wednesday, 15 June 2016 at 02:43:37 UTC, data pulverizer 
> wrote:
>> How do I unravel a sliced item T[].sliced(...) to an array T[]?
>>
>> For instance:
>>
>> import std.experimental.ndslice;
>> auto slice = new int[12].sliced(3, 4);
>> int[] x = ??;
>>
>> Thanks
>
> A slice is just a _view_ on your memory, the easiest way is to 
> save a reference to your array like this:
>
> ```
> int[] arr = new int[12];
> auto slice = arr.sliced(3, 4);
> slice[1, 1] = 42;
> arr // [0, 0, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0]
> ```
>
> For a general case, you should give `byElement` a try:
>
> https://dlang.org/phobos/std_experimental_ndslice_selection.html#byElement

in that case:

import std.array : array;
int[] x = slice.byElement.array;

thanks, now I can go to bed!


More information about the Digitalmars-d-learn mailing list