Re: Disjoint slices of an array as reference

2020-08-20 Thread data pulverizer via Digitalmars-d-learn
On Thursday, 20 August 2020 at 08:26:59 UTC, Ali Çehreli wrote: On 8/19/20 9:11 PM, data pulverizer wrote: Thanks. I might go for a design like this: ``` struct View(T){   T* data;   long[2][] ranges; } ``` [...] I implemented the same idea recently; it's a fun exercise. :) I didn't

Re: Disjoint slices of an array as reference

2020-08-20 Thread Ali Çehreli via Digitalmars-d-learn
On 8/19/20 9:11 PM, data pulverizer wrote: On Thursday, 20 August 2020 at 03:47:15 UTC, Paul Backus wrote: double[][] y; y ~= x[0..5]; Thanks. I might go for a design like this: ``` struct View(T){   T* data;   long[2][] ranges; } ``` The ranges are were the slices are stored and T* (maybe

Re: Disjoint slices of an array as reference

2020-08-20 Thread Ali Çehreli via Digitalmars-d-learn
On 8/19/20 7:40 PM, data pulverizer wrote: > An array in D is either two pointers or one pointer and a length (I > don't know which) It is the length, followed by the pointer, equivalent of the following struct: struct A { size_t length_; void * ptr; size_t length() { return

Re: Disjoint slices of an array as reference

2020-08-19 Thread data pulverizer via Digitalmars-d-learn
On Thursday, 20 August 2020 at 03:47:15 UTC, Paul Backus wrote: double[][] y; y ~= x[0..5]; Thanks. I might go for a design like this: ``` struct View(T){ T* data; long[2][] ranges; } ``` The ranges are were the slices are stored and T* (maybe even immutable(T*)) is a pointer is to the

Re: Disjoint slices of an array as reference

2020-08-19 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 20 August 2020 at 02:21:15 UTC, data pulverizer wrote: ``` double[] y; y ~= x[0..5]; y ~= x[9..14]; ``` But the act of appending results in array copying - breaks the reference with the original array. The only other thing I have considered is creating an array of references to

Re: Disjoint slices of an array as reference

2020-08-19 Thread data pulverizer via Digitalmars-d-learn
On Thursday, 20 August 2020 at 02:38:33 UTC, data pulverizer wrote: I've been thinking about this some more and I don't think it is possible. An array in D is effectively two pointers either side of a memory block. When you create a slice you are creating another array two pointers somewhere

Re: Disjoint slices of an array as reference

2020-08-19 Thread data pulverizer via Digitalmars-d-learn
On Thursday, 20 August 2020 at 02:21:15 UTC, data pulverizer wrote: However I would like to have disjoint slices, something like this: ``` auto y = x[0..5, 9..14]; ``` I've been thinking about this some more and I don't think it is possible. An array in D is effectively two pointers either

Disjoint slices of an array as reference

2020-08-19 Thread data pulverizer via Digitalmars-d-learn
I have been trying to create a new array from an existing array that is effectively a view on the original array. This can be done with slices in D: ``` auto x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; auto y = [0..5]; ``` y a subset of the x array. If I