Impose structure on array

9il ilyayaroshenko at gmail.com
Mon May 27 03:11:12 UTC 2019


On Monday, 20 May 2019 at 12:09:02 UTC, Alex wrote:
> given some array, is there some way to easily impose structure 
> on that array at runtime?
>
> void* data;
>
> auto x = cast(byte[A,B,C])data;
>
> X is then an AxBxC matrix.
>
> I'm having to compute the index myself and it just seems 
> unnecessary. A and B are not known at compile time though.
>
> Obviously it should be just as efficient as computing the 
> offset manually.
>
> I could probably do this with a struct and override opIndex but 
> I'm wondering what is already out there. If it's slower or 
> consumes more memory than manual it's not worth it(since my 
> code already calcs the index).

Slightly updated version of the prev. example.
```
import mir.ndslice;
byte data[];
...
// canonical is `optional`
auto tensor = data.sliced(A, B, C).canonical;
...
byte elem = tensor[i, j, k];
auto matrix = tensor[i, j];
auto otherKindOfMatrix = tensor[0..$, i, j];
```

It is efficient as handwritten code.


More information about the Digitalmars-d-learn mailing list