5 Commits

  1. 20
      Apewer/AfterBehavior.cs
  2. 84
      Apewer/CollectionUtility.cs
  3. 26
      Apewer/JumpStatement.cs

20
Apewer/AfterBehavior.cs

@ -1,20 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Apewer
{
/// <summary>后续动作。</summary>
public enum AfterBehavior : int
{
/// <summary>打断。</summary>
Break = 0,
/// <summary>继续。</summary>
Continue = 1
}
}

84
Apewer/CollectionUtility.cs

@ -731,13 +731,27 @@ namespace Apewer
if (selector == null) throw new ArgumentNullException(nameof(selector));
if (collection == null) return new TOut[0];
return Map(collection, (item, index) => selector.Invoke(item));
}
/// <summary>遍历集合,转换元素,生成新数组。</summary>
/// <typeparam name="TIn">输入的元素类型。</typeparam>
/// <typeparam name="TOut">输出的元素类型。</typeparam>
/// <param name="collection">要遍历的集合。</param>
/// <param name="selector">转换程序,提供索引参数(从 0 开始)。</param>
/// <returns>新数组的元素类型。</returns>
public static TOut[] Map<TIn, TOut>(this IEnumerable<TIn> collection, Func<TIn, int, TOut> selector)
{
if (selector == null) throw new ArgumentNullException(nameof(selector));
if (collection == null) return new TOut[0];
if (collection is TIn[] array)
{
var count = array.Length;
var result = new TOut[count];
for (var i = 0; i < count; i++)
{
result[i] = selector.Invoke(array[i]);
result[i] = selector.Invoke(array[i], i);
}
return result;
}
@ -753,7 +767,7 @@ namespace Apewer
capacity += 1024;
list.Capacity += capacity;
}
list.Add(selector.Invoke(item));
list.Add(selector.Invoke(item, count));
count += 1;
}
return list.ToArray();
@ -774,7 +788,7 @@ namespace Apewer
ForEach<T>(collection, (item, index) =>
{
callback.Invoke(item);
return true;
return JumpStatement.Continue;
});
}
@ -788,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) =>
@ -810,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;
@ -819,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
@ -828,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;
}
}
@ -840,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;
@ -847,7 +862,7 @@ namespace Apewer
ForEach<T>(collection, limit, (items, index) =>
{
callback.Invoke(items);
return true;
return JumpStatement.Continue;
});
}
@ -856,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;
@ -863,7 +879,7 @@ namespace Apewer
ForEach<T>(collection, limit, (items, index) =>
{
callback.Invoke(items, index);
return true;
return JumpStatement.Continue;
});
}
@ -871,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;
@ -887,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;
@ -906,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;
@ -924,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;
}
@ -934,6 +952,30 @@ namespace Apewer
#endregion
#region Join
/// <summary>连接多个集合的元素为单个集合。</summary>
public static T[] Join<T>(params IEnumerable<T>[] collections)
{
if (collections == null) return new T[0];
var count = collections.Length;
if (count < 1) return new T[0];
var result = new List<T>();
foreach (var collection in collections)
{
if (collection == null) continue;
foreach (var item in collection)
{
result.Add(item);
}
}
return result.ToArray();
}
#endregion
}
}

26
Apewer/JumpStatement.cs

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Apewer
{
/// <summary>跳转语句。</summary>
public enum JumpStatement : int
{
/// <summary>终止最接近的迭代。</summary>
Break = 0,
/// <summary>启动最接近的迭代。</summary>
Continue = 1,
// /// <summary>终止所在函数的执行,并将控制权和函数结果(若有)返回给调用方。</summary>
// Return = 2,
// /// <summary>将控制权转交给带有标签的语句。</summary>
// Goto = 3,
}
}
Loading…
Cancel
Save