Skip to content

Commit 0fd22db

Browse files
committed
MinBy and MaxBy Span
1 parent e689ea5 commit 0fd22db

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

src/Extensions/Span/SpanExtensions.Linq.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Numerics;
34

45
namespace SpanExtensions
@@ -1111,5 +1112,71 @@ public static BigInteger Max<T>(this Span<T> source, Func<T, BigInteger> selecto
11111112
return ReadOnlySpanExtensions.Max(source, selector);
11121113
}
11131114
#endif
1115+
1116+
/// <summary>
1117+
/// Returns the minimum value in a generic sequence according to a specified key selector function.
1118+
/// </summary>
1119+
/// <typeparam name="TSource">The type of elements in <paramref name="source"/>.</typeparam>
1120+
/// <typeparam name="TKey">The type of key to compare elements by.</typeparam>
1121+
/// <param name="source">A <see cref="Span{TSource}"/> to determine the minimum value of.</param>
1122+
/// <param name="keySelector">A function to extract the key for each element.</param>
1123+
/// <returns>The value with the minimum key in <paramref name="source"/>.</returns>
1124+
/// <exception cref="ArgumentNullException"><paramref name="keySelector"/> is null.</exception>
1125+
/// <exception cref="InvalidOperationException"><typeparamref name="TSource"/> is a primitive type and <paramref name="source"/> is empty.</exception>
1126+
/// <remarks>If <paramref name="source"/> is empty and <typeparamref name="TSource"/> is a non-nullable struct, such as a primitive type, an <see cref="InvalidOperationException"/> is thrown.</remarks>
1127+
public static TSource MinBy<TSource, TKey>(this Span<TSource> source, Func<TSource, TKey> keySelector) where TKey : IComparable<TKey>
1128+
{
1129+
return ReadOnlySpanExtensions.MinBy(source, keySelector);
1130+
}
1131+
1132+
/// <summary>
1133+
/// Returns the minimum value in a generic sequence according to a specified key selector function.
1134+
/// </summary>
1135+
/// <typeparam name="TSource">The type of elements in <paramref name="source"/>.</typeparam>
1136+
/// <typeparam name="TKey">The type of key to compare elements by.</typeparam>
1137+
/// <param name="source">A <see cref="Span{TSource}"/> to determine the minimum value of.</param>
1138+
/// <param name="keySelector">A function to extract the key for each element.</param>
1139+
/// <param name="comparer">The <see cref="IComparer{TSource}"/> to compare keys.</param>
1140+
/// <returns>The value with the minimum key in <paramref name="source"/>.</returns>
1141+
/// <exception cref="ArgumentNullException"><paramref name="keySelector"/> is null.</exception>
1142+
/// <exception cref="InvalidOperationException"><typeparamref name="TSource"/> is a primitive type and <paramref name="source"/> is empty.</exception>
1143+
/// <remarks>If <paramref name="source"/> is empty and <typeparamref name="TSource"/> is a non-nullable struct, such as a primitive type, an <see cref="InvalidOperationException"/> is thrown.</remarks>
1144+
public static TSource MinBy<TSource, TKey>(this Span<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
1145+
{
1146+
return ReadOnlySpanExtensions.MinBy(source, keySelector, comparer);
1147+
}
1148+
1149+
/// <summary>
1150+
/// Returns the maximum value in a generic sequence according to a specified key selector function.
1151+
/// </summary>
1152+
/// <typeparam name="TSource">The type of elements in <paramref name="source"/>.</typeparam>
1153+
/// <typeparam name="TKey">The type of key to compare elements by.</typeparam>
1154+
/// <param name="source">A <see cref="Span{TSource}"/> to determine the maximum value of.</param>
1155+
/// <param name="keySelector">A function to extract the key for each element.</param>
1156+
/// <returns>The value with the maximum key in <paramref name="source"/>.</returns>
1157+
/// <exception cref="ArgumentNullException"><paramref name="keySelector"/> is null.</exception>
1158+
/// <exception cref="InvalidOperationException"><typeparamref name="TSource"/> is a primitive type and <paramref name="source"/> is empty.</exception>
1159+
/// <remarks>If <paramref name="source"/> is empty and <typeparamref name="TSource"/> is a non-nullable struct, such as a primitive type, an <see cref="InvalidOperationException"/> is thrown.</remarks>
1160+
public static TSource MaxBy<TSource, TKey>(this Span<TSource> source, Func<TSource, TKey> keySelector) where TKey : IComparable<TKey>
1161+
{
1162+
return ReadOnlySpanExtensions.MaxBy(source, keySelector);
1163+
}
1164+
1165+
/// <summary>
1166+
/// Returns the maximum value in a generic sequence according to a specified key selector function.
1167+
/// </summary>
1168+
/// <typeparam name="TSource">The type of elements in <paramref name="source"/>.</typeparam>
1169+
/// <typeparam name="TKey">The type of key to compare elements by.</typeparam>
1170+
/// <param name="source">A <see cref="Span{TSource}"/> to determine the maximum value of.</param>
1171+
/// <param name="keySelector">A function to extract the key for each element.</param>
1172+
/// <param name="comparer">The <see cref="IComparer{TSource}"/> to compare keys.</param>
1173+
/// <returns>The value with the maximum key in <paramref name="source"/>.</returns>
1174+
/// <exception cref="ArgumentNullException"><paramref name="keySelector"/> is null.</exception>
1175+
/// <exception cref="InvalidOperationException"><typeparamref name="TSource"/> is a primitive type and <paramref name="source"/> is empty.</exception>
1176+
/// <remarks>If <paramref name="source"/> is empty and <typeparamref name="TSource"/> is a non-nullable struct, such as a primitive type, an <see cref="InvalidOperationException"/> is thrown.</remarks>
1177+
public static TSource MaxBy<TSource, TKey>(this Span<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
1178+
{
1179+
return ReadOnlySpanExtensions.MaxBy(source, keySelector, comparer);
1180+
}
11141181
}
11151182
}

0 commit comments

Comments
 (0)