2-D array initialization

Andy Balba pwplus7 at gmail.com
Sun Aug 2 19:19:51 UTC 2020


On Sunday, 2 August 2020 at 06:37:06 UTC, tastyminerals wrote:

> You haven't said anything about efficiency because if you care 
> and your arrays are rather big, you better go with 
> https://github.com/libmir/mir-algorithm as mentioned above. It 
> might be a little finicky at the start but this post: 
> https://tastyminerals.github.io/tasty-blog/dlang/2020/03/22/multidimensional_arrays_in_d.html should get you up to speed.
>
>
> Keep in mind that std.array.staticArray is not efficient for  
> large arrays.
>
> If you want to stick to standard D, I would not initialize a 2D 
> array because it is just cumbersome but rather use a 1D array 
> and transform it into 2D view on demand via ".chunks" method. 
> Here is an example.
>
> import std.range;
> import std.array;
>
> void main() {
>     int[] arr = 20.iota.array;
>     auto arr2dView = arr.chunks(5);
> }
>
> Should give you
>
> ┌              ┐
> │ 0  1  2  3  4│
> │ 5  6  7  8  9│
> │10 11 12 13 14│
> │15 16 17 18 19│
> └              ┘
>
> whenever you need to access its elements as arr.chunks(5)[1][1 
> .. 3] --> [6, 7].

@ tastyminerals  Thanks for your help on this. These comments, 
combined with the others, are making my climb of the D learning 
curve much quicker.

I'm not a gitHub fan, but I like the mir functions; and it looks 
like I have to download mir before using it.
mir has quite a few .d files..Is there a quick way to download it 
?


More information about the Digitalmars-d-learn mailing list