Sorry, this has become a 3x question regarding arrays
I think the (dynamic) arrays are really efficient in D, but the following bothered me for a while:
In C ++, I could easily allocate an array with the assigned values, but in D I did not find a way to do this. This is undoubtedly not a problem:
int[] a = new int[N]; a[] = a0;
But it looks inefficient, since the first line will be initialized with 0 and like 2 with a0 . Could something like this do in D?
int[] a = new int(a0)[N];
Another efficiency value that I have when using a step in std.range:
import std.stdio; import std.range; struct S { int x; this(this) { writeln("copy ", x); } } void f(S[] s) { } int main() { S[] s = new S[10]; foreach (i, ref v; s) { vx = i; } f(stride(s, 3));
Of course, I naively thought I could just use the step to create a new array without copying its elements? Is there no way in D to do this?
So, I went and modeled as if the array was like a step back, and implemented f as:
f(s, 3); void f(S[] s, uint stride) { ref S get(uint i) { assert (i * stride < s.length); return s[i * stride]; } for (uint x ... ) { get(x) = ...; } }
Would there be a way to write get (x) instead using the get[x] index operator? That way I could statically mix / include the get step function and keep the rest of the function similar. I would be interested in the approach, since the local structure was not allowed to access the variables of the function domain (why not?).