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

Use Span<T>.Fill implementation for Array.Fill #52590

Merged
merged 16 commits into from
Jun 30, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public BitArray(int length, bool defaultValue)

if (defaultValue)
{
m_array.AsSpan().Fill(-1);
Array.Fill(m_array, -1);

// clear high bit values in the last int
Div32Rem(length, out int extraBits);
Expand Down
27 changes: 21 additions & 6 deletions src/libraries/System.Private.CoreLib/src/System/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -785,9 +785,16 @@ public static void Fill<T>(T[] array, T value)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
}

for (int i = 0; i < array.Length; i++)
if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
{
for (int i = 0; i < array.Length; i++)
{
array[i] = value;
}
}
else
{
array[i] = value;
new Span<T>(array).Fill(value);
}
}

Expand All @@ -798,19 +805,27 @@ public static void Fill<T>(T[] array, T value, int startIndex, int count)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
}

if (startIndex < 0 || startIndex > array.Length)
if ((uint)startIndex > (uint)array.Length)
{
ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
}

if (count < 0 || startIndex > array.Length - count)
if ((uint)count > (uint)(array.Length - startIndex))
{
ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
}

for (int i = startIndex; i < startIndex + count; i++)
if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
{
for (int i = startIndex; i < startIndex + count; i++)
{
array[i] = value;
}
}
else
{
array[i] = value;
ref T first = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), (nint)(uint)startIndex);
new Span<T>(ref first, count).Fill(value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private enum Token
private static char[] CreateDefaultIndentChars()
{
var result = new char[IndentArrayLength];
result.AsSpan().Fill(DefaultIndentChar);
Array.Fill(result, DefaultIndentChar);
return result;
}

Expand Down
70 changes: 70 additions & 0 deletions src/libraries/System.Runtime/tests/System/ArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4251,6 +4251,76 @@ public void Fill_InvalidStartIndexCount_ThrowsArgumentOutOfRangeException(int ar
AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => Array.Fill(new string[arrayLength], "", startIndex, count));
}

public class Bar : IEquatable<Bar>
{
public string Value { get; set; }

public bool Equals(Bar other) => string.Equals(Value, other.Value);
}

public class Foo
{
}

private static Bar[] CreateBarArray()
{
return new Bar[]
{
new Bar() { Value = "0" },
new Bar() { Value = "1" },
new Bar() { Value = "2" },
new Bar() { Value = "3" },
};
}

[Fact]
public static void Fill_Downcast()
{
Bar[] barArray = CreateBarArray();
Array.Fill<object>(barArray, new Bar() { Value = "x" });
Assert.Equal(new string[] { "x", "x", "x", "x" }, barArray.Select(e => e.Value));
}

[Fact]
public static void FillWithStartIndexAndCount_Downcast()
{
Bar[] barArray = CreateBarArray();
Array.Fill<object>(barArray, new Bar() { Value = "x" }, 1, 2);
Assert.Equal(new string[] { "0", "x", "x", "3" }, barArray.Select(e => e.Value));
}

[Fact]
public static void Fill_Sidecast()
{
uint[] uintArray = (uint[])(object)new int[] { 0, 1, 2, 3 };
Array.Fill<uint>(uintArray, 42);
Assert.Equal(new int[] { 42, 42, 42, 42 }, (int[])(object)uintArray);
}

[Fact]
public static void FillWithStartIndexAndCount_Sidecast()
{
uint[] uintArray = (uint[])(object)new int[] { 0, 1, 2, 3 };
Array.Fill<uint>(uintArray, 42, 1, 2);
Assert.Equal(new int[] { 0, 42, 42, 3 }, (int[])(object)uintArray);
}

[Fact]
public static void Fill_ThrowsArrayTypeMismatchException()
{
Bar[] barArray = CreateBarArray();
Assert.Throws<ArrayTypeMismatchException>(() => Array.Fill<object>(barArray, new Foo()));
Assert.Equal(CreateBarArray(), barArray);
}

[Fact]
public static void FillWithStartIndexAndCount_ThrowsArrayTypeMismatchException()
{
Bar[] barArray = CreateBarArray();
Assert.Throws<ArrayTypeMismatchException>(() => Array.Fill<object>(barArray, new Foo(), 1, 2));
Assert.Equal(CreateBarArray(), barArray);
}

public static IEnumerable<object[]> Reverse_Generic_Int_TestData()
{
// TODO: use (or merge this data into) Reverse_TestData if/when xunit/xunit#965 is merged
Expand Down