diff --git a/Yafc.Model/Data/SortedList.cs b/Yafc.Model/Data/SortedList.cs deleted file mode 100644 index 5042ee8e..00000000 --- a/Yafc.Model/Data/SortedList.cs +++ /dev/null @@ -1,144 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; - -namespace Yafc.Model { - // Simple set with array as backing storage with O(ln(n)) search, O(n) insertion and iteration - public class SortedList(IComparer comparer) : ICollection, IReadOnlyList, IList { - private readonly IComparer comparer = comparer; - private int version; - private T[] data = []; - IEnumerator IEnumerable.GetEnumerator() { - return GetEnumerator(); - } - - IEnumerator IEnumerable.GetEnumerator() { - return GetEnumerator(); - } - - public Enumerator GetEnumerator() { - return new Enumerator(this); - } - - public struct Enumerator(SortedList list) : IEnumerator { - private readonly SortedList list = list; - private int index = -1; - private int version = list.version; - - public bool MoveNext() { - if (list.version != version) { - Throw(); - } - - if (++index >= list.Count) { - Current = default; - return false; - } - - Current = list.data[index]; - return true; - } - - private static void Throw() { - throw new InvalidOperationException("Collection was modified, enumeration cannot continue"); - } - - public void Reset() { - index = -1; - version = list.version; - } - - public T Current { get; private set; } = default; - - readonly object IEnumerator.Current => Current; - public readonly void Dispose() { } - } - - - public void Add(T item) { - if (item == null) { - throw new NullReferenceException(); - } - - int index = Array.BinarySearch(data, 0, Count, item, comparer); - if (index >= 0) { - return; - } - - index = ~index; - if (Count == data.Length) { - Array.Resize(ref data, Math.Max(data.Length * 2, 4)); - } - - if (index < Count) { - Array.Copy(data, index, data, index + 1, Count - index); - } - - data[index] = item; - ++version; - ++Count; - } - - public void Clear() { - Array.Clear(data, 0, Count); - ++version; - Count = 0; - } - - public bool Contains(T item) { - if (item == null) { - throw new NullReferenceException(); - } - - return Array.BinarySearch(data, 0, Count, item, comparer) >= 0; - } - - public void CopyTo(T[] array, int arrayIndex) { - Array.Copy(data, 0, array, arrayIndex, Count); - } - - public bool Remove(T item) { - if (item == null) { - throw new NullReferenceException(); - } - - int index = Array.BinarySearch(data, 0, Count, item, comparer); - if (index < 0) { - return false; - } - - RemoveAt(index); - return true; - } - - public int Count { get; private set; } - public bool IsReadOnly => false; - - public int IndexOf(T item) { - if (item == null) { - throw new NullReferenceException(); - } - - int index = Array.BinarySearch(data, 0, Count, item, comparer); - return index < 0 ? -1 : index; - } - - public void Insert(int index, T item) { - throw new NotSupportedException(); - } - - public void RemoveAt(int index) { - if (index < Count - 1) { - Array.Copy(data, index + 1, data, index, Count - index - 1); - } - - ++version; - --Count; - } - - public T this[int index] { - get => ((uint)index) < Count ? data[index] : throw new ArgumentOutOfRangeException(nameof(index)); - set => throw new NotSupportedException(); - } - } -}