Distinct.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. throw new ArgumentNullException(nameof(source));
  16. if (keySelector == null)
  17. throw new ArgumentNullException(nameof(keySelector));
  18. if (comparer == null)
  19. throw new ArgumentNullException(nameof(comparer));
  20. return new DistinctAsyncIterator<TSource, TKey>(source, keySelector, comparer);
  21. }
  22. public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  23. {
  24. if (source == null)
  25. throw new ArgumentNullException(nameof(source));
  26. if (keySelector == null)
  27. throw new ArgumentNullException(nameof(keySelector));
  28. return source.Distinct(keySelector, EqualityComparer<TKey>.Default);
  29. }
  30. public static IAsyncEnumerable<TSource> Distinct<TSource>(this IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  31. {
  32. if (source == null)
  33. throw new ArgumentNullException(nameof(source));
  34. if (comparer == null)
  35. throw new ArgumentNullException(nameof(comparer));
  36. return new DistinctAsyncIterator<TSource>(source, comparer);
  37. }
  38. public static IAsyncEnumerable<TSource> Distinct<TSource>(this IAsyncEnumerable<TSource> source)
  39. {
  40. if (source == null)
  41. throw new ArgumentNullException(nameof(source));
  42. return source.Distinct(EqualityComparer<TSource>.Default);
  43. }
  44. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource>(this IAsyncEnumerable<TSource> source)
  45. {
  46. if (source == null)
  47. throw new ArgumentNullException(nameof(source));
  48. return source.DistinctUntilChanged(EqualityComparer<TSource>.Default);
  49. }
  50. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource>(this IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  51. {
  52. if (source == null)
  53. throw new ArgumentNullException(nameof(source));
  54. if (comparer == null)
  55. throw new ArgumentNullException(nameof(comparer));
  56. return new DistinctUntilChangedAsyncIterator<TSource>(source, comparer);
  57. }
  58. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  59. {
  60. if (source == null)
  61. throw new ArgumentNullException(nameof(source));
  62. if (keySelector == null)
  63. throw new ArgumentNullException(nameof(keySelector));
  64. return source.DistinctUntilChanged_(keySelector, EqualityComparer<TKey>.Default);
  65. }
  66. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  67. {
  68. if (source == null)
  69. throw new ArgumentNullException(nameof(source));
  70. if (keySelector == null)
  71. throw new ArgumentNullException(nameof(keySelector));
  72. if (comparer == null)
  73. throw new ArgumentNullException(nameof(comparer));
  74. return source.DistinctUntilChanged_(keySelector, comparer);
  75. }
  76. private static IAsyncEnumerable<TSource> DistinctUntilChanged_<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  77. {
  78. return new DistinctUntilChangedAsyncIterator<TSource, TKey>(source, keySelector, comparer);
  79. }
  80. private sealed class DistinctAsyncIterator<TSource, TKey> : AsyncIterator<TSource>, IIListProvider<TSource>
  81. {
  82. private readonly IEqualityComparer<TKey> comparer;
  83. private readonly Func<TSource, TKey> keySelector;
  84. private readonly IAsyncEnumerable<TSource> source;
  85. private IAsyncEnumerator<TSource> enumerator;
  86. private Set<TKey> set;
  87. public DistinctAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  88. {
  89. Debug.Assert(source != null);
  90. Debug.Assert(keySelector != null);
  91. Debug.Assert(comparer != 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. Debug.Assert(comparer != null);
  298. Debug.Assert(source != null);
  299. this.source = source;
  300. this.comparer = comparer;
  301. }
  302. public override AsyncIterator<TSource> Clone()
  303. {
  304. return new DistinctUntilChangedAsyncIterator<TSource>(source, comparer);
  305. }
  306. public override void Dispose()
  307. {
  308. if (enumerator != null)
  309. {
  310. enumerator.Dispose();
  311. enumerator = null;
  312. currentValue = default(TSource);
  313. }
  314. base.Dispose();
  315. }
  316. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  317. {
  318. switch (state)
  319. {
  320. case AsyncIteratorState.Allocated:
  321. enumerator = source.GetEnumerator();
  322. state = AsyncIteratorState.Iterating;
  323. goto case AsyncIteratorState.Iterating;
  324. case AsyncIteratorState.Iterating:
  325. while (await enumerator.MoveNext(cancellationToken)
  326. .ConfigureAwait(false))
  327. {
  328. var item = enumerator.Current;
  329. var comparerEquals = false;
  330. if (hasCurrentValue)
  331. {
  332. comparerEquals = comparer.Equals(currentValue, item);
  333. }
  334. if (!hasCurrentValue || !comparerEquals)
  335. {
  336. hasCurrentValue = true;
  337. currentValue = item;
  338. current = item;
  339. return true;
  340. }
  341. }
  342. break;
  343. }
  344. Dispose();
  345. return false;
  346. }
  347. }
  348. private sealed class DistinctUntilChangedAsyncIterator<TSource, TKey> : AsyncIterator<TSource>
  349. {
  350. private readonly IEqualityComparer<TKey> comparer;
  351. private readonly Func<TSource, TKey> keySelector;
  352. private readonly IAsyncEnumerable<TSource> source;
  353. private TKey currentKeyValue;
  354. private IAsyncEnumerator<TSource> enumerator;
  355. private bool hasCurrentKey;
  356. public DistinctUntilChangedAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  357. {
  358. this.source = source;
  359. this.keySelector = keySelector;
  360. this.comparer = comparer;
  361. }
  362. public override AsyncIterator<TSource> Clone()
  363. {
  364. return new DistinctUntilChangedAsyncIterator<TSource, TKey>(source, keySelector, comparer);
  365. }
  366. public override void Dispose()
  367. {
  368. if (enumerator != null)
  369. {
  370. enumerator.Dispose();
  371. enumerator = null;
  372. currentKeyValue = default(TKey);
  373. }
  374. base.Dispose();
  375. }
  376. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  377. {
  378. switch (state)
  379. {
  380. case AsyncIteratorState.Allocated:
  381. enumerator = source.GetEnumerator();
  382. state = AsyncIteratorState.Iterating;
  383. goto case AsyncIteratorState.Iterating;
  384. case AsyncIteratorState.Iterating:
  385. while (await enumerator.MoveNext(cancellationToken)
  386. .ConfigureAwait(false))
  387. {
  388. var item = enumerator.Current;
  389. var key = keySelector(item);
  390. var comparerEquals = false;
  391. if (hasCurrentKey)
  392. {
  393. comparerEquals = comparer.Equals(currentKeyValue, key);
  394. }
  395. if (!hasCurrentKey || !comparerEquals)
  396. {
  397. hasCurrentKey = true;
  398. currentKeyValue = key;
  399. current = item;
  400. return true;
  401. }
  402. }
  403. break; // case
  404. }
  405. Dispose();
  406. return false;
  407. }
  408. }
  409. }
  410. }