Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Add Array.Fill apis #8137

Merged
merged 3 commits into from
Nov 23, 2016
Merged

Add Array.Fill apis #8137

merged 3 commits into from
Nov 23, 2016

Conversation

jamesqo
Copy link

@jamesqo jamesqo commented Nov 16, 2016

This PR adds the initial implementation of Array.Fill, which has been approved in dotnet/corefx#6695.

FYI @weshaggard @jkotas @benaadams @justinvp

for (int i = 0; i < array.Length; i++)
{
array[i] = value;
}
Copy link
Author

@jamesqo jamesqo Nov 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: This is just the initial implementation which gets things up and running. This can probably be optimized later to be specialized for different types, e.g. use initblk for bytes, or write 16 bytes at a time for other primitives. Or, implementing this natively, so for classes we only incur the array write type-check once. But that is work for a later PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI this is going to mean that my Fill extension method will no longer be selected

@jnm2 This is a static method, so there should be no conflict.

}
}

public static void Fill<T>(T[] array, int startIndex, int count, T value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@terrajobst, this API shape is a little concerning to me. Suppose T == int. Then going from the shorter overload to the longer overload changes the interpretation of the int as the second argument from value to startIndex. Did we consider reordering the args to:

public static void Fill<T>(T[] array, T value);
public static void Fill<T>(T[] array, T value, int startIndex, int count);

? I do understand that it's common to have array/offset/count all next to each other as args; maybe that consistency is more important?

Copy link
Author

@jamesqo jamesqo Nov 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephentoub, good point. I just checked and the generic IndexOf accepts (T[], T, int, int). It sounds reasonable to put the value first.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
}

for (int i = startIndex, end = startIndex + count; i < end; i++)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this give different code than for (int i = startIndex; i < startIndex + count; i++)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danmosemsft Probably not. That was more of a style thing, but I can change it to the form you've suggested.

@danmoseley
Copy link
Member

@jamesqo next step is on you right? just making sure we're not waiting for each other.

@jamesqo
Copy link
Author

jamesqo commented Nov 22, 2016

@danmosemsft Yep, sorry, have been busy lately. I should be able to make the change later today.

@danmoseley
Copy link
Member

No problem @jamesqo just keeping the PR's moving.

@jamesqo
Copy link
Author

jamesqo commented Nov 22, 2016

@danmosemsft This is OK for merge now.

@danmoseley
Copy link
Member

@jamesqo were you going to address @stephentoub 's comment about param ordering?

@jamesqo
Copy link
Author

jamesqo commented Nov 23, 2016

@danmosemsft Strange, I thought I had already taken care of that. Done as well.

@jamesqo
Copy link
Author

jamesqo commented Nov 23, 2016

Test Windows_NT x86 ryujit Checked Build and Test

@danmoseley danmoseley merged commit 3653534 into dotnet:master Nov 23, 2016
@jamesqo jamesqo deleted the array.fill branch November 23, 2016 20:35
@karelz karelz modified the milestone: 2.0.0 Aug 28, 2017
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants