Issue 8280 - std.array.uninitializedArrayExtend, std.array.initializedArray
Summary: std.array.uninitializedArrayExtend, std.array.initializedArray
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-06-21 17:19 UTC by bearophile_hugs
Modified: 2024-12-01 16:15 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2012-06-21 17:19:31 UTC
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 bearophile_hugs 2014-06-13 20:10:18 UTC
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 safety0ff.bugz 2014-06-13 20:13:57 UTC
See also: Issue #12444
Comment 3 bearophile_hugs 2014-06-13 20:45:47 UTC
(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 bearophile_hugs 2014-06-13 20:48:19 UTC
(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 dlangBugzillaToGithub 2024-12-01 16:15:15 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/9928

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB