[D1,static array] fill static multidimensional array.

%u e at ee.com
Fri Oct 22 11:54:02 PDT 2010


== Quote from spir (denis.spir at gmail.com)'s article
> How does this method perform? would you be kind enough to include it in you
> r tests, %u? Even if it's slightly slower than "(cast(int[n1*n2*n3])arr)[]
> = e;", I would use it because I find it cleaner.

But it does take up more memory (assuming you don't generate the arrays every fill).
And, it performs worse than the casts (with this array length).
If you really want to investigate, I suggest disassembling ;)

Here is the added code:
int[50][50] arr2;
int[50] arr1;

timer.start();
for(int i = 0; i < 10_000; i++) {
    arr1[] = i;
    arr2[] = arr1;
    arr3[] = arr2;
}
timer.stop();
writefln("123         : ",timer.periodCount());

And killing almost every process on my computer the new timings are:
[x][y][z]   : 37..
[x][y][]    : 38..
cast(int[*]): 28..
cast(int[1]): 28..
123         : 36..
fill        : 28..

> Is D reflexive enough to know at runtime the number of dimensions of an arr
> ay and their sizes, and write a generic
>     arrayDeepFill(arr, element);
> where element is the "terminal" value? Or maybe
>     arrayDeepFill(arr, element, [sizes]);

Yeah, wouldn't that be nice ?!
arr.Fill(elem);

:D

template BaseType(T: T[]) { alias BaseType!(T) BaseType; }
template BaseType(T) { alias T BaseType; }

void Fill(T,E)(T arr, E elem) {
	static assert( isStaticArray!(T) );
	static assert( is( BaseType!(T) == E ) );
	(cast(E[arr.sizeof / elem.sizeof])arr)[] = elem;
}


More information about the Digitalmars-d-learn mailing list