/** * 多重映射结构 * */ using System.Collections.Generic; namespace ET { public class UnOrderMultiMapSet { private readonly Dictionary> dictionary = new Dictionary>(); // 重用HashSet private readonly Queue> queue = new Queue>(); public HashSet this[T t] { get { HashSet set; if (!this.dictionary.TryGetValue(t, out set)) { set = new HashSet(); } return set; } } public Dictionary> GetDictionary() { return this.dictionary; } public void Add(T t, K k) { HashSet set; this.dictionary.TryGetValue(t, out set); if (set == null) { set = this.FetchList(); this.dictionary[t] = set; } set.Add(k); } public bool Remove(T t, K k) { HashSet set; this.dictionary.TryGetValue(t, out set); if (set == null) { return false; } if (!set.Remove(k)) { return false; } if (set.Count == 0) { this.RecycleList(set); this.dictionary.Remove(t); } return true; } public bool Remove(T t) { HashSet set = null; this.dictionary.TryGetValue(t, out set); if (set != null) { this.RecycleList(set); } return this.dictionary.Remove(t); } private HashSet FetchList() { if (this.queue.Count > 0) { HashSet set = this.queue.Dequeue(); set.Clear(); return set; } return new HashSet(); } private void RecycleList(HashSet set) { // 防止暴涨 if (this.queue.Count > 100) { return; } set.Clear(); this.queue.Enqueue(set); } public bool Contains(T t, K k) { HashSet set; this.dictionary.TryGetValue(t, out set); if (set == null) { return false; } return set.Contains(k); } public bool ContainsKey(T t) { return this.dictionary.ContainsKey(t); } public void Clear() { dictionary.Clear(); } public int Count { get { int count = 0; foreach (KeyValuePair> kv in this.dictionary) { count += kv.Value.Count; } return count; } } } }