Distinct.cs 18 KB

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