45 lines
1.1 KiB
C#
45 lines
1.1 KiB
C#
using System.Collections;
|
|
|
|
namespace Dashboard.Collections
|
|
{
|
|
public class TypeHashSet(bool hierarchical = false) : IEnumerable<TypeAtom>
|
|
{
|
|
private readonly HashSet<int> _set = new HashSet<int>();
|
|
|
|
public bool Contains<T>() => _set.Contains(TypeAtom<T>.Id);
|
|
|
|
public bool Set<T>()
|
|
{
|
|
if (!_set.Add(TypeAtom<T>.Id))
|
|
return false;
|
|
|
|
if (hierarchical)
|
|
foreach (TypeAtom ancestor in TypeAtom<T>.Ancestors)
|
|
{
|
|
_set.Add(ancestor.Id);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool Reset<T>()
|
|
{
|
|
if (!_set.Remove(TypeAtom<T>.Id))
|
|
return false;
|
|
|
|
if (hierarchical)
|
|
foreach (TypeAtom ancestor in TypeAtom<T>.Ancestors)
|
|
{
|
|
_set.Remove(ancestor.Id);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public void Clear() => _set.Clear();
|
|
|
|
public IEnumerator<TypeAtom> GetEnumerator() => _set.Select(x => TypeAtom.Get(x)!).GetEnumerator();
|
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
|
}
|
|
}
|