feat: use new IEnumeratable type

This commit is contained in:
2025-11-18 21:56:50 +08:00
parent 9d41119710
commit 334accd070
3 changed files with 91 additions and 19 deletions

View File

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BallanceTasEditor.Utils {
/// <summary>
/// 一种提前给定元素个数的的IEnumerable。
/// </summary>
public sealed class CountableEnumerable<T> {
/// <summary>
/// 以直接方式构建。
/// </summary>
/// <param name="enumerable">一个迭代器,其最多只能迭代给定次数。</param>
/// <param name="count">迭代器会迭代的次数。</param>
public CountableEnumerable(IEnumerable<T> enumerable, int count) {
m_Inner = enumerable;
m_Count = count;
}
/// <summary>
/// 从数组便捷构建。
/// </summary>
/// <param name="array">要使用的数组。</param>
public CountableEnumerable(T[] array) {
m_Inner = array;
m_Count = array.Length;
}
private IEnumerable<T> m_Inner;
private int m_Count;
/// <summary>
/// 获取迭代器对象。
/// </summary>
/// <returns>用于迭代的迭代器。</returns>
/// <exception cref="ArgumentException">当迭代器迭代次数与给定次数不匹配时。</exception>
public IEnumerable<T> GetInner() {
int counter = 0;
foreach (var item in m_Inner) {
if (counter >= m_Count) {
throw new ArgumentException("Given IEnumerable<T> is not stopped at given count.");
} else {
yield return item;
++counter;
}
}
if (counter != m_Count) {
throw new ArgumentException("Given IEnumerable<T> is not stopped at given count.");
}
}
/// <summary>
/// 获取该迭代器会迭代的次数。
/// </summary>
/// <returns>迭代器会迭代的次数,用于给使用该结构的方法提前分配必要的空间。</returns>
public int GetCount() {
return m_Count;
}
}
}

View File

