Comment #0 by bearophile_hugs — 2012-06-21T17:19:31Z
Maybe it's useful to add two small functions to std.array similar to std.array.minimallyInitializedArray and std.array.uninitializedArray, that increase the length of an already allocated array:
uninitializedArrayExtend(arr, 50_000);
and
minimallyInitializedArrayExtend(arr, 50_000);
Are similar to:
arr.length += 50_000;
Comment #1 by bearophile_hugs — 2014-06-13T20:10:18Z
I also suggest to add a "std.array.initializedArray" function similar to std.array.uninitializedArray, that accepts another extra argument that is the initialization value or a lambda to fill the array:
This allocated an array of 100 spaces:
auto data = initializedArray!(char[])(100, ' ');
It is equivalent to:
auto data = uninitializedArray!(char[])(100);
data[] = ' ';
Another example usage (note the result is immutable):
immutable data = initializedArray!(int[])(50, i => i);
That is equivalent to (but there is no data_ temporary variable):
auto data_ = uninitializedArray!(int[])(50);
foreach (immutable i; 0 .. 50)
data_[i] = i;
immutable data = data_.assumeUnique;
Another example usage:
immutable mat = initializedArray!(int[][])(20, 20, (i, j) => i * j);
That is equivalent to (but there is no mat_ temporary variable):
auto mat_ = uninitializedArray!(int[][])(20, 20);
foreach (immutable i; 0 .. 20)
foreach (immutable j; 0 .. 20)
mat_[i][j] = i * j;
immutable mat = mat_.assumeUnique;
Comment #2 by safety0ff.bugz — 2014-06-13T20:13:57Z
See also: Issue #12444
Comment #3 by bearophile_hugs — 2014-06-13T20:45:47Z
(In reply to bearophile_hugs from comment #1)
> auto data = initializedArray!(char[])(100, ' ');
>
> It is equivalent to:
>
> auto data = uninitializedArray!(char[])(100);
> data[] = ' ';
>
>
> Another example usage (note the result is immutable):
>
> immutable data = initializedArray!(int[])(50, i => i);
Perhaps there is one case where there is some ambiguity:
auto funcs = initializedArray!(int function(int)[])(10, i => i);
Comment #4 by bearophile_hugs — 2014-06-13T20:48:19Z
(In reply to bearophile_hugs from comment #3)
> auto funcs = initializedArray!(int function(int)[])(10, i => i);
There can be some very uncommon strange cases, but that's a value and not a lambda to fill the array.
Comment #5 by robert.schadek — 2024-12-01T16:15:15Z