using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Options; using System; using System.Collections.Generic; namespace ET { public class DoubleDictionary : Dictionary, K> where T1 : notnull where T2 : notnull { private readonly Dictionary _key1Dic = new Dictionary(); private readonly MultiMap _key2Dic = new MultiMap(); public DoubleDictionary() : base() { } public void Add(T1 key1, T2 key2, K value) { base.TryAdd(new KeyValuePair(key1, key2), value); _key1Dic.Add(key1, value); _key2Dic.Add(key2, value); } public bool Remove(T1 key1,T2 key2,K value) { _key1Dic.Remove(key1); _key2Dic.Remove(key2,value); return base.Remove(new KeyValuePair(key1, key2)); } public K this[T1 key1,T2 key2] { get { return base[KeyValuePair.Create(key1, key2)]; } set { _key1Dic.Remove(key1); _key2Dic.Remove(key2,value); base[KeyValuePair.Create(key1, key2)] = value; } } public bool TryGetValueByKey1(T1 key1, out K value) { return _key1Dic.TryGetValue(key1, out value); } public bool TryGetValueByKey2(T2 key2, out List list) { list = _key2Dic[key2]; return list != null && list.Count != 0; } public IEnumerable> GetAllByKey2() { return _key2Dic.Values; } public new void Clear() { base.Clear(); _key1Dic.Clear(); _key2Dic.Clear(); } public void OnDeserialization() { foreach (var item in this) { _key1Dic.Add(item.Key.Key, item.Value); _key2Dic.Add(item.Key.Value, item.Value); } } } }