@ -30,7 +30,7 @@ namespace BallanceTasEditor.Utils {
/// <param name="index">要在前方插入数据的元素的索引。</param> /// <param name="index">要在前方插入数据的元素的索引。</param>
/// <param name="items">要插入的元素的迭代器。</param> /// <param name="items">要插入的元素的迭代器。</param>
/// <exception cref="ArgumentException">给定的索引超出范围。</exception> /// <exception cref="ArgumentException">给定的索引超出范围。</exception>
void Insert(int index, IEnumerable<T> items); void Insert(int index, CountableEnumerable<T> items);
/// <summary> /// <summary>
/// 从给定单元开始,移除给定个数的元素。 /// 从给定单元开始,移除给定个数的元素。
/// </summary> /// </summary>
@ -70,7 +70,7 @@ namespace BallanceTasEditor.Utils {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void Insert(int index, IEnumerable<T> items) { public void Insert(int index, CountableEnumerable<T> items) {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -108,8 +108,8 @@ namespace BallanceTasEditor.Utils {
return m_Container[index]; return m_Container[index];
} }
public void Insert(int index, IEnumerable<T> items) { public void Insert(int index, CountableEnumerable<T> items) {
m_Container.InsertRange(index, items); m_Container.InsertRange(index, items.GetInner());
} }
public void Remove(int index, int count) { public void Remove(int index, int count) {
@ -219,11 +219,11 @@ namespace BallanceTasEditor.Utils {
} }
} }
public void Insert(int index, IEnumerable<T> items) { public void Insert(int index, CountableEnumerable<T> items) {
if (index < 0 || index > GetCount()) { if (index < 0 || index > GetCount()) {
throw new ArgumentOutOfRangeException("Index out of range."); throw new ArgumentOutOfRangeException("Index out of range.");
} else if (index == GetCount()) { } else if (index == GetCount()) {
foreach (T item in items) { foreach (T item in items.GetInner()) {
m_Container.AddLast(item); m_Container.AddLast(item);
} }
@ -234,7 +234,7 @@ namespace BallanceTasEditor.Utils {
MoveToIndex(index); MoveToIndex(index);
int count = 0; int count = 0;
foreach (T item in items) { foreach (T item in items.GetInner()) {
m_Container.AddBefore(m_Cursor, item); m_Container.AddBefore(m_Cursor, item);
++count; ++count;
} }

View File

@ -13,6 +13,14 @@ namespace BallanceTasEditorTests.Utils {
private static readonly int[] BLANK = { }; private static readonly int[] BLANK = { };
private static readonly int[] PROBE = { 10, 20, 30, 40, 50 }; private static readonly int[] PROBE = { 10, 20, 30, 40, 50 };
private static CountableEnumerable<int> GetCountableProbe() {
return new CountableEnumerable<int>(PROBE);
}
private static CountableEnumerable<int> GetCountableBlank() {
return new CountableEnumerable<int>(BLANK);
}
private static IEnumerable<object[]> TasStorageInstanceProvider { private static IEnumerable<object[]> TasStorageInstanceProvider {
get { get {
yield return new object[] { new ListTasStorage<int>() }; yield return new object[] { new ListTasStorage<int>() };
@ -34,7 +42,7 @@ namespace BallanceTasEditorTests.Utils {
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(1)); AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(1));
// 设置数据 // 设置数据
storage.Insert(0, PROBE); storage.Insert(0, GetCountableProbe());
// 访问数据 // 访问数据
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(-1)); AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(-1));
for (int i = 0; i < PROBE.Length; i++) { for (int i = 0; i < PROBE.Length; i++) {
@ -53,9 +61,9 @@ namespace BallanceTasEditorTests.Utils {
// 和在非空时的头,中,尾分别插入的结果。 // 和在非空时的头,中,尾分别插入的结果。
// 先检测空插入 // 先检测空插入
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Insert(-1, PROBE)); AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Insert(-1, GetCountableProbe()));
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Insert(1, PROBE)); AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Insert(1, GetCountableProbe()));
storage.Insert(0, PROBE); storage.Insert(0, GetCountableProbe());
for (int i = 0; i < PROBE.Length; i++) { for (int i = 0; i < PROBE.Length; i++) {
Assert.AreEqual(storage.Visit(i), PROBE[i]); Assert.AreEqual(storage.Visit(i), PROBE[i]);
} }
@ -65,8 +73,8 @@ namespace BallanceTasEditorTests.Utils {
foreach (var index in indices) { foreach (var index in indices) {
// 清空,一次插入,然后二次插入 // 清空,一次插入,然后二次插入
storage.Clear(); storage.Clear();
storage.Insert(0, PROBE); storage.Insert(0, GetCountableProbe());
storage.Insert(index, PROBE); storage.Insert(index, GetCountableProbe());
// 用List做正确模拟 // 用List做正确模拟
var expected = new List<int>(); var expected = new List<int>();
@ -96,7 +104,7 @@ namespace BallanceTasEditorTests.Utils {
foreach (var index in indices) { foreach (var index in indices) {
// 清空,插入,删除 // 清空,插入,删除
storage.Clear(); storage.Clear();
storage.Insert(0, PROBE); storage.Insert(0, GetCountableProbe());
storage.Remove(index, 1); storage.Remove(index, 1);
// 用List做正确模拟 // 用List做正确模拟
@ -119,7 +127,7 @@ namespace BallanceTasEditorTests.Utils {
[DynamicData(nameof(TasStorageInstanceProvider))] [DynamicData(nameof(TasStorageInstanceProvider))]
public void ClearTest(ITasStorage<int> storage) { public void ClearTest(ITasStorage<int> storage) {
// 设置数据后清空 // 设置数据后清空
storage.Insert(0, PROBE); storage.Insert(0, GetCountableProbe());
storage.Clear(); storage.Clear();
// 检查是否为空 // 检查是否为空
@ -136,7 +144,7 @@ namespace BallanceTasEditorTests.Utils {
Assert.IsTrue(storage.IsEmpty()); Assert.IsTrue(storage.IsEmpty());
// 插入数据后再检查 // 插入数据后再检查
storage.Insert(0, PROBE); storage.Insert(0, GetCountableProbe());
Assert.IsFalse(storage.IsEmpty()); Assert.IsFalse(storage.IsEmpty());
} }
@ -150,7 +158,7 @@ namespace BallanceTasEditorTests.Utils {
Assert.AreEqual(storage.GetCount(), 0); Assert.AreEqual(storage.GetCount(), 0);
// 插入数据后再检查 // 插入数据后再检查
storage.Insert(0, PROBE); storage.Insert(0, GetCountableProbe());
Assert.AreEqual(storage.GetCount(), PROBE.Length); Assert.AreEqual(storage.GetCount(), PROBE.Length);
} }
@ -166,7 +174,7 @@ namespace BallanceTasEditorTests.Utils {
Assert.AreEqual(storage.GetCount(), 0); Assert.AreEqual(storage.GetCount(), 0);
// 设置内容 // 设置内容
storage.Insert(0, PROBE); storage.Insert(0, GetCountableProbe());
// 并再次检查大小 // 并再次检查大小
Assert.IsFalse(storage.IsEmpty()); Assert.IsFalse(storage.IsEmpty());
Assert.AreEqual(storage.GetCount(), PROBE.Length); Assert.AreEqual(storage.GetCount(), PROBE.Length);
@ -185,7 +193,7 @@ namespace BallanceTasEditorTests.Utils {
// 清空后插入0项然后确认 // 清空后插入0项然后确认
storage.Clear(); storage.Clear();
storage.Insert(0, BLANK); storage.Insert(0, GetCountableBlank());
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(-1)); AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(-1));
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(0)); AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(0));
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(1)); AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(1));