using System.Collections; using System.Diagnostics.CodeAnalysis; namespace Dashboard.Collections { public class TypeDictionary(bool hierarchical = false) : IEnumerable { private readonly Dictionary _objects = new Dictionary(); public bool Contains() where T2 : T => _objects.ContainsKey(TypeAtom.Id); public bool Add(T2 value) where T2 : T { TypeAtom atom = TypeAtom.Atom; if (!_objects.TryAdd(atom.Id, value)) return false; if (!hierarchical) return true; foreach (TypeAtom ancestor in atom.Ancestors) { _objects[ancestor.Id] = value; } return true; } public T2 Get() where T2 : T => TryGet(out T2? value) ? value : throw new KeyNotFoundException(); public void Set(T2 value) where T2 : T { TypeAtom atom = TypeAtom.Atom; _objects[atom.Id] = value; if (!hierarchical) return; foreach (TypeAtom ancestor in atom.Ancestors) { _objects[ancestor.Id] = value; } } public bool Remove() where T2 : T => Remove(out _); public bool Remove([NotNullWhen(true)] out T2? value) where T2 : T { TypeAtom atom = TypeAtom.Atom; if (!_objects.Remove(atom.Id, out T? subValue)) { value = default; return false; } value = (T2?)subValue; if (hierarchical) { foreach (TypeAtom ancestor in atom.Ancestors) { _objects.Remove(ancestor.Id); } } return value != null; } public bool TryGet([NotNullWhen(true)] out T2? value) where T2 : T { if (!_objects.TryGetValue(TypeAtom.Id, out T? subValue)) { value = default; return false; } value = (T2?)subValue; return value != null; } public void Clear() => _objects.Clear(); public IEnumerator GetEnumerator() => _objects.Values.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } public class TypeDictionary(bool hierarchical = false) : IEnumerable> { private readonly Dictionary _objects = new Dictionary(); public bool Contains() where TKey2 : TKey => _objects.ContainsKey(TypeAtom.Id); public bool Add(TValue value) where TKey2 : TKey { TypeAtom atom = TypeAtom.Atom; if (!_objects.TryAdd(atom.Id, value)) return false; if (!hierarchical) return true; foreach (TypeAtom ancestor in atom.Ancestors) { _objects[ancestor.Id] = value; } return true; } public TValue Get() where TKey2 : TKey => (TValue)_objects[TypeAtom.Id]!; public void Set(TValue value) where TKey2 : TKey { TypeAtom atom = TypeAtom.Atom; _objects[atom.Id] = value; if (!hierarchical) return; foreach (TypeAtom ancestor in atom.Ancestors) { _objects[ancestor.Id] = value; } } public bool Remove() where TKey2 : TKey => Remove(out _); public bool Remove([MaybeNullWhen(false)] out TValue? value) where TKey2 : TKey { TypeAtom atom = TypeAtom.Atom; if (!_objects.Remove(atom.Id, out value)) { return false; } if (hierarchical) { foreach (TypeAtom ancestor in atom.Ancestors) { _objects.Remove(ancestor.Id); } } return true; } public bool TryGet([NotNullWhen(true)] out TValue? value) where TKey2 : TKey => _objects.TryGetValue(TypeAtom.Id, out value); public void Clear() => _objects.Clear(); public IEnumerator> GetEnumerator() => _objects.Select(x => new KeyValuePair(TypeAtom.Get(x.Key)!, x.Value)).GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } }