using System; using System.Collections.Generic; using System.Linq; namespace ET { public class MultiMapSet: SortedDictionary> { private readonly HashSet Empty = new HashSet(); public void Add(T t, K k) { HashSet list; this.TryGetValue(t, out list); if (list == null) { list = new HashSet(); this.Add(t, list); } list.Add(k); } public bool Remove(T t, K k) { HashSet list; this.TryGetValue(t, out list); if (list == null) { return false; } if (!list.Remove(k)) { return false; } if (list.Count == 0) { this.Remove(t); } return true; } /// /// 不返回内部的list,copy一份出来 /// /// /// public K[] GetAll(T t) { HashSet list; this.TryGetValue(t, out list); if (list == null) { return Array.Empty(); } return list.ToArray(); } /// /// 返回内部的list /// /// /// public new HashSet this[T t] { get { this.TryGetValue(t, out var list); return list ?? Empty; } } public K GetOne(T t) { HashSet list; this.TryGetValue(t, out list); if (list != null && list.Count > 0) { return list.FirstOrDefault(); } return default; } public bool Contains(T t, K k) { HashSet list; this.TryGetValue(t, out list); if (list == null) { return false; } return list.Contains(k); } } }