using NetWorkHelper.ICollections; using System; using System.Collections.Generic; namespace NetWorkHelper.Helper { public static class CollectionHelper { #region Find /// /// Find 从集合中选取符合条件的元素 /// public static List Find(IEnumerable source, Predicate predicate) { List list = new List(); CollectionHelper.ActionOnSpecification(source, delegate (TObject ele) { list.Add(ele); }, predicate); return list; } #endregion #region FindFirstSpecification /// /// FindFirstSpecification 返回符合条件的第一个元素 /// public static TObject FindFirstSpecification(IEnumerable source, Predicate predicate) { foreach (TObject element in source) { if (predicate(element)) { return element; } } return default(TObject); } #endregion #region ContainsSpecification /// /// ContainsSpecification 集合中是否包含满足predicate条件的元素。 /// public static bool ContainsSpecification(IEnumerable source, Predicate predicate, out TObject specification) { specification = default(TObject); foreach (TObject element in source) { if (predicate(element)) { specification = element; return true; } } return false; } /// /// ContainsSpecification 集合中是否包含满足predicate条件的元素。 /// public static bool ContainsSpecification(IEnumerable source, Predicate predicate) { TObject specification; return CollectionHelper.ContainsSpecification(source, predicate, out specification); } #endregion #region ActionOnSpecification /// /// ActionOnSpecification 对集合中满足predicate条件的元素执行action。如果没有条件,predicate传入null。 /// public static void ActionOnSpecification(IEnumerable collection, Action action, Predicate predicate) { if (collection == null) { return; } if (predicate == null) { foreach (TObject obj in collection) { action(obj); } return; } foreach (TObject obj in collection) { if (predicate(obj)) { action(obj); } } } #endregion #region ActionOnEach /// /// ActionOnEach 对集合中的每个元素执行action。 /// public static void ActionOnEach(IEnumerable collection, Action action) { CollectionHelper.ActionOnSpecification(collection, action, null); } #endregion #region GetPart public static T[] GetPart(T[] ary, int startIndex, int count) { return CollectionHelper.GetPart(ary, startIndex, count, false); } public static T[] GetPart(T[] ary, int startIndex, int count, bool reverse) { if (startIndex >= ary.Length) { return null; } if (ary.Length < startIndex + count) { count = ary.Length - startIndex; } T[] result = new T[count]; if (!reverse) { for (int i = 0; i < count; i++) { result[i] = ary[startIndex + i]; } } else { for (int i = 0; i < count; i++) { result[i] = ary[ary.Length - startIndex - 1 - i]; } } return result; } #endregion #region BinarySearch /// /// BinarySearch 从已排序的列表中,采用二分查找找到目标在列表中的位置。 /// 如果刚好有个元素与目标相等,则返回true,且minIndex会被赋予该元素的位置;否则,返回false,且minIndex会被赋予比目标小且最接近目标的元素的位置 /// public static bool BinarySearch(IList sortedList, T target, out int minIndex) where T : IComparable { if (target.CompareTo(sortedList[0]) == 0) { minIndex = 0; return true; } if (target.CompareTo(sortedList[0]) < 0) { minIndex = -1; return false; } if (target.CompareTo(sortedList[sortedList.Count - 1]) == 0) { minIndex = sortedList.Count - 1; return true; } if (target.CompareTo(sortedList[sortedList.Count - 1]) > 0) { minIndex = sortedList.Count - 1; return false; } int targetPosIndex = -1; int left = 0; int right = sortedList.Count - 1; while (right - left > 1) { int middle = (left + right) / 2; if (target.CompareTo(sortedList[middle]) == 0) { minIndex = middle; return true; } if (target.CompareTo(sortedList[middle]) < 0) { right = middle; } else { left = middle; } } minIndex = left; return false; } #endregion #region GetIntersection 、GetUnion /// /// GetIntersection 高效地求两个List元素的交集。 /// public static List GetIntersection(List list1, List list2) where T : IComparable { List largList = list1.Count > list2.Count ? list1 : list2; List smallList = largList == list1 ? list2 : list1; largList.Sort(); int minIndex = 0; List result = new List(); foreach (T tmp in smallList) { if (CollectionHelper.BinarySearch(largList, tmp, out minIndex)) { result.Add(tmp); } } return result; } /// /// GetUnion 高效地求两个List元素的并集。 /// public static List GetUnion(List list1, List list2) { SortedDictionary result = new SortedDictionary(); foreach (T tmp in list1) { if (!result.ContainsKey(tmp)) { result.Add(tmp, 0); } } foreach (T tmp in list2) { if (!result.ContainsKey(tmp)) { result.Add(tmp, 0); } } return (List)CollectionConverter.CopyAllToList(result.Keys); } #endregion } }