using System; using System.Collections.Generic; namespace DSPRE.ROMFiles { public class UniqueList { private readonly List list = new List(); private readonly HashSet set = new HashSet(); public UniqueList(int capacity) { list = new List(capacity); set = new HashSet(capacity); } public UniqueList() { } public void Add(T item) { if (!set.Contains(item)) { list.Add(item); set.Add(item); } } public bool Contains(T item) { return set.Contains(item); } public void Clear() { list.Clear(); set.Clear(); } public bool Remove(T item) { if (set.Contains(item)) { list.Remove(item); set.Remove(item); return true; } return false; } public bool RemoveAt(int index) { if (index >= 0 && index < list.Count) { T itemToRemove = list[index]; list.RemoveAt(index); set.Remove(itemToRemove); return true; } return false; } // Expose some methods from the internal List public T Find(Predicate match) { return list.Find(match); } public int FindIndex(Predicate match) { return list.FindIndex(match); } public void Sort() { list.Sort(); } public IEnumerator GetEnumerator() { return list.GetEnumerator(); } public T this[int index] { get { return list[index]; } set { if (set.Contains(value)) { //Then the list also contains the value int oldIndex = list.FindIndex(x => x.Equals(value)); //this is where it is if (index == oldIndex) { //No operation, same index and same existing value. return; } //Otherwise, move the existing element in the list to the new index. list.Move(oldIndex, index); } else { //New element list[index] = value; set.Add(value); } } } public int Count => list.Count; } }