feat: 为 MaxByAll/MinByAll/MaxBy/MinBy 添加 IComparer 重载
Agent-Logs-Url: https://github.com/Meloong-Git/MeloongCore/sessions/e26ccaa3-3e0f-4a45-8af2-9cfba2e43250 Co-authored-by: LTCatt <40348278+LTCatt@users.noreply.github.com>
This commit is contained in:
1 parent
b1b18d4bb3
commit
8ef8796922
1 file changed
+84
@@ -65,6 +65,31 @@ public static class EnumerableExtensions {
|
||||
return results;
|
||||
}
|
||||
/// <summary>
|
||||
/// 选择所有最大值对应的对象。
|
||||
/// 若没有元素则返回空列表。
|
||||
/// </summary>
|
||||
public static List<TSource> MaxByAll<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector, IComparer<TKey> comparer) {
|
||||
var results = new List<TSource>();
|
||||
using var enumerator = source.GetEnumerator();
|
||||
if (!enumerator.MoveNext()) return results;
|
||||
TSource maxItem = enumerator.Current;
|
||||
TKey maxValue = selector(maxItem);
|
||||
results.Add(maxItem);
|
||||
while (enumerator.MoveNext()) {
|
||||
TSource currentItem = enumerator.Current;
|
||||
TKey currentValue = selector(currentItem);
|
||||
int comparisonResult = comparer.Compare(currentValue, maxValue);
|
||||
if (comparisonResult > 0) {
|
||||
maxValue = currentValue;
|
||||
results.Clear();
|
||||
results.Add(currentItem);
|
||||
} else if (comparisonResult == 0) {
|
||||
results.Add(currentItem);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
/// <summary>
|
||||
/// 选择所有最小值对应的对象。
|
||||
/// 若没有元素则返回空列表。
|
||||
/// </summary>
|
||||
@@ -89,6 +114,31 @@ public static class EnumerableExtensions {
|
||||
}
|
||||
return results;
|
||||
}
|
||||
/// <summary>
|
||||
/// 选择所有最小值对应的对象。
|
||||
/// 若没有元素则返回空列表。
|
||||
/// </summary>
|
||||
public static List<TSource> MinByAll<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector, IComparer<TKey> comparer) {
|
||||
var results = new List<TSource>();
|
||||
using var enumerator = source.GetEnumerator();
|
||||
if (!enumerator.MoveNext()) return results;
|
||||
TSource minItem = enumerator.Current;
|
||||
TKey minValue = selector(minItem);
|
||||
results.Add(minItem);
|
||||
while (enumerator.MoveNext()) {
|
||||
TSource currentItem = enumerator.Current;
|
||||
TKey currentValue = selector(currentItem);
|
||||
int comparisonResult = comparer.Compare(currentValue, minValue);
|
||||
if (comparisonResult < 0) {
|
||||
minValue = currentValue;
|
||||
results.Clear();
|
||||
results.Add(currentItem);
|
||||
} else if (comparisonResult == 0) {
|
||||
results.Add(currentItem);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 选择最大值对应的对象。
|
||||
@@ -108,6 +158,23 @@ public static class EnumerableExtensions {
|
||||
return maxItem;
|
||||
}
|
||||
/// <summary>
|
||||
/// 选择最大值对应的对象。
|
||||
/// 若没有元素则返回 null。
|
||||
/// </summary>
|
||||
public static TSource? MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector, IComparer<TKey> comparer) {
|
||||
using var enumerator = source.GetEnumerator();
|
||||
if (!enumerator.MoveNext()) return default;
|
||||
TSource maxItem = enumerator.Current;
|
||||
TKey maxValue = selector(maxItem);
|
||||
while (enumerator.MoveNext()) {
|
||||
TKey value = selector(enumerator.Current);
|
||||
if (comparer.Compare(value, maxValue) <= 0) continue;
|
||||
maxItem = enumerator.Current;
|
||||
maxValue = value;
|
||||
}
|
||||
return maxItem;
|
||||
}
|
||||
/// <summary>
|
||||
/// 选择最小值对应的对象。
|
||||
/// 若没有元素则返回 null。
|
||||
/// </summary>
|
||||
@@ -124,6 +191,23 @@ public static class EnumerableExtensions {
|
||||
}
|
||||
return minItem;
|
||||
}
|
||||
/// <summary>
|
||||
/// 选择最小值对应的对象。
|
||||
/// 若没有元素则返回 null。
|
||||
/// </summary>
|
||||
public static TSource? MinBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector, IComparer<TKey> comparer) {
|
||||
using var enumerator = source.GetEnumerator();
|
||||
if (!enumerator.MoveNext()) { return default; }
|
||||
TSource minItem = enumerator.Current;
|
||||
TKey minValue = selector(minItem);
|
||||
while (enumerator.MoveNext()) {
|
||||
TKey value = selector(enumerator.Current);
|
||||
if (comparer.Compare(value, minValue) >= 0) { continue; }
|
||||
minItem = enumerator.Current;
|
||||
minValue = value;
|
||||
}
|
||||
return minItem;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
Reference in new issue
Block a user