123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- namespace Animancer
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public struct FastEnumerator<T> : IList<T>, IEnumerator<T>
- {
-
-
- private readonly IList<T> List;
-
- private int _Count;
-
-
-
-
- public int Count
- {
- get => _Count;
- set
- {
- AssertCount(value);
- _Count = value;
- }
- }
-
- private int _Index;
-
- public int Index
- {
- get => _Index;
- set
- {
- AssertIndex(value);
- _Index = value;
- }
- }
-
-
- public T Current
- {
- get
- {
- AssertCount(_Count);
- AssertIndex(_Index);
- return List[_Index];
- }
- set
- {
- AssertCount(_Count);
- AssertIndex(_Index);
- List[_Index] = value;
- }
- }
-
- object IEnumerator.Current => Current;
-
-
-
-
-
- public FastEnumerator(IList<T> list)
- : this(list, list.Count)
- { }
-
-
-
-
- public FastEnumerator(IList<T> list, int count)
- {
- List = list;
- _Count = count;
- _Index = -1;
- AssertCount(count);
- }
-
-
-
- public bool MoveNext()
- {
- _Index++;
- if ((uint)_Index < (uint)_Count)
- {
- return true;
- }
- else
- {
- _Index = int.MinValue;
- return false;
- }
- }
-
-
-
- public bool MovePrevious()
- {
- if (_Index > 0)
- {
- _Index--;
- return true;
- }
- else
- {
- _Index = -1;
- return false;
- }
- }
-
-
- public void Reset()
- {
- _Index = -1;
- }
-
-
- void IDisposable.Dispose() { }
-
-
-
-
- public FastEnumerator<T> GetEnumerator() => this;
-
- IEnumerator<T> IEnumerable<T>.GetEnumerator() => this;
-
- IEnumerator IEnumerable.GetEnumerator() => this;
-
-
-
-
- public int IndexOf(T item) => List.IndexOf(item);
-
- public T this[int index]
- {
- get
- {
- AssertIndex(index);
- return List[index];
- }
- set
- {
- AssertIndex(index);
- List[index] = value;
- }
- }
-
- public void Insert(int index, T item)
- {
- AssertIndex(index);
- List.Insert(index, item);
- if (_Index >= index)
- _Index++;
- _Count++;
- }
-
- public void RemoveAt(int index)
- {
- AssertIndex(index);
- List.RemoveAt(index);
- if (_Index >= index)
- _Index--;
- _Count--;
- }
-
-
-
-
- public bool IsReadOnly => List.IsReadOnly;
-
- public bool Contains(T item) => List.Contains(item);
-
- public void Add(T item)
- {
- List.Add(item);
- _Count++;
- }
-
- public bool Remove(T item)
- {
- var index = List.IndexOf(item);
- if (index >= 0)
- {
- RemoveAt(index);
- return true;
- }
- else return false;
- }
-
- public void Clear()
- {
- List.Clear();
- _Index = -1;
- _Count = 0;
- }
-
- public void CopyTo(T[] array, int arrayIndex)
- {
- for (int i = 0; i < _Count; i++)
- array[arrayIndex + i] = List[i];
- }
-
-
-
- [System.Diagnostics.Conditional(Strings.Assertions)]
- private void AssertIndex(int index)
- {
- #if UNITY_ASSERTIONS
- if ((uint)index > (uint)_Count)
- throw new ArgumentOutOfRangeException(nameof(index),
- $"{nameof(FastEnumerator<T>)}.{nameof(Index)}" +
- $" must be within 0 <= {nameof(Index)} ({index}) < {nameof(Count)} ({_Count}).");
- #endif
- }
-
-
-
- [System.Diagnostics.Conditional(Strings.Assertions)]
- private void AssertCount(int count)
- {
- #if UNITY_ASSERTIONS
- if (List == null)
- {
- if (count != 0)
- throw new ArgumentOutOfRangeException(nameof(count),
- $"Must be within 0 since the {nameof(List)} is null.");
- }
- else
- {
- if ((uint)count > (uint)List.Count)
- throw new ArgumentOutOfRangeException(nameof(count),
- $"Must be within 0 <= {nameof(count)} ({count}) < {nameof(List)}.{nameof(List.Count)} ({List.Count}).");
- }
- #endif
- }
-
- }
- }
|