Distinct.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerableEx
  11. {
  12. public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  13. {
  14. if (source == null)
  15. throw new ArgumentNullException(nameof(source));
  16. if (keySelector == null)
  17. throw new ArgumentNullException(nameof(keySelector));
  18. return DistinctCore(source, keySelector, EqualityComparer<TKey>.Default);
  19. }
  20. public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  21. {
  22. if (source == null)
  23. throw new ArgumentNullException(nameof(source));
  24. if (keySelector == null)
  25. throw new ArgumentNullException(nameof(keySelector));
  26. if (comparer == null)
  27. throw new ArgumentNullException(nameof(comparer));
  28. return DistinctCore(source, keySelector, comparer);
  29. }
  30. public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector)
  31. {
  32. if (source == null)
  33. throw new ArgumentNullException(nameof(source));
  34. if (keySelector == null)
  35. throw new ArgumentNullException(nameof(keySelector));
  36. return DistinctCore(source, keySelector, EqualityComparer<TKey>.Default);
  37. }
  38. public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  39. {
  40. if (source == null)
  41. throw new ArgumentNullException(nameof(source));
  42. if (keySelector == null)
  43. throw new ArgumentNullException(nameof(keySelector));
  44. if (comparer == null)
  45. throw new ArgumentNullException(nameof(comparer));
  46. return DistinctCore(source, keySelector, comparer);
  47. }
  48. private static IAsyncEnumerable<TSource> DistinctCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  49. {
  50. return new DistinctAsyncIterator<TSource, TKey>(source, keySelector, comparer);
  51. }
  52. private static IAsyncEnumerable<TSource> DistinctCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  53. {
  54. return new DistinctAsyncIteratorWithTask<TSource, TKey>(source, keySelector, comparer);
  55. }
  56. private sealed class DistinctAsyncIterator<TSource, TKey> : AsyncIterator<TSource>, IIListProvider<TSource>
  57. {
  58. private readonly IEqualityComparer<TKey> comparer;
  59. private readonly Func<TSource, TKey> keySelector;
  60. private readonly IAsyncEnumerable<TSource> source;
  61. private IAsyncEnumerator<TSource> enumerator;
  62. private Set<TKey> set;
  63. public DistinctAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  64. {
  65. Debug.Assert(source != null);
  66. Debug.Assert(keySelector != null);
  67. Debug.Assert(comparer != null);
  68. this.source = source;
  69. this.keySelector = keySelector;
  70. this.comparer = comparer;
  71. }
  72. public async Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  73. {
  74. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  75. return s.ToArray();
  76. }
  77. public async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  78. {
  79. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  80. return s;
  81. }
  82. public async Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  83. {
  84. if (onlyIfCheap)
  85. {
  86. return -1;
  87. }
  88. var count = 0;
  89. var s = new Set<TKey>(comparer);
  90. var enu = source.GetAsyncEnumerator();
  91. try
  92. {
  93. while (await enu.MoveNextAsync().ConfigureAwait(false))
  94. {
  95. var item = enu.Current;
  96. if (s.Add(keySelector(item)))
  97. {
  98. count++;
  99. }
  100. }
  101. }
  102. finally
  103. {
  104. await enu.DisposeAsync().ConfigureAwait(false);
  105. }
  106. return count;
  107. }
  108. public override AsyncIterator<TSource> Clone()
  109. {
  110. return new DistinctAsyncIterator<TSource, TKey>(source, keySelector, comparer);
  111. }
  112. public override async Task DisposeAsync()
  113. {
  114. if (enumerator != null)
  115. {
  116. await enumerator.DisposeAsync().ConfigureAwait(false);
  117. enumerator = null;
  118. set = null;
  119. }
  120. await base.DisposeAsync().ConfigureAwait(false);
  121. }
  122. protected override async Task<bool> MoveNextCore()
  123. {
  124. switch (state)
  125. {
  126. case AsyncIteratorState.Allocated:
  127. enumerator = source.GetAsyncEnumerator();
  128. if (!await enumerator.MoveNextAsync().ConfigureAwait(false))
  129. {
  130. await DisposeAsync().ConfigureAwait(false);
  131. return false;
  132. }
  133. var element = enumerator.Current;
  134. set = new Set<TKey>(comparer);
  135. set.Add(keySelector(element));
  136. current = element;
  137. state = AsyncIteratorState.Iterating;
  138. return true;
  139. case AsyncIteratorState.Iterating:
  140. while (await enumerator.MoveNextAsync().ConfigureAwait(false))
  141. {
  142. element = enumerator.Current;
  143. if (set.Add(keySelector(element)))
  144. {
  145. current = element;
  146. return true;
  147. }
  148. }
  149. break;
  150. }
  151. await DisposeAsync().ConfigureAwait(false);
  152. return false;
  153. }
  154. private async Task<List<TSource>> FillSetAsync(CancellationToken cancellationToken)
  155. {
  156. var s = new Set<TKey>(comparer);
  157. var r = new List<TSource>();
  158. var enu = source.GetAsyncEnumerator();
  159. try
  160. {
  161. while (await enu.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  162. {
  163. var item = enu.Current;
  164. if (s.Add(keySelector(item)))
  165. {
  166. r.Add(item);
  167. }
  168. }
  169. }
  170. finally
  171. {
  172. await enu.DisposeAsync().ConfigureAwait(false);
  173. }
  174. return r;
  175. }
  176. }
  177. private sealed class DistinctAsyncIteratorWithTask<TSource, TKey> : AsyncIterator<TSource>, IIListProvider<TSource>
  178. {
  179. private readonly IEqualityComparer<TKey> comparer;
  180. private readonly Func<TSource, Task<TKey>> keySelector;
  181. private readonly IAsyncEnumerable<TSource> source;
  182. private IAsyncEnumerator<TSource> enumerator;
  183. private Set<TKey> set;
  184. public DistinctAsyncIteratorWithTask(IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  185. {
  186. Debug.Assert(source != null);
  187. Debug.Assert(keySelector != null);
  188. Debug.Assert(comparer != null);
  189. this.source = source;
  190. this.keySelector = keySelector;
  191. this.comparer = comparer;
  192. }
  193. public async Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  194. {
  195. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  196. return s.ToArray();
  197. }
  198. public async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  199. {
  200. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  201. return s;
  202. }
  203. public async Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  204. {
  205. if (onlyIfCheap)
  206. {
  207. return -1;
  208. }
  209. var count = 0;
  210. var s = new Set<TKey>(comparer);
  211. var enu = source.GetAsyncEnumerator();
  212. try
  213. {
  214. while (await enu.MoveNextAsync().ConfigureAwait(false))
  215. {
  216. var item = enu.Current;
  217. if (s.Add(await keySelector(item).ConfigureAwait(false)))
  218. {
  219. count++;
  220. }
  221. }
  222. }
  223. finally
  224. {
  225. await enu.DisposeAsync().ConfigureAwait(false);
  226. }
  227. return count;
  228. }
  229. public override AsyncIterator<TSource> Clone()
  230. {
  231. return new DistinctAsyncIteratorWithTask<TSource, TKey>(source, keySelector, comparer);
  232. }
  233. public override async Task DisposeAsync()
  234. {
  235. if (enumerator != null)
  236. {
  237. await enumerator.DisposeAsync().ConfigureAwait(false);
  238. enumerator = null;
  239. set = null;
  240. }
  241. await base.DisposeAsync().ConfigureAwait(false);
  242. }
  243. protected override async Task<bool> MoveNextCore()
  244. {
  245. switch (state)
  246. {
  247. case AsyncIteratorState.Allocated:
  248. enumerator = source.GetAsyncEnumerator();
  249. if (!await enumerator.MoveNextAsync().ConfigureAwait(false))
  250. {
  251. await DisposeAsync().ConfigureAwait(false);
  252. return false;
  253. }
  254. var element = enumerator.Current;
  255. set = new Set<TKey>(comparer);
  256. set.Add(await keySelector(element).ConfigureAwait(false));
  257. current = element;
  258. state = AsyncIteratorState.Iterating;
  259. return true;
  260. case AsyncIteratorState.Iterating:
  261. while (await enumerator.MoveNextAsync().ConfigureAwait(false))
  262. {
  263. element = enumerator.Current;
  264. if (set.Add(await keySelector(element).ConfigureAwait(false)))
  265. {
  266. current = element;
  267. return true;
  268. }
  269. }
  270. break;
  271. }
  272. await DisposeAsync().ConfigureAwait(false);
  273. return false;
  274. }
  275. private async Task<List<TSource>> FillSetAsync(CancellationToken cancellationToken)
  276. {
  277. var s = new Set<TKey>(comparer);
  278. var r = new List<TSource>();
  279. var enu = source.GetAsyncEnumerator();
  280. try
  281. {
  282. while (await enu.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  283. {
  284. var item = enu.Current;
  285. if (s.Add(await keySelector(item).ConfigureAwait(false)))
  286. {
  287. r.Add(item);
  288. }
  289. }
  290. }
  291. finally
  292. {
  293. await enu.DisposeAsync().ConfigureAwait(false);
  294. }
  295. return r;
  296. }
  297. }
  298. }
  299. }