MultiDictionary.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. #pragma warning disable CS8601 // Possible null reference assignment.
  6. #pragma warning disable CS8602 // Dereference of a possibly null reference.
  7. #pragma warning disable CS8603 // Possible null reference return.
  8. #pragma warning disable CS8604 // Possible null reference argument.
  9. #pragma warning disable CS8605 // Unboxing a possibly null value.
  10. #pragma warning disable CS8618 // Non-nullable property 'Text' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
  11. #pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
  12. namespace EasyDevCore.Common.Wrapper
  13. {
  14. /// <summary>
  15. /// Represents a collection of keys and values. You can add multiple values to the same key.
  16. /// </summary>
  17. /// <typeparam name="TKey">The type of the key.</typeparam>
  18. /// <typeparam name="TValue">The type of the value.</typeparam>
  19. public class MultiDictionary<TKey, TValue> : IDictionary<TKey, TValue>
  20. {
  21. #pragma warning disable CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
  22. private Dictionary<TKey, List<TValue>> _Dictionary = null;
  23. #pragma warning restore CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="MultiDictionary&lt;TKey, TValue&gt;"/> class.
  26. /// </summary>
  27. public MultiDictionary()
  28. {
  29. #pragma warning disable CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
  30. _Dictionary = new Dictionary<TKey, List<TValue>>();
  31. #pragma warning restore CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
  32. }
  33. /// <summary>
  34. /// Adds an element with the provided key and value to the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
  35. /// </summary>
  36. /// <param name="key">The object to use as the key of the element to add.</param>
  37. /// <param name="value">The object to use as the value of the element to add.</param>
  38. /// <exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.
  39. /// </exception>
  40. ///
  41. /// <exception cref="T:System.ArgumentException">
  42. /// An element with the same key already exists in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
  43. /// </exception>
  44. ///
  45. /// <exception cref="T:System.NotSupportedException">
  46. /// The <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.
  47. /// </exception>
  48. public void Add(TKey key, TValue value)
  49. {
  50. List<TValue> list;
  51. if (_Dictionary.TryGetValue(key, out list))
  52. {
  53. list.Add(value);
  54. }
  55. else
  56. {
  57. list = new List<TValue>();
  58. list.Add(value);
  59. _Dictionary.Add(key, list);
  60. }
  61. }
  62. /// <summary>
  63. /// Determines whether the <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the specified key.
  64. /// </summary>
  65. /// <param name="key">The key to locate in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.</param>
  66. /// <returns>
  67. /// true if the <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the key; otherwise, false.
  68. /// </returns>
  69. /// <exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.
  70. /// </exception>
  71. public bool ContainsKey(TKey key)
  72. {
  73. return _Dictionary.ContainsKey(key);
  74. }
  75. /// <summary>
  76. /// Removes the element with the specified key from the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
  77. /// </summary>
  78. /// <param name="key">The key of the element to remove.</param>
  79. /// <returns>
  80. /// true if the element is successfully removed; otherwise, false. This method also returns false if <paramref name="key"/> was not found in the original <see cref="T:System.Collections.Generic.IDictionary`2"/>.
  81. /// </returns>
  82. /// <exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.
  83. /// </exception>
  84. ///
  85. /// <exception cref="T:System.NotSupportedException">
  86. /// The <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.
  87. /// </exception>
  88. public bool Remove(TKey key)
  89. {
  90. return _Dictionary.Remove(key);
  91. }
  92. /// <summary>
  93. /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"/> containing the keys of the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
  94. /// </summary>
  95. /// <returns>
  96. /// An <see cref="T:System.Collections.Generic.ICollection`1"/> containing the keys of the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/>.
  97. /// </returns>
  98. public ICollection<TKey> Keys
  99. {
  100. get
  101. {
  102. return _Dictionary.Keys;
  103. }
  104. }
  105. /// <summary>
  106. /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"/> containing the values in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
  107. /// </summary>
  108. /// <returns>
  109. /// An <see cref="T:System.Collections.Generic.ICollection`1"/> containing the values in the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/>.
  110. /// </returns>
  111. public ICollection<TValue> Values
  112. {
  113. get
  114. {
  115. List<TValue> values = new List<TValue>();
  116. #pragma warning disable CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
  117. using (Dictionary<TKey, List<TValue>>.Enumerator enumerator = _Dictionary.GetEnumerator())
  118. #pragma warning restore CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
  119. {
  120. while (enumerator.MoveNext())
  121. {
  122. values.AddRange(enumerator.Current.Value);
  123. }
  124. }
  125. return values;
  126. }
  127. }
  128. bool IDictionary<TKey, TValue>.TryGetValue(TKey key, out TValue value)
  129. {
  130. throw new NotSupportedException("TryGetValue is not supported");
  131. }
  132. /// <summary>
  133. /// Gets or sets the element with the specified key.
  134. /// </summary>
  135. /// <returns>
  136. /// The element with the specified key.
  137. /// </returns>
  138. ///
  139. /// <exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.
  140. /// </exception>
  141. ///
  142. /// <exception cref="T:System.Collections.Generic.KeyNotFoundException">
  143. /// The property is retrieved and <paramref name="key"/> is not found.
  144. /// </exception>
  145. ///
  146. /// <exception cref="T:System.NotSupportedException">
  147. /// The property is set and the <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.
  148. /// </exception>
  149. TValue IDictionary<TKey, TValue>.this[TKey key]
  150. {
  151. get
  152. {
  153. return _Dictionary[key][0];
  154. }
  155. set
  156. {
  157. _Dictionary[key][0] = value;
  158. }
  159. }
  160. /// <summary>
  161. /// Gets or sets the element with the specified key.
  162. /// </summary>
  163. /// <returns>
  164. /// The element with the specified key.
  165. /// </returns>
  166. ///
  167. /// <exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.
  168. /// </exception>
  169. ///
  170. /// <exception cref="T:System.Collections.Generic.KeyNotFoundException">
  171. /// The property is retrieved and <paramref name="key"/> is not found.
  172. /// </exception>
  173. ///
  174. /// <exception cref="T:System.NotSupportedException">
  175. /// The property is set and the <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.
  176. /// </exception>
  177. public IList<TValue> this[TKey key]
  178. {
  179. get
  180. {
  181. return _Dictionary[key];
  182. }
  183. }
  184. /// <summary>
  185. /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>.
  186. /// </summary>
  187. /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
  188. /// <exception cref="T:System.NotSupportedException">
  189. /// The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
  190. /// </exception>
  191. public void Add(KeyValuePair<TKey, TValue> item)
  192. {
  193. Add(item.Key, item.Value);
  194. }
  195. /// <summary>
  196. /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
  197. /// </summary>
  198. /// <exception cref="T:System.NotSupportedException">
  199. /// The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
  200. /// </exception>
  201. public void Clear()
  202. {
  203. _Dictionary.Clear();
  204. }
  205. /// <summary>
  206. /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"/> contains a specific value.
  207. /// </summary>
  208. /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
  209. /// <returns>
  210. /// true if <paramref name="item"/> is found in the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false.
  211. /// </returns>
  212. public bool Contains(KeyValuePair<TKey, TValue> item)
  213. {
  214. List<TValue> list;
  215. if (!_Dictionary.TryGetValue(item.Key, out list))
  216. {
  217. return false;
  218. }
  219. else
  220. {
  221. return list.Contains(item.Value);
  222. }
  223. }
  224. /// <summary>
  225. /// Copies to.
  226. /// </summary>
  227. /// <param name="array">The array.</param>
  228. /// <param name="arrayIndex">Index of the array.</param>
  229. public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
  230. {
  231. if (array == null)
  232. throw new ArgumentNullException("array");
  233. if (arrayIndex < 0 || arrayIndex > array.Length)
  234. throw new ArgumentOutOfRangeException("array index out of range");
  235. if (array.Length - arrayIndex < this.Count)
  236. throw new ArgumentException("Array too small");
  237. Dictionary<TKey, List<TValue>>.Enumerator enumerator = _Dictionary.GetEnumerator();
  238. while (enumerator.MoveNext())
  239. {
  240. KeyValuePair<TKey, List<TValue>> mapPair = enumerator.Current;
  241. foreach (TValue val in mapPair.Value)
  242. {
  243. array[arrayIndex++] = new KeyValuePair<TKey, TValue>(mapPair.Key, val);
  244. }
  245. }
  246. }
  247. /// <summary>
  248. /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
  249. /// </summary>
  250. /// <returns>
  251. /// The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
  252. /// </returns>
  253. public int Count
  254. {
  255. get
  256. {
  257. int count = 0;
  258. Dictionary<TKey, List<TValue>>.Enumerator enumerator = _Dictionary.GetEnumerator();
  259. while (enumerator.MoveNext())
  260. {
  261. KeyValuePair<TKey, List<TValue>> pair = enumerator.Current;
  262. count += pair.Value.Count;
  263. }
  264. return count;
  265. }
  266. }
  267. /// <summary>
  268. /// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
  269. /// </summary>
  270. /// <returns>true if the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only; otherwise, false.
  271. /// </returns>
  272. public bool IsReadOnly
  273. {
  274. get { return false; }
  275. }
  276. /// <summary>
  277. /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
  278. /// </summary>
  279. /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
  280. /// <returns>
  281. /// true if <paramref name="item"/> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"/>.
  282. /// </returns>
  283. /// <exception cref="T:System.NotSupportedException">
  284. /// The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
  285. /// </exception>
  286. public bool Remove(KeyValuePair<TKey, TValue> item)
  287. {
  288. List<TValue> list;
  289. if (_Dictionary.TryGetValue(item.Key, out list))
  290. {
  291. return list.Remove(item.Value);
  292. }
  293. else
  294. {
  295. return false;
  296. }
  297. }
  298. /// <summary>
  299. /// Returns an enumerator that iterates through the collection.
  300. /// </summary>
  301. /// <returns>
  302. /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
  303. /// </returns>
  304. public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
  305. {
  306. Dictionary<TKey, List<TValue>>.Enumerator enumerateKeys = _Dictionary.GetEnumerator();
  307. while (enumerateKeys.MoveNext())
  308. {
  309. foreach (TValue val in enumerateKeys.Current.Value)
  310. {
  311. KeyValuePair<TKey, TValue> pair = new KeyValuePair<TKey, TValue>(
  312. enumerateKeys.Current.Key, val);
  313. yield return pair;
  314. }
  315. }
  316. }
  317. /// <summary>
  318. /// Returns an enumerator that iterates through a collection.
  319. /// </summary>
  320. /// <returns>
  321. /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
  322. /// </returns>
  323. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  324. {
  325. return GetEnumerator();
  326. }
  327. }
  328. }