fix: fast list readonly mode not throwing exceptions
This commit is contained in:
@@ -8,13 +8,25 @@ public class FastList<T> : IList<T>, IReadOnlyList<T>, IEnumerable<T> where T :
|
||||
private readonly List<T> items = [];
|
||||
private readonly Dictionary<T, int> 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<T> : IList<T>, IReadOnlyList<T>, IEnumerable<T> 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<T> : IList<T>, IReadOnlyList<T>, IEnumerable<T> 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<T> : IList<T>, IReadOnlyList<T>, IEnumerable<T> 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<T>? comparer = null) => items.BinarySearch(item, comparer);
|
||||
|
||||
public void Sort(IComparer<T> 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<T> GetEnumerator() => items.GetEnumerator();
|
||||
|
Reference in New Issue
Block a user