UnOrderMultiMap.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. ///<summary>接口容器</summary>
  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 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 = FetchList();
  20. dictionary[t] = list;
  21. }
  22. list.Add(k);
  23. }
  24. public KeyValuePair<T, List<K>> First()
  25. {
  26. return dictionary.First();
  27. }
  28. public int Count
  29. {
  30. get
  31. {
  32. return dictionary.Count;
  33. }
  34. }
  35. private List<K> FetchList()
  36. {
  37. if (queue.Count > 0)
  38. {
  39. List<K> list = 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 (queue.Count > 100)
  49. {
  50. return;
  51. }
  52. list.Clear();
  53. queue.Enqueue(list);
  54. }
  55. public bool Remove(T t, K k)
  56. {
  57. List<K> list;
  58. 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. RecycleList(list);
  70. dictionary.Remove(t);
  71. }
  72. return true;
  73. }
  74. public bool Remove(T t)
  75. {
  76. List<K> list = null;
  77. dictionary.TryGetValue(t, out list);
  78. if (list != null)
  79. {
  80. RecycleList(list);
  81. }
  82. return dictionary.Remove(t);
  83. }
  84. /// <summary>不返回内部的list,copy一份出来</summary>
  85. public K[] GetAll(T t)
  86. {
  87. List<K> list;
  88. dictionary.TryGetValue(t, out list);
  89. if (list == null)
  90. {
  91. return new K[0];
  92. }
  93. return list.ToArray();
  94. }
  95. /// <summary>返回内部的list</summary>
  96. public List<K> this[T t]
  97. {
  98. get
  99. {
  100. List<K> list;
  101. dictionary.TryGetValue(t, out list);
  102. return list;
  103. }
  104. }
  105. public K GetOne(T t)
  106. {
  107. List<K> list;
  108. 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. dictionary.TryGetValue(t, out list);
  119. if (list == null)
  120. {
  121. return false;
  122. }
  123. return list.Contains(k);
  124. }
  125. public bool ContainsKey(T t)
  126. {
  127. return dictionary.ContainsKey(t);
  128. }
  129. public void Clear()
  130. {
  131. foreach (KeyValuePair<T, List<K>> keyValuePair in dictionary)
  132. {
  133. RecycleList(keyValuePair.Value);
  134. }
  135. dictionary.Clear();
  136. }
  137. }