Distinct.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 AsyncEnumerableEx
  11. {
  12. public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  13. {
  14. if (source == null)
  15. throw Error.ArgumentNull(nameof(source));
  16. if (keySelector == null)
  17. throw Error.ArgumentNull(nameof(keySelector));
  18. return DistinctCore(source, keySelector, comparer: null);
  19. }
  20. public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  21. {
  22. if (source == null)
  23. throw Error.ArgumentNull(nameof(source));
  24. if (keySelector == null)
  25. throw Error.ArgumentNull(nameof(keySelector));
  26. return DistinctCore(source, keySelector, comparer);
  27. }
  28. public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector)
  29. {
  30. if (source == null)
  31. throw Error.ArgumentNull(nameof(source));
  32. if (keySelector == null)
  33. throw Error.ArgumentNull(nameof(keySelector));
  34. return DistinctCore<TSource, TKey>(source, keySelector, comparer: null);
  35. }
  36. #if !NO_DEEP_CANCELLATION // TODO
  37. #endif
  38. public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  39. {
  40. if (source == null)
  41. throw Error.ArgumentNull(nameof(source));
  42. if (keySelector == null)
  43. throw Error.ArgumentNull(nameof(keySelector));
  44. return DistinctCore(source, keySelector, comparer);
  45. }
  46. #if !NO_DEEP_CANCELLATION // TODO
  47. #endif
  48. private static IAsyncEnumerable<TSource> DistinctCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  49. {
  50. return new DistinctAsyncIterator<TSource, TKey>(source, keySelector, comparer);
  51. }
  52. private static IAsyncEnumerable<TSource> DistinctCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  53. {
  54. return new DistinctAsyncIteratorWithTask<TSource, TKey>(source, keySelector, comparer);
  55. }
  56. private sealed class DistinctAsyncIterator<TSource, TKey> : AsyncIterator<TSource>, IAsyncIListProvider<TSource>
  57. {
  58. private readonly IEqualityComparer<TKey> _comparer;
  59. private readonly Func<TSource, TKey> _keySelector;
  60. private readonly IAsyncEnumerable<TSource> _source;
  61. private IAsyncEnumerator<TSource> _enumerator;
  62. private Set<TKey> _set;
  63. public DistinctAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  64. {
  65. Debug.Assert(source != null);
  66. Debug.Assert(keySelector != null);
  67. _source = source;
  68. _keySelector = keySelector;
  69. _comparer = comparer;
  70. }
  71. public async ValueTask<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  72. {
  73. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  74. return s.ToArray();
  75. }
  76. public async ValueTask<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  77. {
  78. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  79. return s;
  80. }
  81. public async ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  82. {
  83. if (onlyIfCheap)
  84. {
  85. return -1;
  86. }
  87. var count = 0;
  88. var s = new Set<TKey>(_comparer);
  89. var enu = _source.GetAsyncEnumerator(cancellationToken);
  90. try
  91. {
  92. while (await enu.MoveNextAsync().ConfigureAwait(false))
  93. {
  94. var item = enu.Current;
  95. if (s.Add(_keySelector(item)))
  96. {
  97. count++;
  98. }
  99. }
  100. }
  101. finally
  102. {
  103. await enu.DisposeAsync().ConfigureAwait(false);
  104. }
  105. return count;
  106. }
  107. public override AsyncIteratorBase<TSource> Clone()
  108. {
  109. return new DistinctAsyncIterator<TSource, TKey>(_source, _keySelector, _comparer);
  110. }
  111. public override async ValueTask DisposeAsync()
  112. {
  113. if (_enumerator != null)
  114. {
  115. await _enumerator.DisposeAsync().ConfigureAwait(false);
  116. _enumerator = null;
  117. _set = null;
  118. }
  119. await base.DisposeAsync().ConfigureAwait(false);
  120. }
  121. protected override async ValueTask<bool> MoveNextCore()
  122. {
  123. switch (_state)
  124. {
  125. case AsyncIteratorState.Allocated:
  126. _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
  127. if (!await _enumerator.MoveNextAsync().ConfigureAwait(false))
  128. {
  129. await DisposeAsync().ConfigureAwait(false);
  130. return false;
  131. }
  132. var element = _enumerator.Current;
  133. _set = new Set<TKey>(_comparer);
  134. _set.Add(_keySelector(element));
  135. _current = element;
  136. _state = AsyncIteratorState.Iterating;
  137. return true;
  138. case AsyncIteratorState.Iterating:
  139. while (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  140. {
  141. element = _enumerator.Current;
  142. if (_set.Add(_keySelector(element)))
  143. {
  144. _current = element;
  145. return true;
  146. }
  147. }
  148. break;
  149. }
  150. await DisposeAsync().ConfigureAwait(false);
  151. return false;
  152. }
  153. private async Task<List<TSource>> FillSetAsync(CancellationToken cancellationToken)
  154. {
  155. var s = new Set<TKey>(_comparer);
  156. var r = new List<TSource>();
  157. var enu = _source.GetAsyncEnumerator(cancellationToken);
  158. try
  159. {
  160. while (await enu.MoveNextAsync().ConfigureAwait(false))
  161. {
  162. var item = enu.Current;
  163. if (s.Add(_keySelector(item)))
  164. {
  165. r.Add(item);
  166. }
  167. }
  168. }
  169. finally
  170. {
  171. await enu.DisposeAsync().ConfigureAwait(false);
  172. }
  173. return r;
  174. }
  175. }
  176. private sealed class DistinctAsyncIteratorWithTask<TSource, TKey> : AsyncIterator<TSource>, IAsyncIListProvider<TSource>
  177. {
  178. private readonly IEqualityComparer<TKey> _comparer;
  179. private readonly Func<TSource, ValueTask<TKey>> _keySelector;
  180. private readonly IAsyncEnumerable<TSource> _source;
  181. private IAsyncEnumerator<TSource> _enumerator;
  182. private Set<TKey> _set;
  183. public DistinctAsyncIteratorWithTask(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  184. {
  185. Debug.Assert(source != null);
  186. Debug.Assert(keySelector != null);
  187. _source = source;
  188. _keySelector = keySelector;
  189. _comparer = comparer;
  190. }
  191. public async ValueTask<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  192. {
  193. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  194. return s.ToArray();
  195. }
  196. public async ValueTask<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  197. {
  198. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  199. return s;
  200. }
  201. public async ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  202. {
  203. if (onlyIfCheap)
  204. {
  205. return -1;
  206. }
  207. var count = 0;
  208. var s = new Set<TKey>(_comparer);
  209. var enu = _source.GetAsyncEnumerator(cancellationToken);
  210. try
  211. {
  212. while (await enu.MoveNextAsync().ConfigureAwait(false))
  213. {
  214. var item = enu.Current;
  215. if (s.Add(await _keySelector(item).ConfigureAwait(false)))
  216. {
  217. count++;
  218. }
  219. }
  220. }
  221. finally
  222. {
  223. await enu.DisposeAsync().ConfigureAwait(false);
  224. }
  225. return count;
  226. }
  227. public override AsyncIteratorBase<TSource> Clone()
  228. {
  229. return new DistinctAsyncIteratorWithTask<TSource, TKey>(_source, _keySelector, _comparer);
  230. }
  231. public override async ValueTask DisposeAsync()
  232. {
  233. if (_enumerator != null)
  234. {
  235. await _enumerator.DisposeAsync().ConfigureAwait(false);
  236. _enumerator = null;
  237. _set = null;
  238. }
  239. await base.DisposeAsync().ConfigureAwait(false);
  240. }
  241. protected override async ValueTask<bool> MoveNextCore()
  242. {
  243. switch (_state)
  244. {
  245. case AsyncIteratorState.Allocated:
  246. _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
  247. if (!await _enumerator.MoveNextAsync().ConfigureAwait(false))
  248. {
  249. await DisposeAsync().ConfigureAwait(false);
  250. return false;
  251. }
  252. var element = _enumerator.Current;
  253. _set = new Set<TKey>(_comparer);
  254. _set.Add(await _keySelector(element).ConfigureAwait(false));
  255. _current = element;
  256. _state = AsyncIteratorState.Iterating;
  257. return true;
  258. case AsyncIteratorState.Iterating:
  259. while (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  260. {
  261. element = _enumerator.Current;
  262. if (_set.Add(await _keySelector(element).ConfigureAwait(false)))
  263. {
  264. _current = element;
  265. return true;
  266. }
  267. }
  268. break;
  269. }
  270. await DisposeAsync().ConfigureAwait(false);
  271. return false;
  272. }
  273. private async ValueTask<List<TSource>> FillSetAsync(CancellationToken cancellationToken)
  274. {
  275. var s = new Set<TKey>(_comparer);
  276. var r = new List<TSource>();
  277. var enu = _source.GetAsyncEnumerator(cancellationToken);
  278. try
  279. {
  280. while (await enu.MoveNextAsync().ConfigureAwait(false))
  281. {
  282. var item = enu.Current;
  283. if (s.Add(await _keySelector(item).ConfigureAwait(false)))
  284. {
  285. r.Add(item);
  286. }
  287. }
  288. }
  289. finally
  290. {
  291. await enu.DisposeAsync().ConfigureAwait(false);
  292. }
  293. return r;
  294. }
  295. }
  296. }
  297. }