Distinct.cs 19 KB

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