Distinct.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 source.Distinct(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 new DistinctAsyncIterator<TSource, TKey>(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 source.Distinct(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 new DistinctAsyncIteratorWithTask<TSource, TKey>(source, keySelector, comparer);
  47. }
  48. private sealed class DistinctAsyncIterator<TSource, TKey> : AsyncIterator<TSource>, IIListProvider<TSource>
  49. {
  50. private readonly IEqualityComparer<TKey> comparer;
  51. private readonly Func<TSource, TKey> keySelector;
  52. private readonly IAsyncEnumerable<TSource> source;
  53. private IAsyncEnumerator<TSource> enumerator;
  54. private Set<TKey> set;
  55. public DistinctAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  56. {
  57. Debug.Assert(source != null);
  58. Debug.Assert(keySelector != null);
  59. Debug.Assert(comparer != null);
  60. this.source = source;
  61. this.keySelector = keySelector;
  62. this.comparer = comparer;
  63. }
  64. public async Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  65. {
  66. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  67. return s.ToArray();
  68. }
  69. public async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  70. {
  71. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  72. return s;
  73. }
  74. public async Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  75. {
  76. if (onlyIfCheap)
  77. {
  78. return -1;
  79. }
  80. var count = 0;
  81. var s = new Set<TKey>(comparer);
  82. var enu = source.GetAsyncEnumerator();
  83. try
  84. {
  85. while (await enu.MoveNextAsync().ConfigureAwait(false))
  86. {
  87. var item = enu.Current;
  88. if (s.Add(keySelector(item)))
  89. {
  90. count++;
  91. }
  92. }
  93. }
  94. finally
  95. {
  96. await enu.DisposeAsync().ConfigureAwait(false);
  97. }
  98. return count;
  99. }
  100. public override AsyncIterator<TSource> Clone()
  101. {
  102. return new DistinctAsyncIterator<TSource, TKey>(source, keySelector, comparer);
  103. }
  104. public override async Task DisposeAsync()
  105. {
  106. if (enumerator != null)
  107. {
  108. await enumerator.DisposeAsync().ConfigureAwait(false);
  109. enumerator = null;
  110. set = null;
  111. }
  112. await base.DisposeAsync().ConfigureAwait(false);
  113. }
  114. protected override async Task<bool> MoveNextCore()
  115. {
  116. switch (state)
  117. {
  118. case AsyncIteratorState.Allocated:
  119. enumerator = source.GetAsyncEnumerator();
  120. if (!await enumerator.MoveNextAsync().ConfigureAwait(false))
  121. {
  122. await DisposeAsync().ConfigureAwait(false);
  123. return false;
  124. }
  125. var element = enumerator.Current;
  126. set = new Set<TKey>(comparer);
  127. set.Add(keySelector(element));
  128. current = element;
  129. state = AsyncIteratorState.Iterating;
  130. return true;
  131. case AsyncIteratorState.Iterating:
  132. while (await enumerator.MoveNextAsync().ConfigureAwait(false))
  133. {
  134. element = enumerator.Current;
  135. if (set.Add(keySelector(element)))
  136. {
  137. current = element;
  138. return true;
  139. }
  140. }
  141. break;
  142. }
  143. await DisposeAsync().ConfigureAwait(false);
  144. return false;
  145. }
  146. private async Task<List<TSource>> FillSetAsync(CancellationToken cancellationToken)
  147. {
  148. var s = new Set<TKey>(comparer);
  149. var r = new List<TSource>();
  150. var enu = source.GetAsyncEnumerator();
  151. try
  152. {
  153. while (await enu.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  154. {
  155. var item = enu.Current;
  156. if (s.Add(keySelector(item)))
  157. {
  158. r.Add(item);
  159. }
  160. }
  161. }
  162. finally
  163. {
  164. await enu.DisposeAsync().ConfigureAwait(false);
  165. }
  166. return r;
  167. }
  168. }
  169. private sealed class DistinctAsyncIteratorWithTask<TSource, TKey> : AsyncIterator<TSource>, IIListProvider<TSource>
  170. {
  171. private readonly IEqualityComparer<TKey> comparer;
  172. private readonly Func<TSource, Task<TKey>> keySelector;
  173. private readonly IAsyncEnumerable<TSource> source;
  174. private IAsyncEnumerator<TSource> enumerator;
  175. private Set<TKey> set;
  176. public DistinctAsyncIteratorWithTask(IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  177. {
  178. Debug.Assert(source != null);
  179. Debug.Assert(keySelector != null);
  180. Debug.Assert(comparer != null);
  181. this.source = source;
  182. this.keySelector = keySelector;
  183. this.comparer = comparer;
  184. }
  185. public async Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  186. {
  187. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  188. return s.ToArray();
  189. }
  190. public async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  191. {
  192. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  193. return s;
  194. }
  195. public async Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  196. {
  197. if (onlyIfCheap)
  198. {
  199. return -1;
  200. }
  201. var count = 0;
  202. var s = new Set<TKey>(comparer);
  203. var enu = source.GetAsyncEnumerator();
  204. try
  205. {
  206. while (await enu.MoveNextAsync().ConfigureAwait(false))
  207. {
  208. var item = enu.Current;
  209. if (s.Add(await keySelector(item).ConfigureAwait(false)))
  210. {
  211. count++;
  212. }
  213. }
  214. }
  215. finally
  216. {
  217. await enu.DisposeAsync().ConfigureAwait(false);
  218. }
  219. return count;
  220. }
  221. public override AsyncIterator<TSource> Clone()
  222. {
  223. return new DistinctAsyncIteratorWithTask<TSource, TKey>(source, keySelector, comparer);
  224. }
  225. public override async Task DisposeAsync()
  226. {
  227. if (enumerator != null)
  228. {
  229. await enumerator.DisposeAsync().ConfigureAwait(false);
  230. enumerator = null;
  231. set = null;
  232. }
  233. await base.DisposeAsync().ConfigureAwait(false);
  234. }
  235. protected override async Task<bool> MoveNextCore()
  236. {
  237. switch (state)
  238. {
  239. case AsyncIteratorState.Allocated:
  240. enumerator = source.GetAsyncEnumerator();
  241. if (!await enumerator.MoveNextAsync().ConfigureAwait(false))
  242. {
  243. await DisposeAsync().ConfigureAwait(false);
  244. return false;
  245. }
  246. var element = enumerator.Current;
  247. set = new Set<TKey>(comparer);
  248. set.Add(await keySelector(element).ConfigureAwait(false));
  249. current = element;
  250. state = AsyncIteratorState.Iterating;
  251. return true;
  252. case AsyncIteratorState.Iterating:
  253. while (await enumerator.MoveNextAsync().ConfigureAwait(false))
  254. {
  255. element = enumerator.Current;
  256. if (set.Add(await keySelector(element).ConfigureAwait(false)))
  257. {
  258. current = element;
  259. return true;
  260. }
  261. }
  262. break;
  263. }
  264. await DisposeAsync().ConfigureAwait(false);
  265. return false;
  266. }
  267. private async Task<List<TSource>> FillSetAsync(CancellationToken cancellationToken)
  268. {
  269. var s = new Set<TKey>(comparer);
  270. var r = new List<TSource>();
  271. var enu = source.GetAsyncEnumerator();
  272. try
  273. {
  274. while (await enu.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  275. {
  276. var item = enu.Current;
  277. if (s.Add(await keySelector(item).ConfigureAwait(false)))
  278. {
  279. r.Add(item);
  280. }
  281. }
  282. }
  283. finally
  284. {
  285. await enu.DisposeAsync().ConfigureAwait(false);
  286. }
  287. return r;
  288. }
  289. }
  290. }
  291. }