|
3 | 3 |
|
4 | 4 | using System.Collections.Generic; |
5 | 5 | using System.Numerics; |
6 | | -using System.Runtime.Intrinsics; |
7 | 6 |
|
8 | 7 | namespace System.Linq |
9 | 8 | { |
10 | 9 | public static partial class Enumerable |
11 | 10 | { |
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); |
13 | 12 |
|
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); |
23 | 14 |
|
24 | 15 | public static int? Max(this IEnumerable<int?> source) => MaxInteger(source); |
25 | 16 |
|
@@ -317,22 +308,12 @@ public static decimal Max(this IEnumerable<decimal> source) |
317 | 308 | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); |
318 | 309 | } |
319 | 310 |
|
320 | | - comparer ??= Comparer<TSource>.Default; |
| 311 | + if (source.TryGetSpan(out ReadOnlySpan<TSource> span)) |
| 312 | + { |
| 313 | + return span.Max(comparer); |
| 314 | + } |
321 | 315 |
|
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; |
336 | 317 |
|
337 | 318 | TSource? value = default; |
338 | 319 | using IEnumerator<TSource> e = source.GetEnumerator(); |
|
0 commit comments