/** * 多重映射结构 * */ using System.Collections.Generic; namespace ET { public class UnOrderMultiMapSet: Dictionary> { // 重用HashSet public new HashSet this[T t] { get { HashSet set; if (!this.TryGetValue(t, out set)) { set = new HashSet(); } return set; } } public Dictionary> GetDictionary() { return this; } public void Add(T t, K k) { HashSet set; this.TryGetValue(t, out set); if (set == null) { set = new HashSet(); base[t] = set; } set.Add(k); } public bool Remove(T t, K k) { HashSet set; this.TryGetValue(t, out set); if (set == null) { return false; } if (!set.Remove(k)) { return false; } if (set.Count == 0) { this.Remove(t); } return true; } public bool Contains(T t, K k) { HashSet set; this.TryGetValue(t, out set); if (set == null) { return false; } return set.Contains(k); } public new int Count { get { int count = 0; foreach (KeyValuePair> kv in this) { count += kv.Value.Count; } return count; } } } }