Trying to understand multidimensional arrays in D
H. S. Teoh
hsteoh at quickfur.ath.cx
Tue Dec 22 05:43:48 UTC 2020
On Tue, Dec 22, 2020 at 04:47:13AM +0000, Rekel via Digitalmars-d-learn wrote:
[...]
> Defending array-notation by giving an example of explicitly not using
> declared aliases makes no sense to me.
> When I define 2d arrays, or index them, I think in row -> column terms
> (often math notation for matrix size being; 'rows x columns'), or more
> generally in big -> small terms, which is clear when using the
> consistent left->right notation, big picture first followed by detail,
> honestly the whole concept of encapsulation;
If you really want multidimensional arrays, i.e., arrays that logically
range over an n-dimensional space of indices, not just arrays of arrays
(which is what the array[][]... syntax gives you), I highly recommend
designing your own array type using D's multidimensional array overload
mechanism:
https://dlang.org/spec/operatoroverloading.html#array-ops
This link gives only a 2D example, but it can be generalized to
arbitrary dimensions. With the proper overloads, you can do
vertical/horizontal slices, subarrays, etc., all with a consistent
syntax:
arr[1, x..y]
arr[i..j, 5]
arr[2..3, 4..6]
See the ndslice package in the mir library for an actual such
implementation.
T
--
Why did the mathematician reinvent the square wheel?
Because he wanted to drive smoothly over an inverted catenary road.
More information about the Digitalmars-d-learn
mailing list