49 lines
859 B
C#
49 lines
859 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
public class FastUniqueList<T>
|
|
{
|
|
private readonly List<T> list = [];
|
|
private readonly HashSet<T> set = [];
|
|
|
|
public int Count => list.Count;
|
|
|
|
public T this[int index] => list[index];
|
|
|
|
public bool Contains(T item)
|
|
{
|
|
return set.Contains(item);
|
|
}
|
|
|
|
public bool Add(T item)
|
|
{
|
|
if (!set.Add(item))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
list.Add(item);
|
|
return true;
|
|
}
|
|
|
|
public bool Remove(T item)
|
|
{
|
|
if (!set.Remove(item))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
int index = list.IndexOf(item);
|
|
int last = list.Count - 1;
|
|
|
|
list[index] = list[last];
|
|
list.RemoveAt(last);
|
|
|
|
return true;
|
|
}
|
|
|
|
public void ForEach(Action<T> action)
|
|
{
|
|
list.ForEach(action);
|
|
}
|
|
}
|