Modern C++ Lamentations

Timon Gehr timon.gehr at gmx.ch
Sun Dec 30 02:55:24 UTC 2018


On 29.12.18 23:01, Steven Schveighoffer wrote:
> 
> I'm wondering if some generic "emulate N nested loops" with given 
> stopping and starting conditions might be a useful addition for 
> std.range or std.algorithm. I'm thinking of other looping algorithms 
> like Floyd Warshall that might benefit from such building blocks.
> 
> -Steve

cartesianProduct suffices for Floyd-Warshall:

cartesianProduct(iota(n),iota(n),iota(n))
     .each!((k,i,j){ d[i][j]=min(d[i][j],d[i][k]+d[k][j]); });

For loops where nested ranges depend on outer indices, 'then' goes a 
long way. It is also easy to do something like:

mixin(comp!q{(i,j,k) | i in iota(n), j in iota(i), k in iota(j)})

This would expand to:

iota(n).then!(i=>iota(i).then!(j=>iota(j).map!(k=>tuple(i,j,k))))

(Of course, right now, tuples are somewhat inconvenient to use.)


More information about the Digitalmars-d mailing list