Thread overview | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 15, 2016 ndslice: convert a sliced object to T[] | ||||
---|---|---|---|---|
| ||||
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 |
June 15, 2016 Re: ndslice: convert a sliced object to T[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to data pulverizer | 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 |
June 15, 2016 Re: ndslice: convert a sliced object to T[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Seb | 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!
|
June 15, 2016 Re: ndslice: convert a sliced object to T[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to data pulverizer | On Wednesday, 15 June 2016 at 03:11:23 UTC, data pulverizer wrote: > in that case: > > import std.array : array; > int[] x = slice.byElement.array; Are you sure you want to create a _copy_ of your data? In most cases you don't need that ;-) > thanks, now I can go to bed! You are welcome. Sleep tight! |
June 15, 2016 Re: ndslice: convert a sliced object to T[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Seb | On Wednesday, 15 June 2016 at 03:17:39 UTC, Seb wrote:
> On Wednesday, 15 June 2016 at 03:11:23 UTC, data pulverizer wrote:
>> in that case:
>>
>> import std.array : array;
>> int[] x = slice.byElement.array;
>
> Are you sure you want to create a _copy_ of your data? In most cases you don't need that ;-)
>
>> thanks, now I can go to bed!
>
> You are welcome. Sleep tight!
Thanks, I did.
I definitely don't want to create a copy! I thought .byElement would provide a range which I assume is a reference am I forcing it to copy by using .array?
|
June 15, 2016 Re: ndslice: convert a sliced object to T[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to data pulverizer | On Wednesday, 15 June 2016 at 07:24:23 UTC, data pulverizer wrote:
> On Wednesday, 15 June 2016 at 03:17:39 UTC, Seb wrote:
>> On Wednesday, 15 June 2016 at 03:11:23 UTC, data pulverizer wrote:
>>> in that case:
>>>
>>> import std.array : array;
>>> int[] x = slice.byElement.array;
>>
>> Are you sure you want to create a _copy_ of your data? In most cases you don't need that ;-)
>>
>>> thanks, now I can go to bed!
>>
>> You are welcome. Sleep tight!
>
> Thanks, I did.
>
> I definitely don't want to create a copy! I thought .byElement would provide a range which I assume is a reference am I forcing it to copy by using .array?
Yes. You're forcing it to read all elements and copy them in a new array.
|
June 15, 2016 Re: ndslice: convert a sliced object to T[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | On Wednesday, 15 June 2016 at 07:45:12 UTC, Andrea Fontana wrote:
> On Wednesday, 15 June 2016 at 07:24:23 UTC, data pulverizer wrote:
>> On Wednesday, 15 June 2016 at 03:17:39 UTC, Seb wrote:
>>> On Wednesday, 15 June 2016 at 03:11:23 UTC, data pulverizer wrote:
>>>> in that case:
>>>>
>>>> import std.array : array;
>>>> int[] x = slice.byElement.array;
>>>
>>> Are you sure you want to create a _copy_ of your data? In most cases you don't need that ;-)
>>>
>>>> thanks, now I can go to bed!
>>>
>>> You are welcome. Sleep tight!
>>
>> Thanks, I did.
>>
>> I definitely don't want to create a copy! I thought .byElement would provide a range which I assume is a reference am I forcing it to copy by using .array?
>
> Yes. You're forcing it to read all elements and copy them in a new array.
I guess foreach would not copy the elements? for example:
foreach(el; slice.byElement)
x ~= el;
But it feels wrong to be doing work pulling elements that already exists by using foreach. I feel as if I am missing something obvious but can't get it.
|
June 15, 2016 Re: ndslice: convert a sliced object to T[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to data pulverizer | On Wednesday, 15 June 2016 at 08:25:35 UTC, data pulverizer wrote:
> I guess foreach would not copy the elements? for example:
>
> foreach(el; slice.byElement)
> x ~= el;
>
> But it feels wrong to be doing work pulling elements that already exists by using foreach. I feel as if I am missing something obvious but can't get it.
The question is: why you need to put them inside an array? If you can, leave them in the lazy range and work on it.
|
June 15, 2016 Re: ndslice: convert a sliced object to T[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | On Wednesday, 15 June 2016 at 08:53:22 UTC, Andrea Fontana wrote:
> On Wednesday, 15 June 2016 at 08:25:35 UTC, data pulverizer wrote:
>> I guess foreach would not copy the elements? for example:
>>
>> foreach(el; slice.byElement)
>> x ~= el;
>>
>> But it feels wrong to be doing work pulling elements that already exists by using foreach. I feel as if I am missing something obvious but can't get it.
>
> The question is: why you need to put them inside an array? If you can, leave them in the lazy range and work on it.
I need this to work with external libraries that only deal with one dimensional arrays.
|
June 15, 2016 Re: ndslice: convert a sliced object to T[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to data pulverizer | On Wednesday, 15 June 2016 at 08:56:15 UTC, data pulverizer wrote:
> On Wednesday, 15 June 2016 at 08:53:22 UTC, Andrea Fontana wrote:
>> On Wednesday, 15 June 2016 at 08:25:35 UTC, data pulverizer wrote:
>>> I guess foreach would not copy the elements? for example:
>>>
>>> foreach(el; slice.byElement)
>>> x ~= el;
>>>
>>> But it feels wrong to be doing work pulling elements that already exists by using foreach. I feel as if I am missing something obvious but can't get it.
>>
>> The question is: why you need to put them inside an array? If you can, leave them in the lazy range and work on it.
>
> I need this to work with external libraries that only deal with one dimensional arrays.
Then I think the slice.byElement.array is the right solution.
|
Copyright © 1999-2021 by the D Language Foundation