Distinct.cs 19 KB

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