| | | 1 | | #nullable enable |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using Hertzole.ScriptableValues.Helpers; |
| | | 7 | | using UnityEngine.Pool; |
| | | 8 | | |
| | | 9 | | namespace Hertzole.ScriptableValues |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Internal reusable collection enumerator for collections. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <typeparam name="T">The type of hte items in the collection.</typeparam> |
| | | 15 | | internal sealed class CollectionEnumerator<T> : ICollection<T>, IDisposable |
| | | 16 | | { |
| | | 17 | | private T[]? currentArray; |
| | | 18 | | |
| | 6 | 19 | | internal static readonly ObjectPool<CollectionEnumerator<T>> enumeratorPool = |
| | 6 | 20 | | new ObjectPool<CollectionEnumerator<T>>(static () => new CollectionEnumerator<T>()); |
| | | 21 | | |
| | 5256 | 22 | | public int Count { get; private set; } |
| | | 23 | | public bool IsReadOnly |
| | | 24 | | { |
| | 0 | 25 | | get { return true; } |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | public void SetArray(T[] newArray, int newCount) |
| | 1314 | 29 | | { |
| | 1314 | 30 | | currentArray = newArray; |
| | 1314 | 31 | | Count = newCount; |
| | 1314 | 32 | | } |
| | | 33 | | |
| | | 34 | | public void Dispose() |
| | 1314 | 35 | | { |
| | 1314 | 36 | | currentArray = null; |
| | 1314 | 37 | | Count = 0; |
| | 1314 | 38 | | enumeratorPool.Release(this); |
| | 1314 | 39 | | } |
| | | 40 | | |
| | | 41 | | public IEnumerator<T> GetEnumerator() |
| | 0 | 42 | | { |
| | 0 | 43 | | if (currentArray == null || Count == 0) |
| | 0 | 44 | | { |
| | 0 | 45 | | yield break; |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | for (int i = 0; i < Count; i++) |
| | 0 | 49 | | { |
| | 0 | 50 | | yield return currentArray[i]; |
| | 0 | 51 | | } |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | IEnumerator IEnumerable.GetEnumerator() |
| | 0 | 55 | | { |
| | 0 | 56 | | return GetEnumerator(); |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | public void Add(T item) |
| | 0 | 60 | | { |
| | 0 | 61 | | throw new NotSupportedException(); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | public void Clear() |
| | 0 | 65 | | { |
| | 0 | 66 | | throw new NotSupportedException(); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | public bool Contains(T item) |
| | 0 | 70 | | { |
| | 0 | 71 | | throw new NotSupportedException(); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | public void CopyTo(T[] array, int arrayIndex) |
| | 1314 | 75 | | { |
| | 1314 | 76 | | Guard.IsNotNull(currentArray, nameof(currentArray)); |
| | | 77 | | |
| | 1314 | 78 | | Array.Copy(currentArray, 0, array, arrayIndex, Count); |
| | 1314 | 79 | | } |
| | | 80 | | |
| | | 81 | | public bool Remove(T item) |
| | 0 | 82 | | { |
| | 0 | 83 | | throw new NotSupportedException(); |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | } |