-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Add MemoryExtensions.Min/Max #128306
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
base: main
Are you sure you want to change the base?
Add MemoryExtensions.Min/Max #128306
Changes from 3 commits
baff8f3
6ef9e87
a1023cc
d214de8
f5107bd
93c617a
b4fb827
8d32597
69566d1
c2c8dfa
55b1cf7
0acc339
9c71813
9835263
9193d7e
6d34978
9172c33
b9d0ba1
6182e81
bd6d541
2eabf0e
e04c30e
cc58f4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using Xunit; | ||
|
|
||
| namespace System.SpanTests | ||
| { | ||
| public static partial class ReadOnlySpanTests | ||
| { | ||
| [Fact] | ||
| public static void MinMax_Empty_NonNullableValueType_Throws() | ||
| { | ||
| ReadOnlySpan<int> span = ReadOnlySpan<int>.Empty; | ||
|
|
||
| TestHelpers.AssertThrows<InvalidOperationException, int>(span, (_span) => _span.Min()); | ||
|
manandre marked this conversation as resolved.
Outdated
|
||
| TestHelpers.AssertThrows<InvalidOperationException, int>(span, (_span) => _span.Max()); | ||
|
manandre marked this conversation as resolved.
Outdated
|
||
| TestHelpers.AssertThrows<InvalidOperationException, int>(span, (_span) => _span.Min(Comparer<int>.Default)); | ||
| TestHelpers.AssertThrows<InvalidOperationException, int>(span, (_span) => _span.Max(Comparer<int>.Default)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void MinMax_NullComparer_ThrowsArgumentNullException() | ||
| { | ||
| ReadOnlySpan<int> span = new int[] { 4, -1, 7, 2 }; | ||
|
|
||
| TestHelpers.AssertThrows<ArgumentNullException, int>(span, (_span) => _span.Min(comparer: null!)); | ||
| TestHelpers.AssertThrows<ArgumentNullException, int>(span, (_span) => _span.Max(comparer: null!)); | ||
|
manandre marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| [Fact] | ||
| public static void MinMax_Empty_ReferenceAndNullableValueType_ReturnsNull() | ||
| { | ||
| ReadOnlySpan<string?> strings = ReadOnlySpan<string?>.Empty; | ||
| ReadOnlySpan<int?> nullableInts = ReadOnlySpan<int?>.Empty; | ||
|
|
||
| Assert.Null(strings.Min()); | ||
| Assert.Null(strings.Max()); | ||
| Assert.Null(nullableInts.Min()); | ||
| Assert.Null(nullableInts.Max()); | ||
| } | ||
|
manandre marked this conversation as resolved.
manandre marked this conversation as resolved.
|
||
|
|
||
| [Fact] | ||
| public static void MinMax_AllNull_ReturnsNull() | ||
| { | ||
| ReadOnlySpan<string?> strings = new string?[] { null, null, null }; | ||
| ReadOnlySpan<int?> nullableInts = new int?[] { null, null, null }; | ||
|
|
||
| Assert.Null(strings.Min()); | ||
| Assert.Null(strings.Max()); | ||
| Assert.Null(nullableInts.Min()); | ||
| Assert.Null(nullableInts.Max()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void MinMax_NullNotFirst_NullIgnoredForComparison() | ||
| { | ||
| ReadOnlySpan<string?> strings = new string?[] { "charlie", null, "bravo", null, "delta" }; | ||
| ReadOnlySpan<int?> nullableInts = new int?[] { 4, null, -1, null, 7 }; | ||
|
|
||
| Assert.Equal("bravo", strings.Min()); | ||
| Assert.Equal("delta", strings.Max()); | ||
| Assert.Equal(-1, nullableInts.Min()); | ||
| Assert.Equal(7, nullableInts.Max()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void MinMax_DefaultComparer_ProducesExpectedValues() | ||
|
manandre marked this conversation as resolved.
|
||
| { | ||
| ReadOnlySpan<int> ints = new int[] { 4, -1, 7, 2 }; | ||
| ReadOnlySpan<string?> strings = new string?[] { null, "charlie", "bravo", null, "delta" }; | ||
|
|
||
| Assert.Equal(-1, ints.Min()); | ||
| Assert.Equal(7, ints.Max()); | ||
|
|
||
| Assert.Equal("bravo", strings.Min()); | ||
| Assert.Equal("delta", strings.Max()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void MinMax_CustomComparer_IsUsed() | ||
| { | ||
| ReadOnlySpan<int> ints = new int[] { 4, -1, 7, 2 }; | ||
| IComparer<int> reverse = Comparer<int>.Create((left, right) => right.CompareTo(left)); | ||
|
manandre marked this conversation as resolved.
|
||
|
|
||
| Assert.Equal(7, ints.Min(reverse)); | ||
| Assert.Equal(-1, ints.Max(reverse)); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4312,6 +4312,122 @@ public static int BinarySearch<T, TComparer>( | |
| return BinarySearch(span, comparable); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns the minimum value in the span. | ||
| /// </summary> | ||
| /// <typeparam name="T">The type of the elements in the span.</typeparam> | ||
| /// <param name="span">The span of values to determine the minimum value of.</param> | ||
| /// <returns>The minimum value in the span.</returns> | ||
| /// <exception cref="InvalidOperationException"><paramref name="span"/> is empty and <typeparamref name="T"/> is a non-nullable value type.</exception> | ||
|
manandre marked this conversation as resolved.
Outdated
manandre marked this conversation as resolved.
Outdated
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static T? Min<T>(this ReadOnlySpan<T> span) => | ||
| Min(span, Comparer<T>.Default); | ||
|
manandre marked this conversation as resolved.
Outdated
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Much like with LINQ, this should be passing
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tannergooding Should we then update the approved API to explicitly accept a null comparer? public T? Min<T>(this ReadOnlySpan<T> span, IComparer<T>? comparer);
public T? Max<T>(this ReadOnlySpan<T> span, IComparer<T>? comparer);
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nullability annotations are often overlooked in the API review. I've left an explicit comment indicating the oversight, so please adjust the PR accordingly. |
||
|
|
||
|
tannergooding marked this conversation as resolved.
Outdated
manandre marked this conversation as resolved.
Outdated
|
||
| /// <summary> | ||
| /// Returns the minimum value in the span. | ||
| /// </summary> | ||
| /// <typeparam name="T">The type of the elements in the span.</typeparam> | ||
| /// <param name="span">The span of values to determine the minimum value of.</param> | ||
| /// <param name="comparer">The <see cref="IComparer{T}"/> to compare values.</param> | ||
| /// <returns>The minimum value in the span.</returns> | ||
| /// <exception cref="InvalidOperationException"><paramref name="span"/> is empty and <typeparamref name="T"/> is a non-nullable value type.</exception> | ||
| public static T? Min<T>(this ReadOnlySpan<T> span, IComparer<T> comparer) | ||
|
manandre marked this conversation as resolved.
Outdated
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's unclear why this deviates from the enumerable logic in implementation. I'd generally expect the two entry points to be nearly identical. Covering the acceleration and other checks like: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Linq/src/System/Linq/Min.cs#L292-L373 The biggest difference should be that
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have aligned the implementation on the enumerable logic. |
||
| { | ||
| if (comparer is null) | ||
| ThrowHelper.ThrowArgumentNullException(ExceptionArgument.comparer); | ||
|
manandre marked this conversation as resolved.
Outdated
|
||
|
|
||
| if (span.IsEmpty) | ||
| { | ||
| if (default(T) is null) | ||
| { | ||
| return default; | ||
| } | ||
|
|
||
| ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NoElements); | ||
| } | ||
|
|
||
| T? value = span[0]; | ||
| int i = 1; | ||
|
|
||
| while (value is null) | ||
| { | ||
| if ((uint)i >= (uint)span.Length) | ||
| { | ||
| return value; | ||
| } | ||
| value = span[i++]; | ||
| } | ||
|
|
||
| for (; (uint)i < (uint)span.Length; i++) | ||
| { | ||
| T next = span[i]; | ||
| if (next is not null && comparer.Compare(next, value) < 0) | ||
| { | ||
| value = next; | ||
| } | ||
| } | ||
|
manandre marked this conversation as resolved.
Outdated
manandre marked this conversation as resolved.
Outdated
|
||
|
|
||
| return value; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns the maximum value in the span. | ||
| /// </summary> | ||
| /// <typeparam name="T">The type of the elements in the span.</typeparam> | ||
| /// <param name="span">The span of values to determine the maximum value of.</param> | ||
| /// <returns>The maximum value in the span.</returns> | ||
| /// <exception cref="InvalidOperationException"><paramref name="span"/> is empty and <typeparamref name="T"/> is a non-nullable value type.</exception> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static T? Max<T>(this ReadOnlySpan<T> span) => | ||
| Max(span, Comparer<T>.Default); | ||
|
|
||
| /// <summary> | ||
| /// Returns the maximum value in the span. | ||
| /// </summary> | ||
| /// <typeparam name="T">The type of the elements in the span.</typeparam> | ||
| /// <param name="span">The span of values to determine the maximum value of.</param> | ||
| /// <param name="comparer">The <see cref="IComparer{T}"/> to compare values.</param> | ||
| /// <returns>The maximum value in the span.</returns> | ||
| /// <exception cref="InvalidOperationException"><paramref name="span"/> is empty and <typeparamref name="T"/> is a non-nullable value type.</exception> | ||
| public static T? Max<T>(this ReadOnlySpan<T> span, IComparer<T> comparer) | ||
| { | ||
| if (comparer is null) | ||
| ThrowHelper.ThrowArgumentNullException(ExceptionArgument.comparer); | ||
|
|
||
| if (span.IsEmpty) | ||
| { | ||
| if (default(T) is null) | ||
| { | ||
| return default; | ||
| } | ||
|
|
||
| ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NoElements); | ||
| } | ||
|
|
||
| T? value = span[0]; | ||
| int i = 1; | ||
|
|
||
| while (value is null) | ||
| { | ||
| if ((uint)i >= (uint)span.Length) | ||
| { | ||
| return value; | ||
| } | ||
| value = span[i++]; | ||
| } | ||
|
|
||
| for (; (uint)i < (uint)span.Length; i++) | ||
| { | ||
| T next = span[i]; | ||
| if (next is not null && comparer.Compare(next, value) > 0) | ||
| { | ||
| value = next; | ||
| } | ||
| } | ||
|
|
||
| return value; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sorts the elements in the entire <see cref="Span{T}" /> using the <see cref="IComparable{T}" /> implementation | ||
| /// of each element of the <see cref= "Span{T}" /> | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.