2-D array initialization
tastyminerals
tastyminerals at gmail.com
Tue Aug 4 19:56:39 UTC 2020
On Sunday, 2 August 2020 at 19:19:51 UTC, Andy Balba wrote:
> 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 ?
mir is a D package (akin to Python pip package). You can easily
include it into your program by adding at the top of your file
the following code:
/+ dub.sdl:
name "my_script"
dependency "mir-algorithm" version="~>3.9.12"
+/
And then just run your script with "dub my_script.d", dub will
fetch the necessary dependencies, compile and run the file.
However, it will not generate compiled versions of your
my_script.d for that, you better set a dub project. Here, see to
do it:
https://tastyminerals.github.io/tasty-blog/dlang/2020/03/01/how_to_use_external_libraries_in_d_project.html
More information about the Digitalmars-d-learn
mailing list