Note that there are some explanatory texts on larger screens.

plurals
  1. POD dynamic array initialization, stride and the index operation
    primarykey
    data
    text
    <p>Sorry, this became a 3-fold question regarding arrays</p> <p>I think (dynamic) arrays are truly powerful in D, but the following has been bothering me for a while:</p> <p>In C++ I could easily allocate an array with designated values, but in D I haven't found a way to do so. Surely the following is no problem:</p> <pre><code>int[] a = new int[N]; a[] = a0; </code></pre> <p>But it looks inefficient, since line one will initialize with <code>0</code>, and like 2 with <code>a0</code>. Could something similar to the following be done in D?</p> <pre><code>int[] a = new int(a0)[N]; // illegal </code></pre> <hr> <p>Another efficiency matter I have when using stride in std.range:</p> <pre><code>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) { v.x = i; } f(stride(s, 3)); // error return 0; } </code></pre> <p>Surely I was naive thinking I could simply use stride to create a new array without copying it's elements? There is no way to do so in D, right?</p> <hr> <p>So I went and simulated as if the array was as stride would return, and implemented <code>f</code> as:</p> <pre><code>f(s, 3); void f(S[] s, uint stride) { ref S get(uint i) { assert (i * stride &lt; s.length); return s[i * stride]; } for (uint x ... ) { get(x) = ...; } } </code></pre> <p>Would there be a way to instead write get(x) using the index operator <code>get[x]</code>? This way I could statically mixin / include the striding <code>get</code> function and keep the rest of the function similar. I'd be interested in the approach taken, since a local struct is not allowed to access function scope variables (why not?).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload