MultiMap.cs 2.7 KB

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