using System; using System.Collections; using System.Collections.Generic; namespace CompatBot.Utils { public class NameUniqueObjectCollection: IDictionary> { private readonly Dictionary> dict; private readonly IEqualityComparer valueComparer; public NameUniqueObjectCollection(IEqualityComparer keyComparer = null, IEqualityComparer valueComparer = null) { dict = new Dictionary>(keyComparer); this.valueComparer = valueComparer; } public IEnumerator>> GetEnumerator() => dict.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)dict).GetEnumerator(); void ICollection>>.Add(KeyValuePair> item) => Add(item.Key, item.Value); public void Add(string key, UniqueList value) { value ??= new UniqueList(valueComparer); if (dict.TryGetValue(key, out var c)) c.AddRange(value); else dict.Add(key, value); } public void Add(string key, TValue value) { if (dict.TryGetValue(key, out var c)) c.Add(value); else { dict[key] = c = new UniqueList(valueComparer); c.Add(value); } } public void Clear() => dict.Clear(); bool ICollection>>.Contains(KeyValuePair> item) => ((IDictionary>)dict).Contains(item); void ICollection>>.CopyTo(KeyValuePair>[] array, int arrayIndex) => ((IDictionary>)dict).CopyTo(array, arrayIndex); bool ICollection>>.Remove(KeyValuePair> item) => ((IDictionary>)dict).Remove(item); public int Count => dict.Count; public bool IsReadOnly => false; public bool ContainsKey(string key) => dict.ContainsKey(key); public bool Remove(string key) => dict.Remove(key); public bool TryGetValue(string key, out UniqueList value) { var result = dict.TryGetValue(key, out value); if (!result) dict[key] = value = new UniqueList(valueComparer); return result; } public UniqueList this[string key] { get { TryGetValue(key, out var value); return value; } set => dict[key] = (value ?? new UniqueList(valueComparer)); } public ICollection Keys => dict.Keys; public ICollection> Values => dict.Values; } }