Iterating over multiple collections in parallel

BCS ao at pathlink.com
Thu Jul 3 09:41:13 PDT 2008


Reply to Koroskin,

> If found myself solving the same problem again and again: how to
> implement simultaneous iteration over two (or more collections)?
> 
> suppose, we have the arrays:
> int[] a = [1, 2, 3];
> int[] b = [1, 4, 9];
> and would like to multiply them per-component, like this:
> 
> int[] c = new int[a.length];
> for (int i = 0; i < a.length; ++j) {
> c[i] = a[i] * b[i];
> }
> Since foreach is the prefered (and sometimes the only) way to iterate
> over  collection, there should be a way to iterate over multiple
> collections  simultaneously, like (just an example) this:
> 
> int[] c = new int[a.length];
> foreach (i : a; j : b; ref k : c) {
> k = a + b;
> }
> But this isn't supported (yet?)
> More complicated example would be an iterating over two (or more)
> collection with *no* random access iterators available, opApply only.
> I spend a lot of time on this and have found no solution how to do it
> with  existing D feature set, but this is surely doable with a few
> inter-function gotos and an exception if collections sizes don't
> match.  It's just a small layer written in asm, nothing more.
> 
> How about an enhancement proposal? :)
> 

Can a singe thread have more than one stack? If someone can figure out how 
to do that, then this wouldn't be "to hard" to do in a lib.

call dg1 on first stack, body jumps to stack 2 and calls dg 2, ..., last 
dg is called on the threads normal stack and calls the real body.





More information about the Digitalmars-d mailing list