1
0

Distinct.cs 13 KB

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