Formal Review of std.range.ndslice
Ilya via Digitalmars-d
digitalmars-d at puremagic.com
Fri Dec 11 15:29:03 PST 2015
On Friday, 11 December 2015 at 22:56:15 UTC, Ilya wrote:
> On Friday, 11 December 2015 at 19:31:14 UTC, Stefan Frijters
> wrote:
>> Today I've made an abortive attempt at replacing my code's [1]
>> dependence on unstd.multidimarray [2] with ndslice.
>> I'm guessing it's just me being stupid, but could anyone
>> supply with some hints on how to do the conversion with a
>> minimum of fuss?
>>
>> Basically I have an N-dimensional array (N is known at compile
>> time) of type T wrapped in a struct to carry some additional
>> information.
>> I then address it using size_t[N] fixed-size arrays, and loop
>> over it a lot with foreach, which also uses size_t[N] as index.
>>
>> So it looks something like this:
>>
>> struct Field(T, uint N) {
>>
>> alias arr this;
>>
>> MultidimArray!(T, N) arr; // is there any way to supply the
>> correct type here with ndslice? I cannot use auto, right?
>
> Slice!(N, T*) arr;
>
>>
>> this (in size_t[N] lengths) {
>> arr = multidimArray!T(lengths);
>
> // compute length
> // more flexible construtors would be added after
> // allocatrs support for ndslice
> size_t len = 1;
> foreach(l; lengths)
> len *= l;
>
> arr = new T[len].sliced(lengths);
>
>> }
>> }
>> and then things like
>>
>> foreach(immutable p, ref pop; someField) {
>> pop = foo(someOtherField[bar(p)]);
>> ...
>> }
>
> std.experimental.ndslice.selection: indexSlice, byElement;
>
> foreach(p; someField.shape.indexSlice.byElement) {
> someField[p] = foo(someOtherField[bar(p)]);
> ...
> }
faster version:
std.experimental.ndslice.selection: byElement;
for(auto r = someField.arr.byEleemnt; r.popFront) {
r.front = foo(someOtherField[bar(r.index)]);
...
}
>> where p is of type size_t[N].
Ilya
More information about the Digitalmars-d
mailing list