Browse Source

CollectionUtility:扩展方法 IEnumerable<T> 的 bool 返回值改为 JumpStatement。

master
王厅 1 month ago
parent
commit
6301818fb7
  1. 44
      Apewer/CollectionUtility.cs

44
Apewer/CollectionUtility.cs

@ -738,7 +738,7 @@ namespace Apewer
/// <typeparam name="TIn">输入的元素类型。</typeparam>
/// <typeparam name="TOut">输出的元素类型。</typeparam>
/// <param name="collection">要遍历的集合。</param>
/// <param name="selector">转换程序。</param>
/// <param name="selector">转换程序,提供索引参数(从 0 开始)。</param>
/// <returns>新数组的元素类型。</returns>
public static TOut[] Map<TIn, TOut>(this IEnumerable<TIn> collection, Func<TIn, int, TOut> selector)
{
@ -788,7 +788,7 @@ namespace Apewer
ForEach<T>(collection, (item, index) =>
{
callback.Invoke(item);
return true;
return JumpStatement.Continue;
});
}
@ -802,15 +802,15 @@ namespace Apewer
ForEach<T>(collection, (item, index) =>
{
callback.Invoke(item, index);
return true;
return JumpStatement.Continue;
});
}
/// <summary>遍历每个元素,执行回调。</summary>
/// <typeparam name="T">元素的类型。</typeparam>
/// <param name="collection">元素的集合。</param>
/// <param name="callback">回调。参数为本次遍历的元素。返回 true 将继续遍历,返回 false 打断遍历。</param>
public static void ForEach<T>(this IEnumerable<T> collection, Func<T, bool> callback)
/// <param name="callback">回调。参数为本次遍历的元素。</param>
public static void ForEach<T>(this IEnumerable<T> collection, Func<T, JumpStatement> callback)
{
if (collection == null || callback == null) return;
ForEach<T>(collection, (item, index) =>
@ -824,7 +824,7 @@ namespace Apewer
/// <typeparam name="T">元素的类型。</typeparam>
/// <param name="collection">元素的集合。</param>
/// <param name="callback">回调。参数为本次遍历的元素和遍历的索引值,索引值从 0 开始。返回 true 将继续遍历,返回 false 打断遍历。</param>
public static void ForEach<T>(this IEnumerable<T> collection, Func<T, int, bool> callback)
public static void ForEach<T>(this IEnumerable<T> collection, Func<T, int, JumpStatement> callback)
{
if (collection == null || callback == null) return;
@ -833,8 +833,8 @@ namespace Apewer
var count = array.Length;
for (var i = 0; i < count; i++)
{
var @continue = callback.Invoke(array[i], i);
if (!@continue) break;
var jump = callback.Invoke(array[i], i);
if (jump == JumpStatement.Break) break;
}
}
else
@ -842,8 +842,8 @@ namespace Apewer
var index = 0;
foreach (var item in collection)
{
var @continue = callback.Invoke(item, index);
if (!@continue) break;
var jump = callback.Invoke(item, index);
if (jump == JumpStatement.Break) break;
index += 1;
}
}
@ -854,6 +854,7 @@ namespace Apewer
/// <param name="collection">元素的集合。</param>
/// <param name="limit">每次遍历的元素数量。</param>
/// <param name="callback">回调。参数为本次遍历的元素和遍历的索引值,索引值从 0 开始。</param>
/// <exception cref="ArgumentOutOfRangeException" />
public static void ForEach<T>(this IEnumerable<T> collection, int limit, Action<T[]> callback)
{
if (collection == null || callback == null) return;
@ -861,7 +862,7 @@ namespace Apewer
ForEach<T>(collection, limit, (items, index) =>
{
callback.Invoke(items);
return true;
return JumpStatement.Continue;
});
}
@ -870,6 +871,7 @@ namespace Apewer
/// <param name="collection">元素的集合。</param>
/// <param name="limit">每次遍历的元素数量。</param>
/// <param name="callback">回调。参数为本次遍历的元素和遍历的索引值,索引值从 0 开始。</param>
/// <exception cref="ArgumentOutOfRangeException" />
public static void ForEach<T>(this IEnumerable<T> collection, int limit, Action<T[], int> callback)
{
if (collection == null || callback == null) return;
@ -877,7 +879,7 @@ namespace Apewer
ForEach<T>(collection, limit, (items, index) =>
{
callback.Invoke(items, index);
return true;
return JumpStatement.Continue;
});
}
@ -885,8 +887,9 @@ namespace Apewer
/// <typeparam name="T">元素的类型。</typeparam>
/// <param name="collection">元素的集合。</param>
/// <param name="limit">每次遍历的元素数量。</param>
/// <param name="callback">回调。参数为本次遍历的元素。返回 true 将继续遍历,返回 false 打断遍历。</param>
public static void ForEach<T>(this IEnumerable<T> collection, int limit, Func<T[], bool> callback)
/// <param name="callback">回调。参数为本次遍历的元素。</param>
/// <exception cref="ArgumentOutOfRangeException" />
public static void ForEach<T>(this IEnumerable<T> collection, int limit, Func<T[], JumpStatement> callback)
{
if (collection == null || callback == null) return;
@ -901,8 +904,9 @@ namespace Apewer
/// <typeparam name="T">元素的类型。</typeparam>
/// <param name="collection">元素的集合。</param>
/// <param name="limit">每次遍历的元素数量。</param>
/// <param name="callback">回调。参数为本次遍历的元素和遍历的索引值,索引值从 0 开始。返回 true 将继续遍历,返回 false 打断遍历。</param>
public static void ForEach<T>(this IEnumerable<T> collection, int limit, Func<T[], int, bool> callback)
/// <param name="callback">回调。参数为本次遍历的元素和遍历的索引值,索引值从 0 开始。</param>
/// <exception cref="ArgumentOutOfRangeException" />
public static void ForEach<T>(this IEnumerable<T> collection, int limit, Func<T[], int, JumpStatement> callback)
{
if (limit < 1) throw new ArgumentOutOfRangeException(nameof(limit));
if (collection == null) return;
@ -920,8 +924,8 @@ namespace Apewer
if (start + length > total) length = total - start;
var temp = new T[length];
System.Array.Copy(array, start, temp, 0L, length);
var @continue = callback.Invoke(temp, index);
if (!@continue) break;
var jump = callback.Invoke(temp, index);
if (jump == JumpStatement.Break) break;
start += length;
index += 1;
@ -938,8 +942,8 @@ namespace Apewer
while (list.Count < limit && queue.Count > 0) list.Add(queue.Dequeue());
var temp = list.ToArray();
var @continue = callback.Invoke(temp, index);
if (!@continue) break;
var jump = callback.Invoke(temp, index);
if (jump == JumpStatement.Break) break;
index += 1;
}

Loading…
Cancel
Save