From 935a4c8dbbd61228c7ff1f65c57842dc74dcc20a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com> Date: Sun, 19 Apr 2026 22:25:05 +0800 Subject: [PATCH] =?UTF-8?q?ref:=20=E5=88=9D=E6=AD=A5=E8=BF=81=E7=A7=BB?= =?UTF-8?q?=E9=83=A8=E5=88=86=E6=9E=9A=E4=B8=BE=E6=89=A9=E5=B1=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Extensions/Enumerable.cs | 142 ++++++++++++++++++ General/Extensions.cs => Extensions/String.cs | 10 +- General/PathUtils.cs | 3 +- 3 files changed, 147 insertions(+), 8 deletions(-) create mode 100644 Extensions/Enumerable.cs rename General/Extensions.cs => Extensions/String.cs (93%) diff --git a/Extensions/Enumerable.cs b/Extensions/Enumerable.cs new file mode 100644 index 0000000..25fcf6a --- /dev/null +++ b/Extensions/Enumerable.cs @@ -0,0 +1,142 @@ +using System; +using System.Collections.Generic; + +namespace MeloongCore.Extensions { + public static class EnumerableExtensions { + + /// + /// 去重。 + /// + public static List Distinct(this IList Arr, Func IsEqual) { + var ResultArray = new List(); + for (int i = 0; i <= Arr.Count - 1; i++) { + for (int ii = i + 1; ii <= Arr.Count - 1; ii++) { + if (IsEqual(Arr[i], Arr[ii])) { goto NextElement; } + } + ResultArray.Add(Arr[i]); +NextElement:; + } + return ResultArray; + } + + /// + /// 判断这是否是仅有一个元素的集合。 + /// + public static bool IsSingle(this IEnumerable Source) { + if (Source == null) { + return false; + } else if (Source is IList list) { + return list.Count == 1; + } else { + using (var Enumerator = Source.GetEnumerator()) { + if (!Enumerator.MoveNext()) { return false; } + if (Enumerator.MoveNext()) { return false; } + return true; + } + } + } + + /// + /// 对集合的每个元素执行指定操作。 + /// + public static IEnumerable ForEach(this IEnumerable Collection, Action Action) { + foreach (T Item in Collection) { + Action(Item); + } + return Collection; + } + + /// + /// 选择所有最大值对应的对象。 + /// 若没有元素则返回空列表。 + /// + public static List MaxByAll(this IEnumerable Source, Func Selector) where C : IComparable { + var Results = new List(); + using (var Enumerator = Source.GetEnumerator()) { + if (!Enumerator.MoveNext()) { return Results; } + T MaxItem = Enumerator.Current; + C MaxValue = Selector(MaxItem); + Results.Add(MaxItem); + while (Enumerator.MoveNext()) { + T CurrentItem = Enumerator.Current; + C CurrentValue = Selector(CurrentItem); + int Comparison = CurrentValue.CompareTo(MaxValue); + if (Comparison > 0) { + MaxValue = CurrentValue; + Results.Clear(); + Results.Add(CurrentItem); + } else if (Comparison == 0) { + Results.Add(CurrentItem); + } + } + } + return Results; + } + + /// + /// 选择所有最小值对应的对象。 + /// 若没有元素则返回空列表。 + /// + public static List MinByAll(this IEnumerable List, Func Selector) where C : IComparable { + var Results = new List(); + using (var Enumerator = List.GetEnumerator()) { + if (!Enumerator.MoveNext()) { return Results; } + T MinItem = Enumerator.Current; + C MinValue = Selector(MinItem); + Results.Add(MinItem); + while (Enumerator.MoveNext()) { + T CurrentItem = Enumerator.Current; + C CurrentValue = Selector(CurrentItem); + int Comparison = CurrentValue.CompareTo(MinValue); + if (Comparison < 0) { + MinValue = CurrentValue; + Results.Clear(); + Results.Add(CurrentItem); + } else if (Comparison == 0) { + Results.Add(CurrentItem); + } + } + } + return Results; + } + + /// + /// 选择最大值对应的对象。 + /// 若没有元素则返回 Nothing。 + /// + public static T MaxBy(this IEnumerable Source, Func Selector) where C : IComparable { + using (var Enumerator = Source.GetEnumerator()) { + if (!Enumerator.MoveNext()) { return default(T); } + T MaxItem = Enumerator.Current; + C MaxValue = Selector(MaxItem); + while (Enumerator.MoveNext()) { + C Value = Selector(Enumerator.Current); + if (Value.CompareTo(MaxValue) <= 0) { continue; } + MaxItem = Enumerator.Current; + MaxValue = Value; + } + return MaxItem; + } + } + + /// + /// 选择最小值对应的对象。 + /// 若没有元素则返回 Nothing。 + /// + public static T MinBy(this IEnumerable List, Func Selector) where C : IComparable { + using (var Enumerator = List.GetEnumerator()) { + if (!Enumerator.MoveNext()) { return default; } + T MinItem = Enumerator.Current; + C MinValue = Selector(MinItem); + while (Enumerator.MoveNext()) { + C Value = Selector(Enumerator.Current); + if (Value.CompareTo(MinValue) >= 0) { continue; } + MinItem = Enumerator.Current; + MinValue = Value; + } + return MinItem; + } + } + + } +} \ No newline at end of file diff --git a/General/Extensions.cs b/Extensions/String.cs similarity index 93% rename from General/Extensions.cs rename to Extensions/String.cs index 6c5cd73..95169b3 100644 --- a/General/Extensions.cs +++ b/Extensions/String.cs @@ -1,10 +1,8 @@ using System; using System.Runtime.CompilerServices; -namespace MeloongCore { - public static class Extensions { - - #region String +namespace MeloongCore.Extensions { + public static class StringExtensions { /// /// 高速的 StartsWith。 @@ -28,7 +26,7 @@ namespace MeloongCore { /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool ContainsIgnoreCase(this string value, string subString) { - return value.IndexOf(subString,StringComparison.OrdinalIgnoreCase) >= 0; + return value.IndexOf(subString, StringComparison.OrdinalIgnoreCase) >= 0; } /// @@ -61,7 +59,5 @@ namespace MeloongCore { return value.LastIndexOf(subString, startIndex, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); } - #endregion - } } diff --git a/General/PathUtils.cs b/General/PathUtils.cs index 3a7b847..1f33a23 100644 --- a/General/PathUtils.cs +++ b/General/PathUtils.cs @@ -1,4 +1,5 @@ -using System.IO; +using MeloongCore.Extensions; +using System.IO; using System.Runtime.InteropServices; using System.Text;