www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - .dup with twodimensional arrays

reply berni <someone somemail.de> writes:
I need code, that generates a copy of a twodimensional array.

What I do is:

 auto tmp = new int[][](X,X);
 foreach (i; 0..X) tmp[i] = solution[i].dup;
 
 solutions ~= tmp;
because solutions ~= solution.dup obviously doesn't work (the refs are copied, not the elements of the inner arrays). Is there a better solution without this extraneous tmp variable? Im thinking of something like
 solutions ~= solution.nice_phobos_function_id_dont_know.dup;
or something similar?
Mar 21 2018
next sibling parent berni <someone somemail.de> writes:
Oops, sorry. I just have seen, that I posted in the wrong forum, 
should have been in "New users Learn". Is it possible to move 
this post over?
Mar 21 2018
prev sibling next sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 3/21/18 5:59 AM, berni wrote:
 I need code, that generates a copy of a twodimensional array.
 
 What I do is:
 
 auto tmp = new int[][](X,X);
 foreach (i; 0..X) tmp[i] = solution[i].dup;

 solutions ~= tmp;
because solutions ~= solution.dup obviously doesn't work (the refs are copied, not the elements of the inner arrays). Is there a better solution without this extraneous tmp variable? Im thinking of something like
 solutions ~= solution.nice_phobos_function_id_dont_know.dup;
or something similar?
I don't think there is, but you could potentially use map and array: import std.algorithm: map; import std.array: array; solutions ~= tmp .map!(a => a.dup) // every access to an element dups it first .array; // build an array out of the result I'm not 100% sure array only calls front once per element, but I'm pretty sure. On 3/21/18 6:01 AM, berni wrote:
 Oops, sorry. I just have seen, that I posted in the wrong forum, should
 have been in "New users Learn". Is it possible to move this post over?
Sorry, posts can't be moved, but no big deal :) -Steve
Mar 21 2018
parent berni <someone somemail.de> writes:
On Wednesday, 21 March 2018 at 10:26:03 UTC, Steven Schveighoffer 
wrote:
 import std.algorithm: map;
 import std.array: array;

 solutions ~= tmp
    .map!(a => a.dup) // every access to an element dups it first
    .array;           // build an array out of the result
Oh, thanks, that's already better. (And with ldc it's much faster, saves about half of the time).
Mar 21 2018
prev sibling parent Petar Kirov [ZombineDev] <petar.p.kirov gmail.com> writes:
On Wednesday, 21 March 2018 at 09:59:52 UTC, berni wrote:
 I need code, that generates a copy of a twodimensional array.

 What I do is:

 auto tmp = new int[][](X,X);
 foreach (i; 0..X) tmp[i] = solution[i].dup;
 
 solutions ~= tmp;
because solutions ~= solution.dup obviously doesn't work (the refs are copied, not the elements of the inner arrays). Is there a better solution without this extraneous tmp variable? Im thinking of something like
 solutions ~= solution.nice_phobos_function_id_dont_know.dup;
or something similar?
If you happen to need more extensive utilities for multi-dimensional arrays, I strongly recommend taking a look at https://github.com/libmir/mir-algorithm. For example, for the general case of creaing an N-dimensional array, I think this is the function you need: http://docs.algorithm.dlang.io/latest/mir_ndslice_allocation.html#makeNdarray. Before, part of this functionality of libmir was part of the standard library, but due to the explosive growth of the package it was deemed better to keep its developement as a separate dub packages - http://code.dlang.org/search?q=mir.
Mar 21 2018