digitalmars.D.learn - Using zip to copy subarray into another
- Craig Dillabaugh (18/18) Aug 13 2013 I have code that attempts to copy a slice of one array into
- Jonathan M Davis (5/11) Aug 13 2013 Why not just use std.algorithm.copy, or even just assigning one array to...
- Craig Dillabaugh (14/26) Aug 13 2013 These work reasonably well and I will likely use your suggestions
- John Colvin (11/43) Aug 13 2013 warning: untested!
- Craig Dillabaugh (4/54) Aug 14 2013 Thanks. I ended up using something along these lines in my code
- Joseph Rushton Wakeling (4/10) Aug 13 2013 Try lockstep instead of zip. See:
- Craig Dillabaugh (7/10) Aug 14 2013 On Tuesday, 13 August 2013 at 21:37:28 UTC, Joseph Rushton
I have code that attempts to copy a slice of one array into another using zip. However, the array is not updated. I am guessing this is because modifying the returned tuple does not modify the actual arrays. Is there any way to do this. import std.stdio; import std.range; void main( string[] args ) { int[] array1 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]; int[] array2 = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]; foreach( ref a; zip(array1[2..$], array2[4..6] ) ) { a[0] = a[1]; } foreach( int v; array1 ) write(v, ","); } prints 1,1,1,1,... Thanks Craig
Aug 13 2013
On Tuesday, August 13, 2013 21:22:24 Craig Dillabaugh wrote:I have code that attempts to copy a slice of one array into another using zip. However, the array is not updated. I am guessing this is because modifying the returned tuple does not modify the actual arrays. Is there any way to do this.Why not just use std.algorithm.copy, or even just assigning one array to the other? e.g. dest[2 .. 5] = src[1 .. 4]; - Jonathan M Davis
Aug 13 2013
On Tuesday, 13 August 2013 at 19:50:37 UTC, Jonathan M Davis wrote:On Tuesday, August 13, 2013 21:22:24 Craig Dillabaugh wrote:These work reasonably well and I will likely use your suggestions here. The one thing I get with zip that I lose using std.algorithm.copy or straight assignment of the slices is that it handles mismatches in slice sizes automatically (it stops as soon as either slice is exhausted). For example copy(array2[4..8], array1[9..$]); where array1 had 10 elements fails because it copies data off the end of array1, and if I use straight assignment the slices must be the same length. CraigI have code that attempts to copy a slice of one array into another using zip. However, the array is not updated. I am guessing this is because modifying the returned tuple does not modify the actual arrays. Is there any way to do this.Why not just use std.algorithm.copy, or even just assigning one array to the other? e.g. dest[2 .. 5] = src[1 .. 4]; - Jonathan M Davis
Aug 13 2013
On Tuesday, 13 August 2013 at 20:23:00 UTC, Craig Dillabaugh wrote:On Tuesday, 13 August 2013 at 19:50:37 UTC, Jonathan M Davis wrote:warning: untested! void myCopy(T)(T[] a, T[] b) { auto possible = min(a.length, b.length); a[0 .. possible] = b[0 .. possible]; } myCopy(array2[4..8], array1[9..$]); Arrays only atm, but you could quite easily spruce that up in to something much more generic.On Tuesday, August 13, 2013 21:22:24 Craig Dillabaugh wrote:These work reasonably well and I will likely use your suggestions here. The one thing I get with zip that I lose using std.algorithm.copy or straight assignment of the slices is that it handles mismatches in slice sizes automatically (it stops as soon as either slice is exhausted). For example copy(array2[4..8], array1[9..$]); where array1 had 10 elements fails because it copies data off the end of array1, and if I use straight assignment the slices must be the same length. CraigI have code that attempts to copy a slice of one array into another using zip. However, the array is not updated. I am guessing this is because modifying the returned tuple does not modify the actual arrays. Is there any way to do this.Why not just use std.algorithm.copy, or even just assigning one array to the other? e.g. dest[2 .. 5] = src[1 .. 4]; - Jonathan M Davis
Aug 13 2013
On Tuesday, 13 August 2013 at 21:10:53 UTC, John Colvin wrote:On Tuesday, 13 August 2013 at 20:23:00 UTC, Craig Dillabaugh wrote:Thanks. I ended up using something along these lines in my code (although I didn't write a nice generic function as you have done).On Tuesday, 13 August 2013 at 19:50:37 UTC, Jonathan M Davis wrote:warning: untested! void myCopy(T)(T[] a, T[] b) { auto possible = min(a.length, b.length); a[0 .. possible] = b[0 .. possible]; } myCopy(array2[4..8], array1[9..$]); Arrays only atm, but you could quite easily spruce that up in to something much more generic.On Tuesday, August 13, 2013 21:22:24 Craig Dillabaugh wrote:These work reasonably well and I will likely use your suggestions here. The one thing I get with zip that I lose using std.algorithm.copy or straight assignment of the slices is that it handles mismatches in slice sizes automatically (it stops as soon as either slice is exhausted). For example copy(array2[4..8], array1[9..$]); where array1 had 10 elements fails because it copies data off the end of array1, and if I use straight assignment the slices must be the same length. CraigI have code that attempts to copy a slice of one array into another using zip. However, the array is not updated. I am guessing this is because modifying the returned tuple does not modify the actual arrays. Is there any way to do this.Why not just use std.algorithm.copy, or even just assigning one array to the other? e.g. dest[2 .. 5] = src[1 .. 4]; - Jonathan M Davis
Aug 14 2013
On 08/13/2013 09:22 PM, Craig Dillabaugh wrote:I have code that attempts to copy a slice of one array into another using zip. However, the array is not updated. I am guessing this is because modifying the returned tuple does not modify the actual arrays. Is there any way to do this.Try lockstep instead of zip. See: http://d.puremagic.com/issues/show_bug.cgi?id=8155#c8 ... for more info on the reasons why zip doesn't work here.
Aug 13 2013
On Tuesday, 13 August 2013 at 21:37:28 UTC, Joseph Rushton Wakeling wrote: clip.Try lockstep instead of zip. See: http://d.puremagic.com/issues/show_bug.cgi?id=8155#c8 ... for more info on the reasons why zip doesn't work here.While I've now solved my problem using a slightly different method, you are right lockstep would have also likely made a good solution. Thanks.
Aug 14 2013