Formal Review of std.range.ndslice

Ilya via Digitalmars-d digitalmars-d at puremagic.com
Fri Dec 11 14:56:15 PST 2015


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)]);
           ...
    }

> where p is of type size_t[N].
>
> I tried using ndarray in conjunction with the 
> std.experimental.allocator, but I don't particularly care about 
> memory management;
> these are large arrays that are allocated once and kept around 
> for the duration of the program.
>
> Any help would be appreciated.
>
> [1] https://github.com/SFrijters/DLBC
> [2] https://bitbucket.org/SFrijters/unstandard

See also updated docs: 
http://dtest.thecybershadow.net/artifact/website-13cbdcf17d84fc31328c3f517a56bea783c418d6-d9c63e815273f0906309088334e7dfb1/web/phobos-prerelease/std_experimental_ndslice.html

Ilya


More information about the Digitalmars-d mailing list