ToDictionary.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using Cysharp.Threading.Tasks.Internal;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. namespace Cysharp.Threading.Tasks.Linq
  6. {
  7. public static partial class UniTaskAsyncEnumerable
  8. {
  9. public static UniTask<Dictionary<TKey, TSource>> ToDictionaryAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken = default)
  10. {
  11. Error.ThrowArgumentNullException(source, nameof(source));
  12. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  13. return ToDictionary.ToDictionaryAsync(source, keySelector, EqualityComparer<TKey>.Default, cancellationToken);
  14. }
  15. public static UniTask<Dictionary<TKey, TSource>> ToDictionaryAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
  16. {
  17. Error.ThrowArgumentNullException(source, nameof(source));
  18. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  19. Error.ThrowArgumentNullException(comparer, nameof(comparer));
  20. return ToDictionary.ToDictionaryAsync(source, keySelector, comparer, cancellationToken);
  21. }
  22. public static UniTask<Dictionary<TKey, TElement>> ToDictionaryAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, CancellationToken cancellationToken = default)
  23. {
  24. Error.ThrowArgumentNullException(source, nameof(source));
  25. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  26. Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
  27. return ToDictionary.ToDictionaryAsync(source, keySelector, elementSelector, EqualityComparer<TKey>.Default, cancellationToken);
  28. }
  29. public static UniTask<Dictionary<TKey, TElement>> ToDictionaryAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
  30. {
  31. Error.ThrowArgumentNullException(source, nameof(source));
  32. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  33. Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
  34. Error.ThrowArgumentNullException(comparer, nameof(comparer));
  35. return ToDictionary.ToDictionaryAsync(source, keySelector, elementSelector, comparer, cancellationToken);
  36. }
  37. public static UniTask<Dictionary<TKey, TSource>> ToDictionaryAwaitAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, CancellationToken cancellationToken = default)
  38. {
  39. Error.ThrowArgumentNullException(source, nameof(source));
  40. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  41. return ToDictionary.ToDictionaryAwaitAsync(source, keySelector, EqualityComparer<TKey>.Default, cancellationToken);
  42. }
  43. public static UniTask<Dictionary<TKey, TSource>> ToDictionaryAwaitAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
  44. {
  45. Error.ThrowArgumentNullException(source, nameof(source));
  46. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  47. Error.ThrowArgumentNullException(comparer, nameof(comparer));
  48. return ToDictionary.ToDictionaryAwaitAsync(source, keySelector, comparer, cancellationToken);
  49. }
  50. public static UniTask<Dictionary<TKey, TElement>> ToDictionaryAwaitAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TSource, UniTask<TElement>> elementSelector, CancellationToken cancellationToken = default)
  51. {
  52. Error.ThrowArgumentNullException(source, nameof(source));
  53. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  54. Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
  55. return ToDictionary.ToDictionaryAwaitAsync(source, keySelector, elementSelector, EqualityComparer<TKey>.Default, cancellationToken);
  56. }
  57. public static UniTask<Dictionary<TKey, TElement>> ToDictionaryAwaitAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TSource, UniTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
  58. {
  59. Error.ThrowArgumentNullException(source, nameof(source));
  60. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  61. Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
  62. Error.ThrowArgumentNullException(comparer, nameof(comparer));
  63. return ToDictionary.ToDictionaryAwaitAsync(source, keySelector, elementSelector, comparer, cancellationToken);
  64. }
  65. public static UniTask<Dictionary<TKey, TSource>> ToDictionaryAwaitWithCancellationAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, CancellationToken cancellationToken = default)
  66. {
  67. Error.ThrowArgumentNullException(source, nameof(source));
  68. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  69. return ToDictionary.ToDictionaryAwaitWithCancellationAsync(source, keySelector, EqualityComparer<TKey>.Default, cancellationToken);
  70. }
  71. public static UniTask<Dictionary<TKey, TSource>> ToDictionaryAwaitWithCancellationAsync<TSource, TKey>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
  72. {
  73. Error.ThrowArgumentNullException(source, nameof(source));
  74. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  75. Error.ThrowArgumentNullException(comparer, nameof(comparer));
  76. return ToDictionary.ToDictionaryAwaitWithCancellationAsync(source, keySelector, comparer, cancellationToken);
  77. }
  78. public static UniTask<Dictionary<TKey, TElement>> ToDictionaryAwaitWithCancellationAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TSource, CancellationToken, UniTask<TElement>> elementSelector, CancellationToken cancellationToken = default)
  79. {
  80. Error.ThrowArgumentNullException(source, nameof(source));
  81. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  82. Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
  83. return ToDictionary.ToDictionaryAwaitWithCancellationAsync(source, keySelector, elementSelector, EqualityComparer<TKey>.Default, cancellationToken);
  84. }
  85. public static UniTask<Dictionary<TKey, TElement>> ToDictionaryAwaitWithCancellationAsync<TSource, TKey, TElement>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TSource, CancellationToken, UniTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default)
  86. {
  87. Error.ThrowArgumentNullException(source, nameof(source));
  88. Error.ThrowArgumentNullException(keySelector, nameof(keySelector));
  89. Error.ThrowArgumentNullException(elementSelector, nameof(elementSelector));
  90. Error.ThrowArgumentNullException(comparer, nameof(comparer));
  91. return ToDictionary.ToDictionaryAwaitWithCancellationAsync(source, keySelector, elementSelector, comparer, cancellationToken);
  92. }
  93. }
  94. internal static class ToDictionary
  95. {
  96. internal static async UniTask<Dictionary<TKey, TSource>> ToDictionaryAsync<TSource, TKey>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  97. {
  98. var dict = new Dictionary<TKey, TSource>(comparer);
  99. var e = source.GetAsyncEnumerator(cancellationToken);
  100. try
  101. {
  102. while (await e.MoveNextAsync())
  103. {
  104. var v = e.Current;
  105. var key = keySelector(v);
  106. dict.Add(key, v);
  107. }
  108. }
  109. finally
  110. {
  111. if (e != null)
  112. {
  113. await e.DisposeAsync();
  114. }
  115. }
  116. return dict;
  117. }
  118. internal static async UniTask<Dictionary<TKey, TElement>> ToDictionaryAsync<TSource, TKey, TElement>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  119. {
  120. var dict = new Dictionary<TKey, TElement>(comparer);
  121. var e = source.GetAsyncEnumerator(cancellationToken);
  122. try
  123. {
  124. while (await e.MoveNextAsync())
  125. {
  126. var v = e.Current;
  127. var key = keySelector(v);
  128. var value = elementSelector(v);
  129. dict.Add(key, value);
  130. }
  131. }
  132. finally
  133. {
  134. if (e != null)
  135. {
  136. await e.DisposeAsync();
  137. }
  138. }
  139. return dict;
  140. }
  141. // with await
  142. internal static async UniTask<Dictionary<TKey, TSource>> ToDictionaryAwaitAsync<TSource, TKey>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  143. {
  144. var dict = new Dictionary<TKey, TSource>(comparer);
  145. var e = source.GetAsyncEnumerator(cancellationToken);
  146. try
  147. {
  148. while (await e.MoveNextAsync())
  149. {
  150. var v = e.Current;
  151. var key = await keySelector(v);
  152. dict.Add(key, v);
  153. }
  154. }
  155. finally
  156. {
  157. if (e != null)
  158. {
  159. await e.DisposeAsync();
  160. }
  161. }
  162. return dict;
  163. }
  164. internal static async UniTask<Dictionary<TKey, TElement>> ToDictionaryAwaitAsync<TSource, TKey, TElement>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, Func<TSource, UniTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  165. {
  166. var dict = new Dictionary<TKey, TElement>(comparer);
  167. var e = source.GetAsyncEnumerator(cancellationToken);
  168. try
  169. {
  170. while (await e.MoveNextAsync())
  171. {
  172. var v = e.Current;
  173. var key = await keySelector(v);
  174. var value = await elementSelector(v);
  175. dict.Add(key, value);
  176. }
  177. }
  178. finally
  179. {
  180. if (e != null)
  181. {
  182. await e.DisposeAsync();
  183. }
  184. }
  185. return dict;
  186. }
  187. // with cancellation
  188. internal static async UniTask<Dictionary<TKey, TSource>> ToDictionaryAwaitWithCancellationAsync<TSource, TKey>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  189. {
  190. var dict = new Dictionary<TKey, TSource>(comparer);
  191. var e = source.GetAsyncEnumerator(cancellationToken);
  192. try
  193. {
  194. while (await e.MoveNextAsync())
  195. {
  196. var v = e.Current;
  197. var key = await keySelector(v, cancellationToken);
  198. dict.Add(key, v);
  199. }
  200. }
  201. finally
  202. {
  203. if (e != null)
  204. {
  205. await e.DisposeAsync();
  206. }
  207. }
  208. return dict;
  209. }
  210. internal static async UniTask<Dictionary<TKey, TElement>> ToDictionaryAwaitWithCancellationAsync<TSource, TKey, TElement>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, Func<TSource, CancellationToken, UniTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  211. {
  212. var dict = new Dictionary<TKey, TElement>(comparer);
  213. var e = source.GetAsyncEnumerator(cancellationToken);
  214. try
  215. {
  216. while (await e.MoveNextAsync())
  217. {
  218. var v = e.Current;
  219. var key = await keySelector(v, cancellationToken);
  220. var value = await elementSelector(v, cancellationToken);
  221. dict.Add(key, value);
  222. }
  223. }
  224. finally
  225. {
  226. if (e != null)
  227. {
  228. await e.DisposeAsync();
  229. }
  230. }
  231. return dict;
  232. }
  233. }
  234. }