Distinct.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 AsyncEnumerable
  11. {
  12. public static IAsyncEnumerable<TSource> Distinct<TSource>(this IAsyncEnumerable<TSource> source)
  13. {
  14. if (source == null)
  15. throw new ArgumentNullException(nameof(source));
  16. return source.Distinct(EqualityComparer<TSource>.Default);
  17. }
  18. public static IAsyncEnumerable<TSource> Distinct<TSource>(this IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  19. {
  20. if (source == null)
  21. throw new ArgumentNullException(nameof(source));
  22. if (comparer == null)
  23. throw new ArgumentNullException(nameof(comparer));
  24. return new DistinctAsyncIterator<TSource>(source, comparer);
  25. }
  26. public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  27. {
  28. if (source == null)
  29. throw new ArgumentNullException(nameof(source));
  30. if (keySelector == null)
  31. throw new ArgumentNullException(nameof(keySelector));
  32. return source.Distinct(keySelector, EqualityComparer<TKey>.Default);
  33. }
  34. public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  35. {
  36. if (source == null)
  37. throw new ArgumentNullException(nameof(source));
  38. if (keySelector == null)
  39. throw new ArgumentNullException(nameof(keySelector));
  40. if (comparer == null)
  41. throw new ArgumentNullException(nameof(comparer));
  42. return new DistinctAsyncIterator<TSource, TKey>(source, keySelector, comparer);
  43. }
  44. public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector)
  45. {
  46. if (source == null)
  47. throw new ArgumentNullException(nameof(source));
  48. if (keySelector == null)
  49. throw new ArgumentNullException(nameof(keySelector));
  50. return source.Distinct(keySelector, EqualityComparer<TKey>.Default);
  51. }
  52. public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  53. {
  54. if (source == null)
  55. throw new ArgumentNullException(nameof(source));
  56. if (keySelector == null)
  57. throw new ArgumentNullException(nameof(keySelector));
  58. if (comparer == null)
  59. throw new ArgumentNullException(nameof(comparer));
  60. return new DistinctAsyncIteratorWithTask<TSource, TKey>(source, keySelector, comparer);
  61. }
  62. private sealed class DistinctAsyncIterator<TSource> : AsyncIterator<TSource>, IIListProvider<TSource>
  63. {
  64. private readonly IEqualityComparer<TSource> comparer;
  65. private readonly IAsyncEnumerable<TSource> source;
  66. private IAsyncEnumerator<TSource> enumerator;
  67. private Set<TSource> set;
  68. public DistinctAsyncIterator(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  69. {
  70. Debug.Assert(source != null);
  71. this.source = source;
  72. this.comparer = comparer;
  73. }
  74. public async Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  75. {
  76. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  77. return s.ToArray();
  78. }
  79. public async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  80. {
  81. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  82. return s.ToList();
  83. }
  84. public async Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  85. {
  86. return onlyIfCheap ? -1 : (await FillSetAsync(cancellationToken).ConfigureAwait(false)).Count;
  87. }
  88. public override AsyncIterator<TSource> Clone()
  89. {
  90. return new DistinctAsyncIterator<TSource>(source, comparer);
  91. }
  92. public override async Task DisposeAsync()
  93. {
  94. if (enumerator != null)
  95. {
  96. await enumerator.DisposeAsync().ConfigureAwait(false);
  97. enumerator = null;
  98. set = null;
  99. }
  100. await base.DisposeAsync().ConfigureAwait(false);
  101. }
  102. protected override async Task<bool> MoveNextCore()
  103. {
  104. switch (state)
  105. {
  106. case AsyncIteratorState.Allocated:
  107. enumerator = source.GetAsyncEnumerator();
  108. if (!await enumerator.MoveNextAsync().ConfigureAwait(false))
  109. {
  110. await DisposeAsync().ConfigureAwait(false);
  111. return false;
  112. }
  113. var element = enumerator.Current;
  114. set = new Set<TSource>(comparer);
  115. set.Add(element);
  116. current = element;
  117. state = AsyncIteratorState.Iterating;
  118. return true;
  119. case AsyncIteratorState.Iterating:
  120. while (await enumerator.MoveNextAsync().ConfigureAwait(false))
  121. {
  122. element = enumerator.Current;
  123. if (set.Add(element))
  124. {
  125. current = element;
  126. return true;
  127. }
  128. }
  129. break;
  130. }
  131. await DisposeAsync().ConfigureAwait(false);
  132. return false;
  133. }
  134. private async Task<Set<TSource>> FillSetAsync(CancellationToken cancellationToken)
  135. {
  136. var s = new Set<TSource>(comparer);
  137. var enu = source.GetAsyncEnumerator();
  138. try
  139. {
  140. while (await enu.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  141. {
  142. s.Add(enu.Current);
  143. }
  144. }
  145. finally
  146. {
  147. await enu.DisposeAsync().ConfigureAwait(false);
  148. }
  149. return s;
  150. }
  151. }
  152. private sealed class DistinctAsyncIterator<TSource, TKey> : AsyncIterator<TSource>, IIListProvider<TSource>
  153. {
  154. private readonly IEqualityComparer<TKey> comparer;
  155. private readonly Func<TSource, TKey> keySelector;
  156. private readonly IAsyncEnumerable<TSource> source;
  157. private IAsyncEnumerator<TSource> enumerator;
  158. private Set<TKey> set;
  159. public DistinctAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  160. {
  161. Debug.Assert(source != null);
  162. Debug.Assert(keySelector != null);
  163. Debug.Assert(comparer != null);
  164. this.source = source;
  165. this.keySelector = keySelector;
  166. this.comparer = comparer;
  167. }
  168. public async Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  169. {
  170. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  171. return s.ToArray();
  172. }
  173. public async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  174. {
  175. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  176. return s;
  177. }
  178. public async Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  179. {
  180. if (onlyIfCheap)
  181. {
  182. return -1;
  183. }
  184. var count = 0;
  185. var s = new Set<TKey>(comparer);
  186. var enu = source.GetAsyncEnumerator();
  187. try
  188. {
  189. while (await enu.MoveNextAsync().ConfigureAwait(false))
  190. {
  191. var item = enu.Current;
  192. if (s.Add(keySelector(item)))
  193. {
  194. count++;
  195. }
  196. }
  197. }
  198. finally
  199. {
  200. await enu.DisposeAsync().ConfigureAwait(false);
  201. }
  202. return count;
  203. }
  204. public override AsyncIterator<TSource> Clone()
  205. {
  206. return new DistinctAsyncIterator<TSource, TKey>(source, keySelector, comparer);
  207. }
  208. public override async Task DisposeAsync()
  209. {
  210. if (enumerator != null)
  211. {
  212. await enumerator.DisposeAsync().ConfigureAwait(false);
  213. enumerator = null;
  214. set = null;
  215. }
  216. await base.DisposeAsync().ConfigureAwait(false);
  217. }
  218. protected override async Task<bool> MoveNextCore()
  219. {
  220. switch (state)
  221. {
  222. case AsyncIteratorState.Allocated:
  223. enumerator = source.GetAsyncEnumerator();
  224. if (!await enumerator.MoveNextAsync().ConfigureAwait(false))
  225. {
  226. await DisposeAsync().ConfigureAwait(false);
  227. return false;
  228. }
  229. var element = enumerator.Current;
  230. set = new Set<TKey>(comparer);
  231. set.Add(keySelector(element));
  232. current = element;
  233. state = AsyncIteratorState.Iterating;
  234. return true;
  235. case AsyncIteratorState.Iterating:
  236. while (await enumerator.MoveNextAsync().ConfigureAwait(false))
  237. {
  238. element = enumerator.Current;
  239. if (set.Add(keySelector(element)))
  240. {
  241. current = element;
  242. return true;
  243. }
  244. }
  245. break;
  246. }
  247. await DisposeAsync().ConfigureAwait(false);
  248. return false;
  249. }
  250. private async Task<List<TSource>> FillSetAsync(CancellationToken cancellationToken)
  251. {
  252. var s = new Set<TKey>(comparer);
  253. var r = new List<TSource>();
  254. var enu = source.GetAsyncEnumerator();
  255. try
  256. {
  257. while (await enu.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  258. {
  259. var item = enu.Current;
  260. if (s.Add(keySelector(item)))
  261. {
  262. r.Add(item);
  263. }
  264. }
  265. }
  266. finally
  267. {
  268. await enu.DisposeAsync().ConfigureAwait(false);
  269. }
  270. return r;
  271. }
  272. }
  273. private sealed class DistinctAsyncIteratorWithTask<TSource, TKey> : AsyncIterator<TSource>, IIListProvider<TSource>
  274. {
  275. private readonly IEqualityComparer<TKey> comparer;
  276. private readonly Func<TSource, Task<TKey>> keySelector;
  277. private readonly IAsyncEnumerable<TSource> source;
  278. private IAsyncEnumerator<TSource> enumerator;
  279. private Set<TKey> set;
  280. public DistinctAsyncIteratorWithTask(IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  281. {
  282. Debug.Assert(source != null);
  283. Debug.Assert(keySelector != null);
  284. Debug.Assert(comparer != null);
  285. this.source = source;
  286. this.keySelector = keySelector;
  287. this.comparer = comparer;
  288. }
  289. public async Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  290. {
  291. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  292. return s.ToArray();
  293. }
  294. public async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  295. {
  296. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  297. return s;
  298. }
  299. public async Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  300. {
  301. if (onlyIfCheap)
  302. {
  303. return -1;
  304. }
  305. var count = 0;
  306. var s = new Set<TKey>(comparer);
  307. var enu = source.GetAsyncEnumerator();
  308. try
  309. {
  310. while (await enu.MoveNextAsync().ConfigureAwait(false))
  311. {
  312. var item = enu.Current;
  313. if (s.Add(await keySelector(item).ConfigureAwait(false)))
  314. {
  315. count++;
  316. }
  317. }
  318. }
  319. finally
  320. {
  321. await enu.DisposeAsync().ConfigureAwait(false);
  322. }
  323. return count;
  324. }
  325. public override AsyncIterator<TSource> Clone()
  326. {
  327. return new DistinctAsyncIteratorWithTask<TSource, TKey>(source, keySelector, comparer);
  328. }
  329. public override async Task DisposeAsync()
  330. {
  331. if (enumerator != null)
  332. {
  333. await enumerator.DisposeAsync().ConfigureAwait(false);
  334. enumerator = null;
  335. set = null;
  336. }
  337. await base.DisposeAsync().ConfigureAwait(false);
  338. }
  339. protected override async Task<bool> MoveNextCore()
  340. {
  341. switch (state)
  342. {
  343. case AsyncIteratorState.Allocated:
  344. enumerator = source.GetAsyncEnumerator();
  345. if (!await enumerator.MoveNextAsync().ConfigureAwait(false))
  346. {
  347. await DisposeAsync().ConfigureAwait(false);
  348. return false;
  349. }
  350. var element = enumerator.Current;
  351. set = new Set<TKey>(comparer);
  352. set.Add(await keySelector(element).ConfigureAwait(false));
  353. current = element;
  354. state = AsyncIteratorState.Iterating;
  355. return true;
  356. case AsyncIteratorState.Iterating:
  357. while (await enumerator.MoveNextAsync().ConfigureAwait(false))
  358. {
  359. element = enumerator.Current;
  360. if (set.Add(await keySelector(element).ConfigureAwait(false)))
  361. {
  362. current = element;
  363. return true;
  364. }
  365. }
  366. break;
  367. }
  368. await DisposeAsync().ConfigureAwait(false);
  369. return false;
  370. }
  371. private async Task<List<TSource>> FillSetAsync(CancellationToken cancellationToken)
  372. {
  373. var s = new Set<TKey>(comparer);
  374. var r = new List<TSource>();
  375. var enu = source.GetAsyncEnumerator();
  376. try
  377. {
  378. while (await enu.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  379. {
  380. var item = enu.Current;
  381. if (s.Add(await keySelector(item).ConfigureAwait(false)))
  382. {
  383. r.Add(item);
  384. }
  385. }
  386. }
  387. finally
  388. {
  389. await enu.DisposeAsync().ConfigureAwait(false);
  390. }
  391. return r;
  392. }
  393. }
  394. }
  395. }