2022-09-21 17:40:04 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
|
|
|
namespace PkmnLibSharp.Utils
|
|
|
|
{
|
2023-01-03 12:57:29 +00:00
|
|
|
public sealed class DisposableCachedExternArray<T> : IReadOnlyList<T>, IDisposable
|
|
|
|
where T: class, IDisposable
|
|
|
|
{
|
|
|
|
private readonly T?[] _array;
|
|
|
|
private readonly Func<ulong, T> _getItem;
|
|
|
|
|
|
|
|
public DisposableCachedExternArray(ulong size, Func<ulong, T> getItem)
|
|
|
|
{
|
|
|
|
_array = new T?[(int)size];
|
|
|
|
_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 => _array.Length;
|
|
|
|
|
|
|
|
public T this[int index]
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (index >= _array.Length)
|
|
|
|
throw new ArgumentOutOfRangeException(
|
|
|
|
$"Index {index} was outside of the bounds of the external array with length {Count}");
|
|
|
|
return _array[index] ??= _getItem((ulong)index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal T? GetCachedValue(int i)
|
|
|
|
{
|
|
|
|
return _array[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
foreach (var item in _array)
|
|
|
|
{
|
|
|
|
item?.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public sealed class CachedExternArray<T> : IReadOnlyList<T>
|
2022-09-21 17:40:04 +00:00
|
|
|
where T: class
|
|
|
|
{
|
|
|
|
private readonly T?[] _array;
|
|
|
|
private readonly Func<ulong, T> _getItem;
|
|
|
|
|
|
|
|
public CachedExternArray(ulong size, Func<ulong, T> getItem)
|
|
|
|
{
|
|
|
|
_array = new T?[(int)size];
|
|
|
|
_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 => _array.Length;
|
|
|
|
|
|
|
|
public T this[int index]
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (index >= _array.Length)
|
|
|
|
throw new ArgumentOutOfRangeException(
|
|
|
|
$"Index {index} was outside of the bounds of the external array with length {Count}");
|
|
|
|
return _array[index] ??= _getItem((ulong)index);
|
|
|
|
}
|
|
|
|
}
|
2022-10-01 13:39:33 +00:00
|
|
|
|
|
|
|
internal T? GetCachedValue(int i)
|
|
|
|
{
|
|
|
|
return _array[i];
|
|
|
|
}
|
2022-09-21 17:40:04 +00:00
|
|
|
}
|
2023-01-03 12:57:29 +00:00
|
|
|
|
2022-09-21 17:40:04 +00:00
|
|
|
|
|
|
|
public class CachedExternValueArray<T> : IReadOnlyList<T>
|
|
|
|
where T: struct
|
|
|
|
{
|
|
|
|
private readonly T?[] _array;
|
|
|
|
private readonly Func<ulong, T> _getItem;
|
|
|
|
|
|
|
|
public CachedExternValueArray(ulong size, Func<ulong, T> getItem)
|
|
|
|
{
|
|
|
|
_array = new T?[(int)size];
|
|
|
|
_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 => _array.Length;
|
|
|
|
|
|
|
|
public T this[int index] => _array[index] ??= _getItem((ulong)index);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|