RFC on range design for D2
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Mon Sep 8 20:53:17 PDT 2008
Robert Jacques wrote:
> On Mon, 08 Sep 2008 20:37:41 -0400, Andrei Alexandrescu
> <SeeWebsiteForEmail at erdani.org> wrote:
>> Denis Koroskin wrote:
>>> 3) Walter mentioned that built-in array could be re-implemented using
>>> a pair of pointers instead of ptr+length. Will it ever get a green
>>> light? It fits range concept much better.
>>
>> Walter told me to first implement my design, and if it works, he'll do
>> the change. Yes, it does fit ranges much better because the often-used
>> next and, um, pop will only touch one word instead of two.
>
> I'd warn that changing away from ptr+length would create logical
> incosistencies between 1D arrays and 2D/3D/ND arrays.
How so?
>>> 4) We need some way of supporting dollar notation in user containers.
>>> The hack of using __dollar is bad (although it works).
>>
>> It doesn't work for multiple dimensions. There should be an
>> opDollar(uint dim) that gives the library information on which
>> argument count it occured in. Consider:
>>
>> auto x = matrix[$-1, $-1];
>>
>> Here the dollar's occurrences have different meanings. A good start
>> would be to expand the above into:
>>
>> auto x = matrix[matrix.opDollar(0)-1, matrix.opDollar(1)-1];
>
> I'd also add that multiple dimension slicing should be supported. i.e.
> auto x = matrix[2..5,0..$,3]
> would become
> auto x =
> matrix.opSlice(Slice!(size_t)(2,5),Slice!(size_t)(0,matrix.opDollar(0)),3)
> with
> struct Slice (T) { T start; T end; }
> Strided slices would also be nice. i.e. matrix[0..$:10] // decimate the
> array
Multidimensional slicing can be implemented with staggered indexing:
matrix[2..5][0..$][3]
means: first, take a slice 2..5 that returns a matrix range one
dimension smaller. Then, for that type take a slice from 0 to $. And so on.
This works great for row-wise storage. I'm not sure how efficient it
would be for other storage schemes.
Note how nice the distinction between the container and its views works:
there is only one matrix. But there are many ranges and subranges within
it, bearing various relationships with one another.
Andrei
More information about the Digitalmars-d-announce
mailing list