Skip to content

Commit 414b194

Browse files
authored
Add MemoryExtensions.Min/Max (#128306)
1 parent bf3fa3f commit 414b194

11 files changed

Lines changed: 545 additions & 223 deletions

File tree

‎src/libraries/System.Linq/src/System.Linq.csproj‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
<Compile Include="System\Linq\Lookup.cs" />
4545
<Compile Include="System\Linq\Lookup.SpeedOpt.cs" />
4646
<Compile Include="System\Linq\Max.cs" />
47-
<Compile Include="System\Linq\MaxMin.cs" />
4847
<Compile Include="System\Linq\Min.cs" />
4948
<Compile Include="System\Linq\OfType.cs" />
5049
<Compile Include="System\Linq\OfType.SpeedOpt.cs" />

‎src/libraries/System.Linq/src/System/Linq/Max.cs‎

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,14 @@
33

44
using System.Collections.Generic;
55
using System.Numerics;
6-
using System.Runtime.Intrinsics;
76

87
namespace System.Linq
98
{
109
public static partial class Enumerable
1110
{
12-
public static int Max(this IEnumerable<int> source) => MinMaxInteger<int, MaxCalc<int>>(source);
11+
public static int Max(this IEnumerable<int> source) => Max(source, comparer: null);
1312

14-
public static long Max(this IEnumerable<long> source) => MinMaxInteger<long, MaxCalc<long>>(source);
15-
16-
private readonly struct MaxCalc<T> : IMinMaxCalc<T> where T : struct, IBinaryInteger<T>
17-
{
18-
public static bool Compare(T left, T right) => left > right;
19-
public static Vector128<T> Compare(Vector128<T> left, Vector128<T> right) => Vector128.Max(left, right);
20-
public static Vector256<T> Compare(Vector256<T> left, Vector256<T> right) => Vector256.Max(left, right);
21-
public static Vector512<T> Compare(Vector512<T> left, Vector512<T> right) => Vector512.Max(left, right);
22-
}
13+
public static long Max(this IEnumerable<long> source) => Max(source, comparer: null);
2314

2415
public static int? Max(this IEnumerable<int?> source) => MaxInteger(source);
2516

@@ -317,22 +308,12 @@ public static decimal Max(this IEnumerable<decimal> source)
317308
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
318309
}
319310

320-
comparer ??= Comparer<TSource>.Default;
311+
if (source.TryGetSpan(out ReadOnlySpan<TSource> span))
312+
{
313+
return span.Max(comparer);
314+
}
321315

322-
// TODO https://github.com/dotnet/csharplang/discussions/6308: Update this to use generic constraint bridging if/when available.
323-
if (typeof(TSource) == typeof(byte) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<byte, MaxCalc<byte>>((IEnumerable<byte>)source);
324-
if (typeof(TSource) == typeof(sbyte) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<sbyte, MaxCalc<sbyte>>((IEnumerable<sbyte>)source);
325-
if (typeof(TSource) == typeof(ushort) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<ushort, MaxCalc<ushort>>((IEnumerable<ushort>)source);
326-
if (typeof(TSource) == typeof(short) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<short, MaxCalc<short>>((IEnumerable<short>)source);
327-
if (typeof(TSource) == typeof(char) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<char, MaxCalc<char>>((IEnumerable<char>)source);
328-
if (typeof(TSource) == typeof(uint) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<uint, MaxCalc<uint>>((IEnumerable<uint>)source);
329-
if (typeof(TSource) == typeof(int) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<int, MaxCalc<int>>((IEnumerable<int>)source);
330-
if (typeof(TSource) == typeof(ulong) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<ulong, MaxCalc<ulong>>((IEnumerable<ulong>)source);
331-
if (typeof(TSource) == typeof(long) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<long, MaxCalc<long>>((IEnumerable<long>)source);
332-
if (typeof(TSource) == typeof(nuint) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<nuint, MaxCalc<nuint>>((IEnumerable<nuint>)source);
333-
if (typeof(TSource) == typeof(nint) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<nint, MaxCalc<nint>>((IEnumerable<nint>)source);
334-
if (typeof(TSource) == typeof(Int128) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<Int128, MaxCalc<Int128>>((IEnumerable<Int128>)source);
335-
if (typeof(TSource) == typeof(UInt128) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<UInt128, MaxCalc<UInt128>>((IEnumerable<UInt128>)source);
316+
comparer ??= Comparer<TSource>.Default;
336317

337318
TSource? value = default;
338319
using IEnumerator<TSource> e = source.GetEnumerator();

‎src/libraries/System.Linq/src/System/Linq/MaxMin.cs‎

Lines changed: 0 additions & 170 deletions
This file was deleted.

‎src/libraries/System.Linq/src/System/Linq/Min.cs‎

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,14 @@
33

44
using System.Collections.Generic;
55
using System.Numerics;
6-
using System.Runtime.Intrinsics;
76

87
namespace System.Linq
98
{
109
public static partial class Enumerable
1110
{
12-
public static int Min(this IEnumerable<int> source) => MinMaxInteger<int, MinCalc<int>>(source);
11+
public static int Min(this IEnumerable<int> source) => Min(source, comparer: null);
1312

14-
public static long Min(this IEnumerable<long> source) => MinMaxInteger<long, MinCalc<long>>(source);
15-
16-
private readonly struct MinCalc<T> : IMinMaxCalc<T> where T : struct, IBinaryInteger<T>
17-
{
18-
public static bool Compare(T left, T right) => left < right;
19-
public static Vector128<T> Compare(Vector128<T> left, Vector128<T> right) => Vector128.Min(left, right);
20-
public static Vector256<T> Compare(Vector256<T> left, Vector256<T> right) => Vector256.Min(left, right);
21-
public static Vector512<T> Compare(Vector512<T> left, Vector512<T> right) => Vector512.Min(left, right);
22-
}
13+
public static long Min(this IEnumerable<long> source) => Min(source, comparer: null);
2314

2415
public static int? Min(this IEnumerable<int?> source) => MinInteger(source);
2516

@@ -296,22 +287,12 @@ public static decimal Min(this IEnumerable<decimal> source)
296287
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
297288
}
298289

299-
comparer ??= Comparer<TSource>.Default;
290+
if (source.TryGetSpan(out ReadOnlySpan<TSource> span))
291+
{
292+
return span.Min(comparer);
293+
}
300294

301-
// TODO https://github.com/dotnet/csharplang/discussions/6308: Update this to use generic constraint bridging if/when available.
302-
if (typeof(TSource) == typeof(byte) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<byte, MinCalc<byte>>((IEnumerable<byte>)source);
303-
if (typeof(TSource) == typeof(sbyte) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<sbyte, MinCalc<sbyte>>((IEnumerable<sbyte>)source);
304-
if (typeof(TSource) == typeof(ushort) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<ushort, MinCalc<ushort>>((IEnumerable<ushort>)source);
305-
if (typeof(TSource) == typeof(short) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<short, MinCalc<short>>((IEnumerable<short>)source);
306-
if (typeof(TSource) == typeof(char) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<char, MinCalc<char>>((IEnumerable<char>)source);
307-
if (typeof(TSource) == typeof(uint) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<uint, MinCalc<uint>>((IEnumerable<uint>)source);
308-
if (typeof(TSource) == typeof(int) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<int, MinCalc<int>>((IEnumerable<int>)source);
309-
if (typeof(TSource) == typeof(ulong) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<ulong, MinCalc<ulong>>((IEnumerable<ulong>)source);
310-
if (typeof(TSource) == typeof(long) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<long, MinCalc<long>>((IEnumerable<long>)source);
311-
if (typeof(TSource) == typeof(nuint) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<nuint, MinCalc<nuint>>((IEnumerable<nuint>)source);
312-
if (typeof(TSource) == typeof(nint) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<nint, MinCalc<nint>>((IEnumerable<nint>)source);
313-
if (typeof(TSource) == typeof(Int128) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<Int128, MinCalc<Int128>>((IEnumerable<Int128>)source);
314-
if (typeof(TSource) == typeof(UInt128) && comparer == Comparer<TSource>.Default) return (TSource)(object)MinMaxInteger<UInt128, MinCalc<UInt128>>((IEnumerable<UInt128>)source);
295+
comparer ??= Comparer<TSource>.Default;
315296

316297
TSource? value = default;
317298
using IEnumerator<TSource> e = source.GetEnumerator();

‎src/libraries/System.Memory/ref/System.Memory.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,10 @@ public static void CopyTo<T>(this T[]? source, System.Span<T> destination) { }
447447
[System.Runtime.CompilerServices.OverloadResolutionPriorityAttribute(-1)]
448448
public static int LastIndexOfAnyInRange<T>(this System.Span<T> span, T lowInclusive, T highInclusive) where T : System.IComparable<T> { throw null; }
449449
public static int LastIndexOfAnyWhiteSpace(this System.ReadOnlySpan<char> span) { throw null; }
450+
public static T? Max<T>(this System.ReadOnlySpan<T> span) { throw null; }
451+
public static T? Max<T>(this System.ReadOnlySpan<T> span, System.Collections.Generic.IComparer<T>? comparer) { throw null; }
452+
public static T? Min<T>(this System.ReadOnlySpan<T> span) { throw null; }
453+
public static T? Min<T>(this System.ReadOnlySpan<T> span, System.Collections.Generic.IComparer<T>? comparer) { throw null; }
450454
public static bool Overlaps<T>(this System.ReadOnlySpan<T> span, System.ReadOnlySpan<T> other) { throw null; }
451455
public static bool Overlaps<T>(this System.ReadOnlySpan<T> span, System.ReadOnlySpan<T> other, out int elementOffset) { throw null; }
452456
[System.Runtime.CompilerServices.OverloadResolutionPriorityAttribute(-1)]

0 commit comments

Comments
 (0)