/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary.cs

https://gitlab.com/0072016/0072016-corefx- · C# · 252 lines · 104 code · 19 blank · 129 comment · 4 complexity · c13b38f103645e3e788bccfacbfdf752 MD5 · raw file

  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Diagnostics.Contracts;
  7. using System.Linq;
  8. namespace System.Collections.Immutable
  9. {
  10. /// <summary>
  11. /// A set of initialization methods for instances of <see cref="ImmutableSortedDictionary{TKey, TValue}"/>.
  12. /// </summary>
  13. [SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix")]
  14. public static class ImmutableSortedDictionary
  15. {
  16. /// <summary>
  17. /// Returns an empty collection.
  18. /// </summary>
  19. /// <typeparam name="TKey">The type of keys stored by the dictionary.</typeparam>
  20. /// <typeparam name="TValue">The type of values stored by the dictionary.</typeparam>
  21. /// <returns>The immutable collection.</returns>
  22. [Pure]
  23. public static ImmutableSortedDictionary<TKey, TValue> Create<TKey, TValue>()
  24. {
  25. return ImmutableSortedDictionary<TKey, TValue>.Empty;
  26. }
  27. /// <summary>
  28. /// Returns an empty collection.
  29. /// </summary>
  30. /// <typeparam name="TKey">The type of keys stored by the dictionary.</typeparam>
  31. /// <typeparam name="TValue">The type of values stored by the dictionary.</typeparam>
  32. /// <param name="keyComparer">The key comparer.</param>
  33. /// <returns>The immutable collection.</returns>
  34. [Pure]
  35. public static ImmutableSortedDictionary<TKey, TValue> Create<TKey, TValue>(IComparer<TKey> keyComparer)
  36. {
  37. return ImmutableSortedDictionary<TKey, TValue>.Empty.WithComparers(keyComparer);
  38. }
  39. /// <summary>
  40. /// Returns an empty collection.
  41. /// </summary>
  42. /// <typeparam name="TKey">The type of keys stored by the dictionary.</typeparam>
  43. /// <typeparam name="TValue">The type of values stored by the dictionary.</typeparam>
  44. /// <param name="keyComparer">The key comparer.</param>
  45. /// <param name="valueComparer">The value comparer.</param>
  46. /// <returns>The immutable collection.</returns>
  47. [Pure]
  48. public static ImmutableSortedDictionary<TKey, TValue> Create<TKey, TValue>(IComparer<TKey> keyComparer, IEqualityComparer<TValue> valueComparer)
  49. {
  50. return ImmutableSortedDictionary<TKey, TValue>.Empty.WithComparers(keyComparer, valueComparer);
  51. }
  52. /// <summary>
  53. /// Creates a new immutable collection prefilled with the specified items.
  54. /// </summary>
  55. /// <typeparam name="TKey">The type of keys stored by the dictionary.</typeparam>
  56. /// <typeparam name="TValue">The type of values stored by the dictionary.</typeparam>
  57. /// <param name="items">The items to prepopulate.</param>
  58. /// <returns>The new immutable collection.</returns>
  59. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
  60. [Pure]
  61. public static ImmutableSortedDictionary<TKey, TValue> CreateRange<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>> items)
  62. {
  63. return ImmutableSortedDictionary<TKey, TValue>.Empty.AddRange(items);
  64. }
  65. /// <summary>
  66. /// Creates a new immutable collection prefilled with the specified items.
  67. /// </summary>
  68. /// <typeparam name="TKey">The type of keys stored by the dictionary.</typeparam>
  69. /// <typeparam name="TValue">The type of values stored by the dictionary.</typeparam>
  70. /// <param name="keyComparer">The key comparer.</param>
  71. /// <param name="items">The items to prepopulate.</param>
  72. /// <returns>The new immutable collection.</returns>
  73. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
  74. [Pure]
  75. public static ImmutableSortedDictionary<TKey, TValue> CreateRange<TKey, TValue>(IComparer<TKey> keyComparer, IEnumerable<KeyValuePair<TKey, TValue>> items)
  76. {
  77. return ImmutableSortedDictionary<TKey, TValue>.Empty.WithComparers(keyComparer).AddRange(items);
  78. }
  79. /// <summary>
  80. /// Creates a new immutable collection prefilled with the specified items.
  81. /// </summary>
  82. /// <typeparam name="TKey">The type of keys stored by the dictionary.</typeparam>
  83. /// <typeparam name="TValue">The type of values stored by the dictionary.</typeparam>
  84. /// <param name="keyComparer">The key comparer.</param>
  85. /// <param name="valueComparer">The value comparer.</param>
  86. /// <param name="items">The items to prepopulate.</param>
  87. /// <returns>The new immutable collection.</returns>
  88. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
  89. [Pure]
  90. public static ImmutableSortedDictionary<TKey, TValue> CreateRange<TKey, TValue>(IComparer<TKey> keyComparer, IEqualityComparer<TValue> valueComparer, IEnumerable<KeyValuePair<TKey, TValue>> items)
  91. {
  92. return ImmutableSortedDictionary<TKey, TValue>.Empty.WithComparers(keyComparer, valueComparer).AddRange(items);
  93. }
  94. /// <summary>
  95. /// Creates a new immutable sorted dictionary builder.
  96. /// </summary>
  97. /// <typeparam name="TKey">The type of keys stored by the dictionary.</typeparam>
  98. /// <typeparam name="TValue">The type of values stored by the dictionary.</typeparam>
  99. /// <returns>The immutable collection builder.</returns>
  100. [Pure]
  101. public static ImmutableSortedDictionary<TKey, TValue>.Builder CreateBuilder<TKey, TValue>()
  102. {
  103. return Create<TKey, TValue>().ToBuilder();
  104. }
  105. /// <summary>
  106. /// Creates a new immutable sorted dictionary builder.
  107. /// </summary>
  108. /// <typeparam name="TKey">The type of keys stored by the dictionary.</typeparam>
  109. /// <typeparam name="TValue">The type of values stored by the dictionary.</typeparam>
  110. /// <param name="keyComparer">The key comparer.</param>
  111. /// <returns>The immutable collection builder.</returns>
  112. [Pure]
  113. public static ImmutableSortedDictionary<TKey, TValue>.Builder CreateBuilder<TKey, TValue>(IComparer<TKey> keyComparer)
  114. {
  115. return Create<TKey, TValue>(keyComparer).ToBuilder();
  116. }
  117. /// <summary>
  118. /// Creates a new immutable sorted dictionary builder.
  119. /// </summary>
  120. /// <typeparam name="TKey">The type of keys stored by the dictionary.</typeparam>
  121. /// <typeparam name="TValue">The type of values stored by the dictionary.</typeparam>
  122. /// <param name="keyComparer">The key comparer.</param>
  123. /// <param name="valueComparer">The value comparer.</param>
  124. /// <returns>The immutable collection builder.</returns>
  125. [Pure]
  126. public static ImmutableSortedDictionary<TKey, TValue>.Builder CreateBuilder<TKey, TValue>(IComparer<TKey> keyComparer, IEqualityComparer<TValue> valueComparer)
  127. {
  128. return Create<TKey, TValue>(keyComparer, valueComparer).ToBuilder();
  129. }
  130. /// <summary>
  131. /// Constructs an immutable sorted dictionary based on some transformation of a sequence.
  132. /// </summary>
  133. /// <typeparam name="TSource">The type of element in the sequence.</typeparam>
  134. /// <typeparam name="TKey">The type of key in the resulting map.</typeparam>
  135. /// <typeparam name="TValue">The type of value in the resulting map.</typeparam>
  136. /// <param name="source">The sequence to enumerate to generate the map.</param>
  137. /// <param name="keySelector">The function that will produce the key for the map from each sequence element.</param>
  138. /// <param name="elementSelector">The function that will produce the value for the map from each sequence element.</param>
  139. /// <param name="keyComparer">The key comparer to use for the map.</param>
  140. /// <param name="valueComparer">The value comparer to use for the map.</param>
  141. /// <returns>The immutable map.</returns>
  142. [Pure]
  143. public static ImmutableSortedDictionary<TKey, TValue> ToImmutableSortedDictionary<TSource, TKey, TValue>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TValue> elementSelector, IComparer<TKey> keyComparer, IEqualityComparer<TValue> valueComparer)
  144. {
  145. Requires.NotNull(source, nameof(source));
  146. Requires.NotNull(keySelector, nameof(keySelector));
  147. Requires.NotNull(elementSelector, nameof(elementSelector));
  148. Contract.Ensures(Contract.Result<ImmutableDictionary<TKey, TValue>>() != null);
  149. return ImmutableSortedDictionary<TKey, TValue>.Empty.WithComparers(keyComparer, valueComparer)
  150. .AddRange(source.Select(element => new KeyValuePair<TKey, TValue>(keySelector(element), elementSelector(element))));
  151. }
  152. /// <summary>
  153. /// Constructs an immutable sorted dictionary based on some transformation of a sequence.
  154. /// </summary>
  155. /// <typeparam name="TSource">The type of element in the sequence.</typeparam>
  156. /// <typeparam name="TKey">The type of key in the resulting map.</typeparam>
  157. /// <typeparam name="TValue">The type of value in the resulting map.</typeparam>
  158. /// <param name="source">The sequence to enumerate to generate the map.</param>
  159. /// <param name="keySelector">The function that will produce the key for the map from each sequence element.</param>
  160. /// <param name="elementSelector">The function that will produce the value for the map from each sequence element.</param>
  161. /// <param name="keyComparer">The key comparer to use for the map.</param>
  162. /// <returns>The immutable map.</returns>
  163. [Pure]
  164. public static ImmutableSortedDictionary<TKey, TValue> ToImmutableSortedDictionary<TSource, TKey, TValue>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TValue> elementSelector, IComparer<TKey> keyComparer)
  165. {
  166. return ToImmutableSortedDictionary(source, keySelector, elementSelector, keyComparer, null);
  167. }
  168. /// <summary>
  169. /// Constructs an immutable sorted dictionary based on some transformation of a sequence.
  170. /// </summary>
  171. /// <typeparam name="TSource">The type of element in the sequence.</typeparam>
  172. /// <typeparam name="TKey">The type of key in the resulting map.</typeparam>
  173. /// <typeparam name="TValue">The type of value in the resulting map.</typeparam>
  174. /// <param name="source">The sequence to enumerate to generate the map.</param>
  175. /// <param name="keySelector">The function that will produce the key for the map from each sequence element.</param>
  176. /// <param name="elementSelector">The function that will produce the value for the map from each sequence element.</param>
  177. /// <returns>The immutable map.</returns>
  178. [Pure]
  179. public static ImmutableSortedDictionary<TKey, TValue> ToImmutableSortedDictionary<TSource, TKey, TValue>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TValue> elementSelector)
  180. {
  181. return ToImmutableSortedDictionary(source, keySelector, elementSelector, null, null);
  182. }
  183. /// <summary>
  184. /// Creates an immutable sorted dictionary given a sequence of key=value pairs.
  185. /// </summary>
  186. /// <typeparam name="TKey">The type of key in the map.</typeparam>
  187. /// <typeparam name="TValue">The type of value in the map.</typeparam>
  188. /// <param name="source">The sequence of key=value pairs.</param>
  189. /// <param name="keyComparer">The key comparer to use when building the immutable map.</param>
  190. /// <param name="valueComparer">The value comparer to use for the immutable map.</param>
  191. /// <returns>An immutable map.</returns>
  192. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
  193. [Pure]
  194. public static ImmutableSortedDictionary<TKey, TValue> ToImmutableSortedDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source, IComparer<TKey> keyComparer, IEqualityComparer<TValue> valueComparer)
  195. {
  196. Requires.NotNull(source, nameof(source));
  197. Contract.Ensures(Contract.Result<ImmutableDictionary<TKey, TValue>>() != null);
  198. var existingDictionary = source as ImmutableSortedDictionary<TKey, TValue>;
  199. if (existingDictionary != null)
  200. {
  201. return existingDictionary.WithComparers(keyComparer, valueComparer);
  202. }
  203. return ImmutableSortedDictionary<TKey, TValue>.Empty.WithComparers(keyComparer, valueComparer).AddRange(source);
  204. }
  205. /// <summary>
  206. /// Creates an immutable sorted dictionary given a sequence of key=value pairs.
  207. /// </summary>
  208. /// <typeparam name="TKey">The type of key in the map.</typeparam>
  209. /// <typeparam name="TValue">The type of value in the map.</typeparam>
  210. /// <param name="source">The sequence of key=value pairs.</param>
  211. /// <param name="keyComparer">The key comparer to use when building the immutable map.</param>
  212. /// <returns>An immutable map.</returns>
  213. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
  214. [Pure]
  215. public static ImmutableSortedDictionary<TKey, TValue> ToImmutableSortedDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source, IComparer<TKey> keyComparer)
  216. {
  217. return ToImmutableSortedDictionary(source, keyComparer, null);
  218. }
  219. /// <summary>
  220. /// Creates an immutable sorted dictionary given a sequence of key=value pairs.
  221. /// </summary>
  222. /// <typeparam name="TKey">The type of key in the map.</typeparam>
  223. /// <typeparam name="TValue">The type of value in the map.</typeparam>
  224. /// <param name="source">The sequence of key=value pairs.</param>
  225. /// <returns>An immutable map.</returns>
  226. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
  227. [Pure]
  228. public static ImmutableSortedDictionary<TKey, TValue> ToImmutableSortedDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source)
  229. {
  230. return ToImmutableSortedDictionary(source, null, null);
  231. }
  232. }
  233. }