diff --git a/src/client/spark_core/Collections/HierarchicalDictionary.cs b/src/client/spark_core/Collections/HierarchicalDictionary.cs new file mode 100644 index 000000000..9ecd3aff6 --- /dev/null +++ b/src/client/spark_core/Collections/HierarchicalDictionary.cs @@ -0,0 +1,348 @@ +#nullable enable +namespace Spark.Core.Collections { + using System; + using System.Collections; + using System.Collections.Generic; + using System.Diagnostics.CodeAnalysis; + using System.Linq; + + public class HierarchicalDictionary : + IReadOnlyDictionary> + where TKey1 : notnull + where TKey2 : notnull { + readonly Dictionary> _data; + int _value_count = 0; + + public HierarchicalDictionary() : + this(EqualityComparer.Default, EqualityComparer.Default) { } + + public HierarchicalDictionary( + IEqualityComparer comparer1, IEqualityComparer comparer2) { + this.comparer1 = comparer1; + this.comparer2 = comparer2; + _data = new(comparer1); + } + + public IEqualityComparer comparer1 { get; } + public IEqualityComparer comparer2 { get; } + + public IEnumerable Keys => _data.Keys; + public IEnumerable> Values => _data.Values; + public int Count => _data.Count; + + public IReadOnlyDictionary this[TKey1 key] => _data[key]; + + public bool ContainsKey(TKey1 key) => + _data.ContainsKey(key); + + public IEnumerator>> GetEnumerator() { + foreach (var (k1, k1_data) in _data) { + yield return new(k1, k1_data); + } + } + + public bool TryGetValue( + TKey1 key, [MaybeNullWhen(false)] out IReadOnlyDictionary value) { + if (_data.TryGetValue(key, out var res)) { + value = res; + return true; + } + value = null; + return false; + } + + public Mux mux() => new(this); + + public IDictionary get_mutable_node(TKey1 key) => + new MutableNodeWrapper(this, key); + + IEnumerator IEnumerable.GetEnumerator() => + GetEnumerator(); + + class MutableNodeWrapper : IDictionary { + readonly HierarchicalDictionary _source; + readonly TKey1 _node_key; + + public MutableNodeWrapper( + HierarchicalDictionary source, TKey1 node_key) { + _source = source; + _node_key = node_key; + } + + public ICollection Keys => _node.Keys; + + public ICollection Values => _node.Values; + + public int Count => _node.Count; + + public bool IsReadOnly => false; + + Dictionary _node { + get { + if (!_source._data.TryGetValue(_node_key, out var node)) { + _source._data.Add(_node_key, node = new()); + } + return node; + } + } + + public TValue this[TKey2 key] { + get => _node[key]; + set { + var n = _node; + int count_before = n.Count; + n[key] = value; + _value_count += n.Count - count_before; + } + } + + int _value_count { + get => _source._value_count; + set => _source._value_count = value; + } + + public void Add(TKey2 key, TValue value) { + _node.Add(key, value); + _value_count++; + } + + public void Add(KeyValuePair item) => + Add(item.Key, item.Value); + + public void Clear() { + var n = _node; + _value_count -= n.Count; + n.Clear(); + } + + public bool Contains(KeyValuePair item) => + _node.Contains(item); + + public bool ContainsKey(TKey2 key) => + _node.ContainsKey(key); + + public void CopyTo(KeyValuePair[] array, int array_index) => + ((IDictionary)_node).CopyTo(array, array_index); + + public IEnumerator> GetEnumerator() => + _node.GetEnumerator(); + + public bool Remove(TKey2 key) { + if (_node.Remove(key)) { + _value_count--; + return true; + } + return false; + } + + public bool Remove(KeyValuePair item) { + if (((IDictionary)_node).Remove(item)) { + _value_count--; + return true; + } + return false; + } + + public bool TryGetValue(TKey2 key, [MaybeNullWhen(false)] out TValue value) => + _node.TryGetValue(key, out value); + + IEnumerator IEnumerable.GetEnumerator() => + _node.GetEnumerator(); + } + + class MuxKeyCollection : ICollection<(TKey1, TKey2)> { + readonly HierarchicalDictionary _source; + + public MuxKeyCollection(HierarchicalDictionary source) => + _source = source; + + public int Count => _source._value_count; + public bool IsReadOnly => true; + + public void Add((TKey1, TKey2) item) => + throw new NotSupportedException(); + + public void Clear() => + throw new NotSupportedException(); + + public bool Contains((TKey1, TKey2) item) => + _source._data.TryGetValue(item.Item1, out var k1_data) && + k1_data.ContainsKey(item.Item2); + + public void CopyTo((TKey1, TKey2)[] array, int array_index) { + foreach (var (k1, k1_data) in _source._data) { + foreach (var k2 in k1_data.Keys) { + array[array_index++] = (k1, k2); + } + } + } + + public IEnumerator<(TKey1, TKey2)> GetEnumerator() { + foreach (var (k1, k1_data) in _source._data) { + foreach (var k2 in k1_data.Keys) { + yield return (k1, k2); + } + } + } + + public bool Remove((TKey1, TKey2) item) => + throw new NotSupportedException(); + + IEnumerator IEnumerable.GetEnumerator() => + GetEnumerator(); + } + + class MuxValueCollection : ICollection { + readonly HierarchicalDictionary _source; + + public MuxValueCollection(HierarchicalDictionary source) => + _source = source; + + public int Count => _source._value_count; + public bool IsReadOnly => true; + + public void Add(TValue item) => + throw new NotSupportedException(); + + public void Clear() => + throw new NotSupportedException(); + + public bool Contains(TValue item) => + _source._data.Values.Any(k1_data => k1_data.ContainsValue(item)); + + public void CopyTo(TValue[] array, int array_index) { + foreach (var k1_data in _source._data.Values) { + foreach (var value in k1_data.Values) { + array[array_index++] = value; + } + } + } + + public IEnumerator GetEnumerator() { + foreach (var k1_data in _source._data.Values) { + foreach (var value in k1_data.Values) { + yield return value; + } + } + } + + public bool Remove(TValue item) => + throw new NotSupportedException(); + + IEnumerator IEnumerable.GetEnumerator() => + GetEnumerator(); + } + + public class Mux : IDictionary<(TKey1, TKey2), TValue> { + readonly HierarchicalDictionary _source; + + public Mux(HierarchicalDictionary source) => + _source = source; + + Dictionary> _data => _source._data; + + public ICollection<(TKey1, TKey2)> Keys => new MuxKeyCollection(_source); + public ICollection Values => new MuxValueCollection(_source); + public int Count => _value_count; + public bool IsReadOnly => false; + + int _value_count { + get => _source._value_count; + set => _source._value_count = value; + } + + public TValue this[(TKey1, TKey2) key_pair] { + get => _data[key_pair.Item1][key_pair.Item2]; + set { + if (!_data.TryGetValue(key_pair.Item1, out var k1_data)) { + _data.Add(key_pair.Item1, k1_data = new()); + } + int count_before = k1_data.Count; + k1_data[key_pair.Item2] = value; + _source._value_count += k1_data.Count - count_before; + } + } + + public void Add((TKey1, TKey2) key_pair, TValue value) { + if (!_data.TryGetValue(key_pair.Item1, out var k1_data)) { + _data.Add(key_pair.Item1, k1_data = new()); + } + k1_data.Add(key_pair.Item2, value); + _value_count++; + } + + public void Add(KeyValuePair<(TKey1, TKey2), TValue> item) => + Add(item.Key, item.Value); + + public void Clear() => + _data.Clear(); + + public bool Contains(KeyValuePair<(TKey1, TKey2), TValue> item) => + _data.TryGetValue(item.Key.Item1, out var k1_data) && + k1_data.TryGetValue(item.Key.Item2, out var val) && + EqualityComparer.Default.Equals(val, item.Value); + + public bool ContainsKey((TKey1, TKey2) key) => + _data.TryGetValue(key.Item1, out var k1_data) && k1_data.ContainsKey(key.Item2); + + public void CopyTo(KeyValuePair<(TKey1, TKey2), TValue>[] array, int array_index) { + foreach (var (k1, k1_data) in _data) { + foreach (var (k2, value) in k1_data) { + array[array_index++] = new((k1, k2), value); + } + } + } + + public IEnumerator> GetEnumerator() { + foreach (var (k1, k1_data) in _data) { + foreach (var (k2, value) in k1_data) { + yield return new((k1, k2), value); + } + } + } + + public bool Remove((TKey1, TKey2) key) { + if (_data.TryGetValue(key.Item1, out var k1_data)) { + if (k1_data.Remove(key.Item2)) { + if (k1_data.Count == 0) { + _data.Remove(key.Item1); + } + return true; + } + } + return false; + } + + public bool Remove(KeyValuePair<(TKey1, TKey2), TValue> item) { + if (_data.TryGetValue(item.Key.Item1, out var k1_data)) { + if (k1_data.TryGetValue(item.Key.Item2, out var value) && + EqualityComparer.Default.Equals(value, item.Value) + ) { + k1_data.Remove(item.Key.Item2); + if (k1_data.Count == 0) { + _data.Remove(item.Key.Item1); + } + return true; + } + } + return false; + } + + public bool TryGetValue((TKey1, TKey2) key, [MaybeNullWhen(false)] out TValue value) { + if (_data.TryGetValue(key.Item1, out var k1_data) && + k1_data.TryGetValue(key.Item2, out value) + ) { + return true; + } + value = default; + return false; + } + + public HierarchicalDictionary demux() => + _source; + + IEnumerator IEnumerable.GetEnumerator() => + GetEnumerator(); + } + } +}