Distinct.cs 20 KB

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