MultiMap.cs 2.3 KB

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