Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.array.uninitializedArrayExtend, std.array.initializedArray #9928

Open
dlangBugzillaToGithub opened this issue Jun 21, 2012 · 4 comments
Open

Comments

@dlangBugzillaToGithub
Copy link

bearophile_hugs reported this on 2012-06-21T17:19:31Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=8280

CC List

  • safety0ff.bugz

Description

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;
@dlangBugzillaToGithub
Copy link
Author

bearophile_hugs commented on 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;

@dlangBugzillaToGithub
Copy link
Author

safety0ff.bugz commented on 2014-06-13T20:13:57Z

See also: Issue #12444

@dlangBugzillaToGithub
Copy link
Author

bearophile_hugs commented on 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);

@dlangBugzillaToGithub
Copy link
Author

bearophile_hugs commented on 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.

@LightBender LightBender removed the P4 label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants