digitalmars.D.learn - Impose structure on array
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).
May 20 2019
On Monday, 20 May 2019 at 12:09:02 UTC, Alex wrote:void* data; auto x = cast(byte[A,B,C])data; X is then an AxBxC matrix.It sounds like you're looking for ndslide from mir http://code.dlang.org/packages/mir-algorithm ``` ubyte data[]; auto x = x.sliced(A, B, C); ``` It should be pretty efficient, but I'm not certain whether it's as good as your hand-written code.
May 20 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.
May 26 2019