Add HierarchicalDictionary
A useful abstraction for nested dictionaries
This commit is contained in:
1 parent
a8992edf88
commit
65665dc384
1 file changed
+348
@@ -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<TKey1, TKey2, TValue> :
|
||||
IReadOnlyDictionary<TKey1, IReadOnlyDictionary<TKey2, TValue>>
|
||||
where TKey1 : notnull
|
||||
where TKey2 : notnull {
|
||||
readonly Dictionary<TKey1, Dictionary<TKey2, TValue>> _data;
|
||||
int _value_count = 0;
|
||||
|
||||
public HierarchicalDictionary() :
|
||||
this(EqualityComparer<TKey1>.Default, EqualityComparer<TKey2>.Default) { }
|
||||
|
||||
public HierarchicalDictionary(
|
||||
IEqualityComparer<TKey1> comparer1, IEqualityComparer<TKey2> comparer2) {
|
||||
this.comparer1 = comparer1;
|
||||
this.comparer2 = comparer2;
|
||||
_data = new(comparer1);
|
||||
}
|
||||
|
||||
public IEqualityComparer<TKey1> comparer1 { get; }
|
||||
public IEqualityComparer<TKey2> comparer2 { get; }
|
||||
|
||||
public IEnumerable<TKey1> Keys => _data.Keys;
|
||||
public IEnumerable<IReadOnlyDictionary<TKey2, TValue>> Values => _data.Values;
|
||||
public int Count => _data.Count;
|
||||
|
||||
public IReadOnlyDictionary<TKey2, TValue> this[TKey1 key] => _data[key];
|
||||
|
||||
public bool ContainsKey(TKey1 key) =>
|
||||
_data.ContainsKey(key);
|
||||
|
||||
public IEnumerator<KeyValuePair<TKey1, IReadOnlyDictionary<TKey2, TValue>>> GetEnumerator() {
|
||||
foreach (var (k1, k1_data) in _data) {
|
||||
yield return new(k1, k1_data);
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryGetValue(
|
||||
TKey1 key, [MaybeNullWhen(false)] out IReadOnlyDictionary<TKey2, TValue> value) {
|
||||
if (_data.TryGetValue(key, out var res)) {
|
||||
value = res;
|
||||
return true;
|
||||
}
|
||||
value = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public Mux mux() => new(this);
|
||||
|
||||
public IDictionary<TKey2, TValue> get_mutable_node(TKey1 key) =>
|
||||
new MutableNodeWrapper(this, key);
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() =>
|
||||
GetEnumerator();
|
||||
|
||||
class MutableNodeWrapper : IDictionary<TKey2, TValue> {
|
||||
readonly HierarchicalDictionary<TKey1, TKey2, TValue> _source;
|
||||
readonly TKey1 _node_key;
|
||||
|
||||
public MutableNodeWrapper(
|
||||
HierarchicalDictionary<TKey1, TKey2, TValue> source, TKey1 node_key) {
|
||||
_source = source;
|
||||
_node_key = node_key;
|
||||
}
|
||||
|
||||
public ICollection<TKey2> Keys => _node.Keys;
|
||||
|
||||
public ICollection<TValue> Values => _node.Values;
|
||||
|
||||
public int Count => _node.Count;
|
||||
|
||||
public bool IsReadOnly => false;
|
||||
|
||||
Dictionary<TKey2, TValue> _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<TKey2, TValue> item) =>
|
||||
Add(item.Key, item.Value);
|
||||
|
||||
public void Clear() {
|
||||
var n = _node;
|
||||
_value_count -= n.Count;
|
||||
n.Clear();
|
||||
}
|
||||
|
||||
public bool Contains(KeyValuePair<TKey2, TValue> item) =>
|
||||
_node.Contains(item);
|
||||
|
||||
public bool ContainsKey(TKey2 key) =>
|
||||
_node.ContainsKey(key);
|
||||
|
||||
public void CopyTo(KeyValuePair<TKey2, TValue>[] array, int array_index) =>
|
||||
((IDictionary<TKey2, TValue>)_node).CopyTo(array, array_index);
|
||||
|
||||
public IEnumerator<KeyValuePair<TKey2, TValue>> GetEnumerator() =>
|
||||
_node.GetEnumerator();
|
||||
|
||||
public bool Remove(TKey2 key) {
|
||||
if (_node.Remove(key)) {
|
||||
_value_count--;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Remove(KeyValuePair<TKey2, TValue> item) {
|
||||
if (((IDictionary<TKey2, TValue>)_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<TKey1, TKey2, TValue> _source;
|
||||
|
||||
public MuxKeyCollection(HierarchicalDictionary<TKey1, TKey2, TValue> 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<TValue> {
|
||||
readonly HierarchicalDictionary<TKey1, TKey2, TValue> _source;
|
||||
|
||||
public MuxValueCollection(HierarchicalDictionary<TKey1, TKey2, TValue> 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<TValue> 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<TKey1, TKey2, TValue> _source;
|
||||
|
||||
public Mux(HierarchicalDictionary<TKey1, TKey2, TValue> source) =>
|
||||
_source = source;
|
||||
|
||||
Dictionary<TKey1, Dictionary<TKey2, TValue>> _data => _source._data;
|
||||
|
||||
public ICollection<(TKey1, TKey2)> Keys => new MuxKeyCollection(_source);
|
||||
public ICollection<TValue> 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<TValue>.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<KeyValuePair<(TKey1, TKey2), TValue>> 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<TValue>.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<TKey1, TKey2, TValue> demux() =>
|
||||
_source;
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() =>
|
||||
GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user