///
/// 自定义泛型可迭代类型
///
///
/// This code shows how to build a instance of :
///
/// var enumerable = new SelfEnumerablePerson5);
/// enumerable.Add(new Person() { Name = "xfh", Age = 27 });
///
///
///
/// 灵感来自:
/// The foreach statement executes a statement or a block of statements for each element in an instance of the type
/// that implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable interface.
/// The foreach statement is not limited to those types and can be applied to an instance of any type that satisfies the following cond
/// 1. has the public parameterless GetEnumerator method whose return type is either class, struct, or interface type,
/// 2. the return type of the GetEnumerator method has the public Current property and the public parameterless MoveNext metho
/// whose return type is Boolean.
/// 参考了:
/// https://github.com/dotnet/corefx/blob/master/src/Common/src/CoreLib/System/Span.cs
/// https://docs.microsoft.com/en-us/dotnet/csharp/iterators
/// https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/foreach-in
/// https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/yield
///
public class SelfEnumerable
{
private readonly int _capacity;
private T[] _innerArray;
private int _addIndex = 0;
public SelfEnumerable(int capacity)
{
_capacity = capacity;
_innerArray = new T[capacity];
}
///
/// 可迭代对象中存储的对象数
///
/// auto-property
public int Count { get; private set; } = 0;
public T this[int index] => _innerArray[index];
///
/// 向可迭代对象中添加元素
///
/// 当添加的元素数超过的值时,新添加的元素会覆盖之前的值
public void Add(T item)
{
if (_addIndex > _capacity - 1)
{
_addIndex = 0;
}
else
{
Count++;
}
_innerArray[_addIndex] = item;
_addIndex++;
}
public SelfEnumerator GetEnumerator() => new SelfEnumerator(this);
public class SelfEnumerator
{
private readonly SelfEnumerable _selfEnumerable;
private int _seekIndex = -1;
internal SelfEnumerator(SelfEnumerable selfEnumerable)
{
_selfEnumerable = selfEnumerable;
}
public T Current => _selfEnumerable[_seekIndex];
public bool MoveNext()
{
_seekIndex++;
if (_seekIndex > _selfEnumerable.Count - 1)
{
return false;
}
return true;
}
}
}