103 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			103 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using System.Collections;
 | 
						|
using System.Collections.Generic;
 | 
						|
 | 
						|
namespace Engine.Core;
 | 
						|
 | 
						|
public class FastList<T> : IList<T>, IReadOnlyList<T>, IEnumerable<T> where T : notnull
 | 
						|
{
 | 
						|
    private readonly List<T> items = [];
 | 
						|
    private readonly Dictionary<T, int> indexMap = [];
 | 
						|
 | 
						|
    public bool IsReadOnly { get; set; } = false;
 | 
						|
    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);
 | 
						|
    }
 | 
						|
 | 
						|
    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;
 | 
						|
 | 
						|
        Remove(item, index);
 | 
						|
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
 | 
						|
    private void Remove(T item, int index)
 | 
						|
    {
 | 
						|
        int lastIndex = items.Count - 1;
 | 
						|
        T lastItem = items[lastIndex];
 | 
						|
 | 
						|
        items[index] = lastItem;
 | 
						|
        indexMap[lastItem] = index;
 | 
						|
 | 
						|
        items.RemoveAt(lastIndex);
 | 
						|
        indexMap.Remove(item);
 | 
						|
    }
 | 
						|
 | 
						|
    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++)
 | 
						|
            indexMap[items[i]] = i;
 | 
						|
    }
 | 
						|
 | 
						|
    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 void CopyTo(T[] array, int arrayIndex) => items.CopyTo(array, arrayIndex);
 | 
						|
 | 
						|
    public IEnumerator<T> GetEnumerator() => items.GetEnumerator();
 | 
						|
    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
 | 
						|
 | 
						|
    public FastList() { }
 | 
						|
    public FastList(int count) { items.Capacity = count; }
 | 
						|
}
 |