T[][T] -> [(k1, v1), ..., (k1, vn), ..., (kn, v1), ..., (kn, vn)]

Tobias Pankrath tobias at pankrath.net
Thu Feb 13 03:00:15 PST 2014


I've got a hash map T[][T] with keys of T and values of T[]. For 
some reason, I'll need a range of tuples (t1, t2), where t1 is a 
key and t2 is an element of the array t1 maps to. To have them in 
order would be great, but is not necessary.

I just wrote my own range type and it works. But I wonder how 
would I do it with phobos utilities?

I came up with this:

int[][int] hash = [1 :[1,2,3,4], 2:[3,1,2,3], 4:[2,3,1,3]];
auto x = hash.keys.map!(x => tuple(x, hash[x]))
                   .map!(x => zip(repeat(x[0]), x[1]));

Is there any other way to create a range of simple (key,value) 
pairs for the array, but needs to allocate (hash.keys return an 
array of keys) and then again look up every key.

We can loop over (key,value) pairs of the AA with foreach, but I 
guess there is no standard way to create a range from a 
foreach-compatible delegate? Or even get a hand on this delegate?


More information about the Digitalmars-d-learn mailing list