Distinct.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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 FillSetAsync(cancellationToken).ConfigureAwait(false);
  99. return s.ToArray();
  100. }
  101. public async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  102. {
  103. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  104. return s;
  105. }
  106. public async Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  107. {
  108. if (onlyIfCheap)
  109. {
  110. return -1;
  111. }
  112. var count = 0;
  113. var s = new Set<TKey>(comparer);
  114. var enu = source.GetAsyncEnumerator();
  115. try
  116. {
  117. while (await enu.MoveNextAsync().ConfigureAwait(false))
  118. {
  119. var item = enu.Current;
  120. if (s.Add(keySelector(item)))
  121. {
  122. count++;
  123. }
  124. }
  125. }
  126. finally
  127. {
  128. await enu.DisposeAsync().ConfigureAwait(false);
  129. }
  130. return count;
  131. }
  132. public override AsyncIterator<TSource> Clone()
  133. {
  134. return new DistinctAsyncIterator<TSource, TKey>(source, keySelector, comparer);
  135. }
  136. public override async Task DisposeAsync()
  137. {
  138. if (enumerator != null)
  139. {
  140. await enumerator.DisposeAsync().ConfigureAwait(false);
  141. enumerator = null;
  142. set = null;
  143. }
  144. await base.DisposeAsync().ConfigureAwait(false);
  145. }
  146. protected override async Task<bool> MoveNextCore()
  147. {
  148. switch (state)
  149. {
  150. case AsyncIteratorState.Allocated:
  151. enumerator = source.GetAsyncEnumerator();
  152. if (!await enumerator.MoveNextAsync().ConfigureAwait(false))
  153. {
  154. await DisposeAsync().ConfigureAwait(false);
  155. return false;
  156. }
  157. var element = enumerator.Current;
  158. set = new Set<TKey>(comparer);
  159. set.Add(keySelector(element));
  160. current = element;
  161. state = AsyncIteratorState.Iterating;
  162. return true;
  163. case AsyncIteratorState.Iterating:
  164. while (await enumerator.MoveNextAsync().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. await DisposeAsync().ConfigureAwait(false);
  176. return false;
  177. }
  178. private async Task<List<TSource>> FillSetAsync(CancellationToken cancellationToken)
  179. {
  180. var s = new Set<TKey>(comparer);
  181. var r = new List<TSource>();
  182. var enu = source.GetAsyncEnumerator();
  183. try
  184. {
  185. while (await enu.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  186. {
  187. var item = enu.Current;
  188. if (s.Add(keySelector(item)))
  189. {
  190. r.Add(item);
  191. }
  192. }
  193. }
  194. finally
  195. {
  196. await enu.DisposeAsync().ConfigureAwait(false);
  197. }
  198. return r;
  199. }
  200. }
  201. private sealed class DistinctAsyncIterator<TSource> : AsyncIterator<TSource>, IIListProvider<TSource>
  202. {
  203. private readonly IEqualityComparer<TSource> comparer;
  204. private readonly IAsyncEnumerable<TSource> source;
  205. private IAsyncEnumerator<TSource> enumerator;
  206. private Set<TSource> set;
  207. public DistinctAsyncIterator(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  208. {
  209. Debug.Assert(source != null);
  210. this.source = source;
  211. this.comparer = comparer;
  212. }
  213. public async Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  214. {
  215. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  216. return s.ToArray();
  217. }
  218. public async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  219. {
  220. var s = await FillSetAsync(cancellationToken)
  221. .ConfigureAwait(false);
  222. return s.ToList();
  223. }
  224. public async Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  225. {
  226. return onlyIfCheap ? -1 : (await FillSetAsync(cancellationToken).ConfigureAwait(false)).Count;
  227. }
  228. public override AsyncIterator<TSource> Clone()
  229. {
  230. return new DistinctAsyncIterator<TSource>(source, comparer);
  231. }
  232. public override async Task DisposeAsync()
  233. {
  234. if (enumerator != null)
  235. {
  236. await enumerator.DisposeAsync().ConfigureAwait(false);
  237. enumerator = null;
  238. set = null;
  239. }
  240. await base.DisposeAsync().ConfigureAwait(false);
  241. }
  242. protected override async Task<bool> MoveNextCore()
  243. {
  244. switch (state)
  245. {
  246. case AsyncIteratorState.Allocated:
  247. enumerator = source.GetAsyncEnumerator();
  248. if (!await enumerator.MoveNextAsync().ConfigureAwait(false))
  249. {
  250. await DisposeAsync().ConfigureAwait(false);
  251. return false;
  252. }
  253. var element = enumerator.Current;
  254. set = new Set<TSource>(comparer);
  255. set.Add(element);
  256. current = element;
  257. state = AsyncIteratorState.Iterating;
  258. return true;
  259. case AsyncIteratorState.Iterating:
  260. while (await enumerator.MoveNextAsync().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. await DisposeAsync().ConfigureAwait(false);
  272. return false;
  273. }
  274. private async Task<Set<TSource>> FillSetAsync(CancellationToken cancellationToken)
  275. {
  276. var s = new Set<TSource>(comparer);
  277. var enu = source.GetAsyncEnumerator();
  278. try
  279. {
  280. while (await enu.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  281. {
  282. s.Add(enu.Current);
  283. }
  284. }
  285. finally
  286. {
  287. await enu.DisposeAsync().ConfigureAwait(false);
  288. }
  289. return s;
  290. }
  291. }
  292. private sealed class DistinctUntilChangedAsyncIterator<TSource> : AsyncIterator<TSource>
  293. {
  294. private readonly IEqualityComparer<TSource> comparer;
  295. private readonly IAsyncEnumerable<TSource> source;
  296. private TSource currentValue;
  297. private IAsyncEnumerator<TSource> enumerator;
  298. private bool hasCurrentValue;
  299. public DistinctUntilChangedAsyncIterator(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  300. {
  301. Debug.Assert(comparer != null);
  302. Debug.Assert(source != null);
  303. this.source = source;
  304. this.comparer = comparer;
  305. }
  306. public override AsyncIterator<TSource> Clone()
  307. {
  308. return new DistinctUntilChangedAsyncIterator<TSource>(source, comparer);
  309. }
  310. public override async Task DisposeAsync()
  311. {
  312. if (enumerator != null)
  313. {
  314. await enumerator.DisposeAsync().ConfigureAwait(false);
  315. enumerator = null;
  316. currentValue = default(TSource);
  317. }
  318. await base.DisposeAsync().ConfigureAwait(false);
  319. }
  320. protected override async Task<bool> MoveNextCore()
  321. {
  322. switch (state)
  323. {
  324. case AsyncIteratorState.Allocated:
  325. enumerator = source.GetAsyncEnumerator();
  326. state = AsyncIteratorState.Iterating;
  327. goto case AsyncIteratorState.Iterating;
  328. case AsyncIteratorState.Iterating:
  329. while (await enumerator.MoveNextAsync().ConfigureAwait(false))
  330. {
  331. var item = enumerator.Current;
  332. var comparerEquals = false;
  333. if (hasCurrentValue)
  334. {
  335. comparerEquals = comparer.Equals(currentValue, item);
  336. }
  337. if (!hasCurrentValue || !comparerEquals)
  338. {
  339. hasCurrentValue = true;
  340. currentValue = item;
  341. current = item;
  342. return true;
  343. }
  344. }
  345. break;
  346. }
  347. await DisposeAsync().ConfigureAwait(false);
  348. return false;
  349. }
  350. }
  351. private sealed class DistinctUntilChangedAsyncIterator<TSource, TKey> : AsyncIterator<TSource>
  352. {
  353. private readonly IEqualityComparer<TKey> comparer;
  354. private readonly Func<TSource, TKey> keySelector;
  355. private readonly IAsyncEnumerable<TSource> source;
  356. private TKey currentKeyValue;
  357. private IAsyncEnumerator<TSource> enumerator;
  358. private bool hasCurrentKey;
  359. public DistinctUntilChangedAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  360. {
  361. this.source = source;
  362. this.keySelector = keySelector;
  363. this.comparer = comparer;
  364. }
  365. public override AsyncIterator<TSource> Clone()
  366. {
  367. return new DistinctUntilChangedAsyncIterator<TSource, TKey>(source, keySelector, comparer);
  368. }
  369. public override async Task DisposeAsync()
  370. {
  371. if (enumerator != null)
  372. {
  373. await enumerator.DisposeAsync().ConfigureAwait(false);
  374. enumerator = null;
  375. currentKeyValue = default(TKey);
  376. }
  377. await base.DisposeAsync().ConfigureAwait(false);
  378. }
  379. protected override async Task<bool> MoveNextCore()
  380. {
  381. switch (state)
  382. {
  383. case AsyncIteratorState.Allocated:
  384. enumerator = source.GetAsyncEnumerator();
  385. state = AsyncIteratorState.Iterating;
  386. goto case AsyncIteratorState.Iterating;
  387. case AsyncIteratorState.Iterating:
  388. while (await enumerator.MoveNextAsync().ConfigureAwait(false))
  389. {
  390. var item = enumerator.Current;
  391. var key = keySelector(item);
  392. var comparerEquals = false;
  393. if (hasCurrentKey)
  394. {
  395. comparerEquals = comparer.Equals(currentKeyValue, key);
  396. }
  397. if (!hasCurrentKey || !comparerEquals)
  398. {
  399. hasCurrentKey = true;
  400. currentKeyValue = key;
  401. current = item;
  402. return true;
  403. }
  404. }
  405. break; // case
  406. }
  407. await DisposeAsync().ConfigureAwait(false);
  408. return false;
  409. }
  410. }
  411. }
  412. }