diff --git a/Engine.Core/Helpers/FastList.cs b/Engine.Core/Helpers/FastList.cs index 89b11b3..32c486b 100644 --- a/Engine.Core/Helpers/FastList.cs +++ b/Engine.Core/Helpers/FastList.cs @@ -8,13 +8,25 @@ public class FastList : IList, IReadOnlyList, IEnumerable where T : private readonly List items = []; private readonly Dictionary indexMap = []; - public int Count => items.Count; - public bool IsReadOnly { get; set; } = false; - public T this[int index] { get => items[index]; set => items[index] = value; } + public int Count => items.Count; + public T this[int index] + { + get => items[index]; + set + { + if (IsReadOnly) + throw new System.Data.ReadOnlyException(); + + items[index] = value; + } + } public void Add(T item) { + if (IsReadOnly) + throw new System.Data.ReadOnlyException(); + indexMap[item] = items.Count; items.Add(item); } @@ -22,6 +34,9 @@ public class FastList : IList, IReadOnlyList, IEnumerable where T : public void RemoveAt(int i) => Remove(items[i], i); public bool Remove(T item) { + if (IsReadOnly) + throw new System.Data.ReadOnlyException(); + if (!indexMap.TryGetValue(item, out int index)) return false; @@ -44,6 +59,9 @@ public class FastList : IList, IReadOnlyList, IEnumerable where T : public void Insert(int index, T item) { + if (IsReadOnly) + throw new System.Data.ReadOnlyException(); + items.Insert(index, item); for (int i = index; i < items.Count; i++) @@ -52,22 +70,28 @@ public class FastList : IList, IReadOnlyList, IEnumerable where T : public void Clear() { + if (IsReadOnly) + throw new System.Data.ReadOnlyException(); + items.Clear(); indexMap.Clear(); } public bool Contains(T item) => indexMap.ContainsKey(item); - + public int IndexOf(T item) => items.IndexOf(item); public int BinarySearch(T item, IComparer? comparer = null) => items.BinarySearch(item, comparer); + public void Sort(IComparer comparer) { + if (IsReadOnly) + throw new System.Data.ReadOnlyException(); + items.Sort(comparer); for (int i = 0; i < items.Count; i++) indexMap[items[i]] = i; } - public int IndexOf(T item) => items.IndexOf(item); public void CopyTo(T[] array, int arrayIndex) => items.CopyTo(array, arrayIndex); public IEnumerator GetEnumerator() => items.GetEnumerator();