From c538dbd56beffc5b086484c48b8160b484acac2a Mon Sep 17 00:00:00 2001 From: "H. Utku Maden" Date: Fri, 25 Jul 2025 23:51:14 +0300 Subject: [PATCH] Implement type dictionaries, with hierarchical sets and unsets. --- .../Collections/TypeDictionary.cs | 157 ++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 Dashboard.Common/Collections/TypeDictionary.cs diff --git a/Dashboard.Common/Collections/TypeDictionary.cs b/Dashboard.Common/Collections/TypeDictionary.cs new file mode 100644 index 0000000..bc4f077 --- /dev/null +++ b/Dashboard.Common/Collections/TypeDictionary.cs @@ -0,0 +1,157 @@ +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(); + } +}