123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- /**
- * 多重映射结构
- *
- */
- using System.Collections.Generic;
- namespace Fort23.Core
- {
- public class UnOrderMultiMapSet<T, K> : Dictionary<T, HashSet<K>>
- {
- // 重用HashSet
- public new HashSet<K> this[T t]
- {
- get
- {
- HashSet<K> set;
- if (!this.TryGetValue(t, out set))
- {
- set = new HashSet<K>();
- }
- return set;
- }
- }
- public Dictionary<T, HashSet<K>> GetDictionary()
- {
- return this;
- }
- public void Add(T t, K k)
- {
- HashSet<K> set;
- this.TryGetValue(t, out set);
- if (set == null)
- {
- set = new HashSet<K>();
- base[t] = set;
- }
- set.Add(k);
- }
- public bool Remove(T t, K k)
- {
- HashSet<K> 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<K> 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<T, HashSet<K>> kv in this)
- {
- count += kv.Value.Count;
- }
- return count;
- }
- }
- }
- }
|