123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- using System.Collections.Generic;
- using System.Linq;
- ///<summary>接口容器</summary>
- public class UnOrderMultiMap<T, K>
- {
- private readonly Dictionary<T, List<K>> dictionary = new Dictionary<T, List<K>>();
- // 重用list
- private readonly Queue<List<K>> queue = new Queue<List<K>>();
- public Dictionary<T, List<K>> GetDictionary()
- {
- return dictionary;
- }
- public void Add(T t, K k)
- {
- List<K> list;
- this.dictionary.TryGetValue(t, out list);
- if (list == null)
- {
- list = FetchList();
- dictionary[t] = list;
- }
- list.Add(k);
- }
- public KeyValuePair<T, List<K>> First()
- {
- return dictionary.First();
- }
- public int Count
- {
- get
- {
- return dictionary.Count;
- }
- }
- private List<K> FetchList()
- {
- if (queue.Count > 0)
- {
- List<K> list = queue.Dequeue();
- list.Clear();
- return list;
- }
- return new List<K>();
- }
- private void RecycleList(List<K> list)
- {
- // 防止暴涨
- if (queue.Count > 100)
- {
- return;
- }
- list.Clear();
- queue.Enqueue(list);
- }
- public bool Remove(T t, K k)
- {
- List<K> list;
- dictionary.TryGetValue(t, out list);
- if (list == null)
- {
- return false;
- }
- if (!list.Remove(k))
- {
- return false;
- }
- if (list.Count == 0)
- {
- RecycleList(list);
- dictionary.Remove(t);
- }
- return true;
- }
- public bool Remove(T t)
- {
- List<K> list = null;
- dictionary.TryGetValue(t, out list);
- if (list != null)
- {
- RecycleList(list);
- }
- return dictionary.Remove(t);
- }
- /// <summary>不返回内部的list,copy一份出来</summary>
- public K[] GetAll(T t)
- {
- List<K> list;
- dictionary.TryGetValue(t, out list);
- if (list == null)
- {
- return new K[0];
- }
- return list.ToArray();
- }
- /// <summary>返回内部的list</summary>
- public List<K> this[T t]
- {
- get
- {
- List<K> list;
- dictionary.TryGetValue(t, out list);
- return list;
- }
- }
- public K GetOne(T t)
- {
- List<K> list;
- dictionary.TryGetValue(t, out list);
- if (list != null && list.Count > 0)
- {
- return list[0];
- }
- return default(K);
- }
- public bool Contains(T t, K k)
- {
- List<K> list;
- dictionary.TryGetValue(t, out list);
- if (list == null)
- {
- return false;
- }
- return list.Contains(k);
- }
- public bool ContainsKey(T t)
- {
- return dictionary.ContainsKey(t);
- }
- public void Clear()
- {
- foreach (KeyValuePair<T, List<K>> keyValuePair in dictionary)
- {
- RecycleList(keyValuePair.Value);
- }
- dictionary.Clear();
- }
- }
|