UnOrderMultiMap.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. 
  2. namespace KYFramework
  3. {
  4. public class UnOrderMultiMap<T, K>
  5. {
  6. private readonly Dictionary<T, List<K>> dictionary = new Dictionary<T, List<K>>();
  7. // 重用list
  8. private readonly Queue<List<K>> queue = new Queue<List<K>>();
  9. public Dictionary<T, List<K>> GetDictionary()
  10. {
  11. return this.dictionary;
  12. }
  13. public void Add(T t, K k)
  14. {
  15. List<K> list;
  16. this.dictionary.TryGetValue(t, out list);
  17. if (list == null)
  18. {
  19. list = this.FetchList();
  20. this.dictionary[t] = list;
  21. }
  22. list.Add(k);
  23. }
  24. public KeyValuePair<T, List<K>> First()
  25. {
  26. return this.dictionary.First();
  27. }
  28. public int Count
  29. {
  30. get
  31. {
  32. return this.dictionary.Count;
  33. }
  34. }
  35. private List<K> FetchList()
  36. {
  37. if (this.queue.Count > 0)
  38. {
  39. List<K> list = this.queue.Dequeue();
  40. list.Clear();
  41. return list;
  42. }
  43. return new List<K>();
  44. }
  45. private void RecycleList(List<K> list)
  46. {
  47. // 防止暴涨
  48. if (this.queue.Count > 100)
  49. {
  50. return;
  51. }
  52. list.Clear();
  53. this.queue.Enqueue(list);
  54. }
  55. public bool Remove(T t, K k)
  56. {
  57. List<K> list;
  58. this.dictionary.TryGetValue(t, out list);
  59. if (list == null)
  60. {
  61. return false;
  62. }
  63. if (!list.Remove(k))
  64. {
  65. return false;
  66. }
  67. if (list.Count == 0)
  68. {
  69. this.RecycleList(list);
  70. this.dictionary.Remove(t);
  71. }
  72. return true;
  73. }
  74. public bool Remove(T t)
  75. {
  76. List<K> list = null;
  77. this.dictionary.TryGetValue(t, out list);
  78. if (list != null)
  79. {
  80. this.RecycleList(list);
  81. }
  82. return this.dictionary.Remove(t);
  83. }
  84. /// <summary>
  85. /// 不返回内部的list,copy一份出来
  86. /// </summary>
  87. /// <param name="t"></param>
  88. /// <returns></returns>
  89. public K[] GetAll(T t)
  90. {
  91. List<K> list;
  92. this.dictionary.TryGetValue(t, out list);
  93. if (list == null)
  94. {
  95. return new K[0];
  96. }
  97. return list.ToArray();
  98. }
  99. /// <summary>
  100. /// 返回内部的list
  101. /// </summary>
  102. /// <param name="t"></param>
  103. /// <returns></returns>
  104. public List<K> this[T t]
  105. {
  106. get
  107. {
  108. List<K> list;
  109. this.dictionary.TryGetValue(t, out list);
  110. return list;
  111. }
  112. }
  113. public K GetOne(T t)
  114. {
  115. List<K> list;
  116. this.dictionary.TryGetValue(t, out list);
  117. if (list != null && list.Count > 0)
  118. {
  119. return list[0];
  120. }
  121. return default(K);
  122. }
  123. public bool Contains(T t, K k)
  124. {
  125. List<K> list;
  126. this.dictionary.TryGetValue(t, out list);
  127. if (list == null)
  128. {
  129. return false;
  130. }
  131. return list.Contains(k);
  132. }
  133. public bool ContainsKey(T t)
  134. {
  135. return this.dictionary.ContainsKey(t);
  136. }
  137. public void Clear()
  138. {
  139. foreach (KeyValuePair<T, List<K>> keyValuePair in this.dictionary)
  140. {
  141. this.RecycleList(keyValuePair.Value);
  142. }
  143. this.dictionary.Clear();
  144. }
  145. }
  146. }