using System; using System.Collections; using System.Collections.Generic; using System.Linq; namespace bts.collections { public interface IGroupingSet : IGrouping, ISet { } public class SetMap : IEnumerable>, ICollection, IReadOnlyDictionary> { readonly IEqualityComparer _key_comparer; readonly IEqualityComparer _element_comparer; readonly Dictionary _dictionary; public SetMap() : this(EqualityComparer.Default) { } public SetMap(IEqualityComparer key_comparer) : this(key_comparer, EqualityComparer.Default) { } public SetMap(IEqualityComparer key_comparer, IEqualityComparer element_comparer) : this(new Dictionary(), key_comparer, element_comparer) { } SetMap(Dictionary dictionary, IEqualityComparer key_comparer, IEqualityComparer element_comparer) { _dictionary = dictionary; _key_comparer = key_comparer; _element_comparer = element_comparer; } public int Count => _dictionary.Count; public object SyncRoot => null; public bool IsSynchronized => false; public IEnumerable Keys => _dictionary.Keys; public IEnumerable> Values => _dictionary.Values; IEnumerable IReadOnlyDictionary>.this[TKey key] => _dictionary[key]; public ISet this[TKey key] => _dictionary[key]; public void CopyTo(Array array, int index) => ((ICollection)_dictionary).CopyTo(array, index); public IEnumerator> GetEnumerator() => _dictionary.Values.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); public bool add(TKey key, TElement value) { if (!_dictionary.TryGetValue(key, out InternalGroupSet values)) _dictionary.Add(key, values = new InternalGroupSet(key, _element_comparer)); return values.Add(value); } public void add(TKey key, IEnumerable values) { if (!_dictionary.TryGetValue(key, out InternalGroupSet igs)) _dictionary.Add(key, igs = new InternalGroupSet(key, _element_comparer)); igs.AddRange(values); } public bool add(TKey key, out ISet values) { if (!_dictionary.TryGetValue(key, out InternalGroupSet igs)) { _dictionary.Add(key, igs = new InternalGroupSet(key, _element_comparer)); values = igs; return true; } values = igs; return false; } /// /// If '' is already contained in the specified 's set (according to the element-comparer being used by this instance), /// it will be replaced with ',' and false will be returned. Otherwise, '' will be added and true will be returned. /// /// The key bucket to which the value should be added. /// The value to added. /// False if equivalent value was already present in the bucket, otherwise true. public bool set(TKey key, TElement value) { if (!_dictionary.TryGetValue(key, out InternalGroupSet values)) { _dictionary[key] = values = new InternalGroupSet(key, _element_comparer); } else { if (values.Remove(value)) { values.Add(value); return false; } } values.Add(value); return true; } public bool remove(TKey key, TElement value) { if (_dictionary.TryGetValue(key, out InternalGroupSet values)) { bool removed = values.Remove(value); if (values.Count == 0) _dictionary.Remove(key); return removed; } return false; } public bool remove(TKey key) => _dictionary.Remove(key); public void clear() => _dictionary.Clear(); public bool try_pop(TKey key, out ISet value) { if (try_lookup(key, out value)) { remove(key); return true; } else return false; } public bool try_lookup(TKey key, out ISet value) { if (_dictionary.TryGetValue(key, out InternalGroupSet result)) { value = result; return true; } value = null; return false; } public SetMap convert(Func selector) => convert(selector, EqualityComparer.Default); public SetMap convert(Func selector, IEqualityComparer result_comparer) => new SetMap( _dictionary.ToDictionary( kv => kv.Key, kv => new SetMap.InternalGroupSet(kv.Key, kv.Value.Select(selector), result_comparer) ), _key_comparer, result_comparer); public SetMap clone() => new SetMap( _dictionary.ToDictionary( kv => kv.Key, kv => new InternalGroupSet(kv.Key, kv.Value, _element_comparer) ), _key_comparer, _element_comparer); public bool ContainsKey(TKey key) => _dictionary.ContainsKey(key); public bool TryGetValue(TKey key, out IEnumerable value) { if (_dictionary.TryGetValue(key, out InternalGroupSet igs)) { value = igs; return true; } value = null; return false; } IEnumerator>> IEnumerable>>.GetEnumerator() { foreach (var kv in _dictionary) yield return new KeyValuePair>(kv.Key, kv.Value); } class InternalGroupSet : HashSet, IGroupingSet { public InternalGroupSet(TKey key, IEqualityComparer comparer) : base(comparer) => Key = key; public InternalGroupSet(TKey key, IEnumerable values, IEqualityComparer comparer) : base(values, comparer) => Key = key; public TKey Key { get; } } } public static class SetMap { public static SetMap create(IEnumerable source, Func key_selector, Func value_selector) => create(source, key_selector, value_selector, EqualityComparer.Default, EqualityComparer.Default); public static SetMap create( IEnumerable source, Func key_selector, Func value_selector, IEqualityComparer key_comparer, IEqualityComparer value_comparer ) { var sm = new SetMap(key_comparer, value_comparer); foreach (var el in source) { sm.add(key_selector(el), value_selector(el)); } return sm; } public static bool removes_key(this SetMap set_map, TKey key, TElement element) { int count_before = set_map.Count; if (set_map.remove(key, element)) return set_map.Count < count_before; return false; } public static bool adds_key(this SetMap set_map, TKey key, TElement element) { int count_before = set_map.Count; if (set_map.add(key, element)) return set_map.Count > count_before; return false; } } }