Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/libraries/System.Linq/src/System/Linq/Max.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@ public static partial class Enumerable

public static long Max(this IEnumerable<long> source) => Max(source, comparer: null);

private static T MaxIntegerEnumerator<T>(IEnumerable<T> source) where T : struct, IBinaryInteger<T>
{
using IEnumerator<T> e = source.GetEnumerator();
if (!e.MoveNext())
{
ThrowHelper.ThrowNoElementsException();
}

T value = e.Current;
while (e.MoveNext())
{
T x = e.Current;
if (x > value)
{
value = x;
}
}

return value;
}

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

public static long? Max(this IEnumerable<long?> source) => MaxInteger(source);
Expand Down Expand Up @@ -313,6 +334,25 @@ public static decimal Max(this IEnumerable<decimal> source)
return span.Max(comparer);
}

// For the non-span integer sequences, use a direct comparison rather than paying the
// per-element cost of Comparer<TSource>.Default.
if (comparer is null || comparer == Comparer<TSource>.Default)
{
if (typeof(TSource) == typeof(byte)) return (TSource)(object)MaxIntegerEnumerator((IEnumerable<byte>)source);
if (typeof(TSource) == typeof(sbyte)) return (TSource)(object)MaxIntegerEnumerator((IEnumerable<sbyte>)source);
if (typeof(TSource) == typeof(ushort)) return (TSource)(object)MaxIntegerEnumerator((IEnumerable<ushort>)source);
if (typeof(TSource) == typeof(short)) return (TSource)(object)MaxIntegerEnumerator((IEnumerable<short>)source);
if (typeof(TSource) == typeof(char)) return (TSource)(object)MaxIntegerEnumerator((IEnumerable<char>)source);
if (typeof(TSource) == typeof(uint)) return (TSource)(object)MaxIntegerEnumerator((IEnumerable<uint>)source);
if (typeof(TSource) == typeof(int)) return (TSource)(object)MaxIntegerEnumerator((IEnumerable<int>)source);
if (typeof(TSource) == typeof(ulong)) return (TSource)(object)MaxIntegerEnumerator((IEnumerable<ulong>)source);
if (typeof(TSource) == typeof(long)) return (TSource)(object)MaxIntegerEnumerator((IEnumerable<long>)source);
if (typeof(TSource) == typeof(nuint)) return (TSource)(object)MaxIntegerEnumerator((IEnumerable<nuint>)source);
if (typeof(TSource) == typeof(nint)) return (TSource)(object)MaxIntegerEnumerator((IEnumerable<nint>)source);
if (typeof(TSource) == typeof(Int128)) return (TSource)(object)MaxIntegerEnumerator((IEnumerable<Int128>)source);
if (typeof(TSource) == typeof(UInt128)) return (TSource)(object)MaxIntegerEnumerator((IEnumerable<UInt128>)source);
}

comparer ??= Comparer<TSource>.Default;

TSource? value = default;
Expand Down
40 changes: 40 additions & 0 deletions src/libraries/System.Linq/src/System/Linq/Min.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@ public static partial class Enumerable

public static long Min(this IEnumerable<long> source) => Min(source, comparer: null);

private static T MinIntegerEnumerator<T>(IEnumerable<T> source) where T : struct, IBinaryInteger<T>
{
using IEnumerator<T> e = source.GetEnumerator();
if (!e.MoveNext())
{
ThrowHelper.ThrowNoElementsException();
}

T value = e.Current;
while (e.MoveNext())
{
T x = e.Current;
if (x < value)
{
value = x;
}
}

return value;
}

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

public static long? Min(this IEnumerable<long?> source) => MinInteger(source);
Expand Down Expand Up @@ -292,6 +313,25 @@ public static decimal Min(this IEnumerable<decimal> source)
return span.Min(comparer);
}

// For the non-span integer sequences, use a direct comparison rather than paying the
// per-element cost of Comparer<TSource>.Default.
if (comparer is null || comparer == Comparer<TSource>.Default)
{
if (typeof(TSource) == typeof(byte)) return (TSource)(object)MinIntegerEnumerator((IEnumerable<byte>)source);
if (typeof(TSource) == typeof(sbyte)) return (TSource)(object)MinIntegerEnumerator((IEnumerable<sbyte>)source);
if (typeof(TSource) == typeof(ushort)) return (TSource)(object)MinIntegerEnumerator((IEnumerable<ushort>)source);
if (typeof(TSource) == typeof(short)) return (TSource)(object)MinIntegerEnumerator((IEnumerable<short>)source);
if (typeof(TSource) == typeof(char)) return (TSource)(object)MinIntegerEnumerator((IEnumerable<char>)source);
if (typeof(TSource) == typeof(uint)) return (TSource)(object)MinIntegerEnumerator((IEnumerable<uint>)source);
if (typeof(TSource) == typeof(int)) return (TSource)(object)MinIntegerEnumerator((IEnumerable<int>)source);
if (typeof(TSource) == typeof(ulong)) return (TSource)(object)MinIntegerEnumerator((IEnumerable<ulong>)source);
if (typeof(TSource) == typeof(long)) return (TSource)(object)MinIntegerEnumerator((IEnumerable<long>)source);
if (typeof(TSource) == typeof(nuint)) return (TSource)(object)MinIntegerEnumerator((IEnumerable<nuint>)source);
if (typeof(TSource) == typeof(nint)) return (TSource)(object)MinIntegerEnumerator((IEnumerable<nint>)source);
if (typeof(TSource) == typeof(Int128)) return (TSource)(object)MinIntegerEnumerator((IEnumerable<Int128>)source);
if (typeof(TSource) == typeof(UInt128)) return (TSource)(object)MinIntegerEnumerator((IEnumerable<UInt128>)source);
}

comparer ??= Comparer<TSource>.Default;

TSource? value = default;
Expand Down
Loading