[D1,static array] fill static multidimensional array.
bearophile
bearophileHUGS at lycos.com
Thu Oct 21 19:41:17 PDT 2010
%u:
> What is the fastest way to fill a static multidimensional array?
If you want a safe solution, then you probably need nested loops, and a array[] = x; in the inner loop (such loops may be generated with a string mixin too).
Otherwise if speed is more important, fixed sized multidimensional arrays are stored in a contiguous way in D, so you may use a memset or something like:
import std.stdio: writeln;
void main() {
int[5][4] mat;
(cast(int[mat[0].length * mat.length])mat)[] = 10;
writeln(mat);
}
Or even:
(cast(int[mat.sizeof / int.sizeof])mat)[] = 10;
(If necessary you may use a recursive template trick to find the type of the basic item of the array).
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list