Distinct.cs 19 KB

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