56 lines
1.2 KiB
C#
56 lines
1.2 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
namespace PkmnLibSharp.Utilities
|
|
{
|
|
internal static class CollectionHelper
|
|
{
|
|
internal static ReadOnlyArray<T> ToReadOnly<T>(this IList<T> arr)
|
|
{
|
|
return new ReadOnlyArray<T>(arr);
|
|
}
|
|
}
|
|
|
|
public class ReadOnlyArray<T> : IReadOnlyList<T>
|
|
{
|
|
private readonly IList<T> _arr;
|
|
|
|
public ReadOnlyArray(IList<T> arr)
|
|
{
|
|
_arr = arr;
|
|
}
|
|
|
|
public IEnumerator<T> GetEnumerator()
|
|
{
|
|
return _arr.GetEnumerator();
|
|
}
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
{
|
|
return GetEnumerator();
|
|
}
|
|
|
|
public int Count => _arr.Count;
|
|
public T this[int index] => _arr[index];
|
|
|
|
public int IndexOf(T item)
|
|
{
|
|
return _arr.IndexOf(item);
|
|
}
|
|
|
|
public int IndexOf(Func<T, bool> predicate)
|
|
{
|
|
for (var index = 0; index < _arr.Count; index++)
|
|
{
|
|
var a = _arr[index];
|
|
if (predicate(a))
|
|
{
|
|
return index;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
}
|
|
} |