PkmnLibRSharp/PkmnLibRSharp/Utils/ExternValueArray.cs

35 lines
846 B
C#

using System;
using System.Collections;
using System.Collections.Generic;
namespace PkmnLibSharp.Utils
{
public class ExternValueArray<T> : IReadOnlyList<T> where T : struct
{
private readonly Func<ulong> _getLength;
private readonly Func<ulong, T> _getItem;
public ExternValueArray(Func<ulong> getSize, Func<ulong, T> getItem)
{
_getLength = getSize;
_getItem = getItem;
}
public IEnumerator<T> GetEnumerator()
{
for (var i = 0; i < Count; i++)
{
yield return this[i];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public int Count => (int)_getLength();
public T this[int index] => _getItem((ulong)index);
}
}