Distinct.cs 18 KB

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