www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - multi-dimensional arrays, not arrays of arrays

reply XavierAP <n3minis-git yahoo.es> writes:
Does D provide anything like this? Otherwise, was this ever 
considered and were reasons found not to have it?

I mean at least in C# (not talking about C/C++ at all) you can 
declare two kind of multi-dimensional arrays: T[][][] or T[,,]. 
The first is the same as the D ones, array of arrays of... 
(except the order of the indices isn't inverted in C# type 
declarations IIRC); but the second provides contiguous, 
rectangular truly multi-dimensional arrays supposedly with one 
single level of indirection. (Of course static arrays don't exist 
in C# just dynamic but that's an apart issue.)

Is there a reason why this feature would not be really desirable 
or possible? Does it really exist and I missed it in Andrei's 
book? As a matter of fact he states in chapter 4.3:

«On the minus side, "tall and thin" arrays with many rows and few 
columns incur a large size overhead as there's one array to keep 
per column. [...] Jagged arrays may have problems with efciency 
of access and cache friendliness. Each element access requires 
two indirections [...] going column-wise through a jagged array 
is a cache miss bonanza.»

It would look that well implemented [,,] arrays would fill some 
application gaps, even though arrays of arrays (jagged in 
practice or not) are elsewhere useful as well of course. In the 
former cases I would think about defining my own struct wrapping 
a flat array with an n-dimensional api such as return _buff[i*m + 
j]... but generic in type and maybe also in dimension.

Again any special reason why this isn't already provided in the 
language? Either a generic struct/class like that, or something 
deeper in the language that allows using the [] operator as 
transparently as with any other arrays (as in C#).
Thanks for any input.
Feb 18
parent reply Ilya Yaroshenko <ilyayaroshenko gmail.com> writes:
On Saturday, 18 February 2017 at 10:37:21 UTC, XavierAP wrote:
 Does D provide anything like this? Otherwise, was this ever 
 considered and were reasons found not to have it?
They are implemented as part of the Mir project. We call them ndslices. https://github.com/libmir/mir-algorithm Docs: http://docs.algorithm.dlang.io/ See also other Mir projects at https://github.com/libmir. std.experimental.ndslice is a deprecated version of mir.ndslice. std.experimental.ndslice provides only numpy like tensors. mir.ndslice provides all kinds of tensors. Sparse tensors can be found at https://github.com/libmir/mir Best, Ilya
Feb 18
parent XavierAP <n3minis-git yahoo.es> writes:
Nice, thanks! Will check it out
Feb 18