Iterating over multiple collections in parallel

Koroskin Denis 2korden at gmail.com
Sat Jul 5 15:07:20 PDT 2008


On Sun, 06 Jul 2008 01:45:08 +0400, Manfred_Nowak <svv1999 at hotmail.com>  
wrote:

> Koroskin Denis wrote:
>
>> int[] c = new int[a.length];
>> foreach (i : a; j : b; ref k : c) {
>>      k = a + b;
>> }
>
> What's wrong with
>
>   auto c= a .* b;
>
> except, that it is neither suggested nor supported?
>
>
> Or
>
>   auto c= a (*) b;
>
> which was suggested some long time ago, but has not got through?
>
>
> Is it horrible then to define:
>
> void opDotMul(T)(out T c, T x, T y){
>     assert( x.length ==  y.length);
>     c.length= x.length;
>     foreach( i,v; x){
>         c[i]= x[i] * y[i];	
>     }
> }
>
> -manfred

Nothing wrong. It was just brought as an example.
Replace int[] with a List!(int) that exposes opApply() but not iterators  
and try again :)

The only way (apart from using stackthreads) you can do it now is  
something like this:

List!(int) a, b, c;

int[] acopy, bcopy;
foreach (i; a) {
     acopy ~= i;
}

foreach (j; b) {
     bcopy ~= j;
}

int i = 0;
foreach (ref k; c) {
     k = acopy[i] + bcopy[i];
}

compare with the following:

List!(int) a, b, c;
foreach (i; a) (j; b)(ref k; c) {
     c = a + b;
}

which one would you prefer?



More information about the Digitalmars-d mailing list