DistinctUntilChanged.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerableEx
  10. {
  11. /// <summary>
  12. /// Returns an async-enumerable sequence that contains only distinct contiguous elements.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  15. /// <param name="source">An async-enumerable sequence to retain distinct contiguous elements for.</param>
  16. /// <returns>An async-enumerable sequence only containing the distinct contiguous elements from the source sequence.</returns>
  17. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  18. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource>(this IAsyncEnumerable<TSource> source)
  19. {
  20. if (source == null)
  21. throw Error.ArgumentNull(nameof(source));
  22. return DistinctUntilChangedCore(source, comparer: null);
  23. }
  24. /// <summary>
  25. /// Returns an async-enumerable sequence that contains only distinct contiguous elements according to the comparer.
  26. /// </summary>
  27. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  28. /// <param name="source">An async-enumerable sequence to retain distinct contiguous elements for.</param>
  29. /// <param name="comparer">Equality comparer for source elements.</param>
  30. /// <returns>An async-enumerable sequence only containing the distinct contiguous elements from the source sequence.</returns>
  31. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="comparer"/> is null.</exception>
  32. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource>(this IAsyncEnumerable<TSource> source, IEqualityComparer<TSource>? comparer)
  33. {
  34. if (source == null)
  35. throw Error.ArgumentNull(nameof(source));
  36. return DistinctUntilChangedCore(source, comparer);
  37. }
  38. /// <summary>
  39. /// Returns an async-enumerable sequence that contains only distinct contiguous elements according to the keySelector.
  40. /// </summary>
  41. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  42. /// <typeparam name="TKey">The type of the discriminator key computed for each element in the source sequence.</typeparam>
  43. /// <param name="source">An async-enumerable sequence to retain distinct contiguous elements for, based on a computed key value.</param>
  44. /// <param name="keySelector">A function to compute the comparison key for each element.</param>
  45. /// <returns>An async-enumerable sequence only containing the distinct contiguous elements, based on a computed key value, from the source sequence.</returns>
  46. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  47. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  48. {
  49. if (source == null)
  50. throw Error.ArgumentNull(nameof(source));
  51. if (keySelector == null)
  52. throw Error.ArgumentNull(nameof(keySelector));
  53. return DistinctUntilChangedCore(source, keySelector, comparer: null);
  54. }
  55. /// <summary>
  56. /// Returns an async-enumerable sequence that contains only distinct contiguous elements according to the keySelector and the comparer.
  57. /// </summary>
  58. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  59. /// <typeparam name="TKey">The type of the discriminator key computed for each element in the source sequence.</typeparam>
  60. /// <param name="source">An async-enumerable sequence to retain distinct contiguous elements for, based on a computed key value.</param>
  61. /// <param name="keySelector">A function to compute the comparison key for each element.</param>
  62. /// <param name="comparer">Equality comparer for computed key values.</param>
  63. /// <returns>An async-enumerable sequence only containing the distinct contiguous elements, based on a computed key value, from the source sequence.</returns>
  64. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="comparer"/> is null.</exception>
  65. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
  66. {
  67. if (source == null)
  68. throw Error.ArgumentNull(nameof(source));
  69. if (keySelector == null)
  70. throw Error.ArgumentNull(nameof(keySelector));
  71. return DistinctUntilChangedCore(source, keySelector, comparer);
  72. }
  73. /// <summary>
  74. /// Returns an async-enumerable sequence that contains only distinct contiguous elements according to the asynchronous keySelector.
  75. /// </summary>
  76. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  77. /// <typeparam name="TKey">The type of the discriminator key computed for each element in the source sequence.</typeparam>
  78. /// <param name="source">An async-enumerable sequence to retain distinct contiguous elements for, based on a computed key value.</param>
  79. /// <param name="keySelector">A function to compute the comparison key for each element asynchronously.</param>
  80. /// <returns>An async-enumerable sequence only containing the distinct contiguous elements, based on a computed key value, from the source sequence.</returns>
  81. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  82. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector)
  83. {
  84. if (source == null)
  85. throw Error.ArgumentNull(nameof(source));
  86. if (keySelector == null)
  87. throw Error.ArgumentNull(nameof(keySelector));
  88. return DistinctUntilChangedCore<TSource, TKey>(source, keySelector, comparer: null);
  89. }
  90. #if !NO_DEEP_CANCELLATION
  91. /// <summary>
  92. /// Returns an async-enumerable sequence that contains only distinct contiguous elements according to the asynchronous and cancellable keySelector.
  93. /// </summary>
  94. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  95. /// <typeparam name="TKey">The type of the discriminator key computed for each element in the source sequence.</typeparam>
  96. /// <param name="source">An async-enumerable sequence to retain distinct contiguous elements for, based on a computed key value.</param>
  97. /// <param name="keySelector">A function to compute the comparison key for each element asynchronously while supporting cancellation.</param>
  98. /// <returns>An async-enumerable sequence only containing the distinct contiguous elements, based on a computed key value, from the source sequence.</returns>
  99. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  100. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector)
  101. {
  102. if (source == null)
  103. throw Error.ArgumentNull(nameof(source));
  104. if (keySelector == null)
  105. throw Error.ArgumentNull(nameof(keySelector));
  106. return DistinctUntilChangedCore<TSource, TKey>(source, keySelector, comparer: null);
  107. }
  108. #endif
  109. /// <summary>
  110. /// Returns an async-enumerable sequence that contains only distinct contiguous elements according to the asynchronous keySelector and the comparer.
  111. /// </summary>
  112. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  113. /// <typeparam name="TKey">The type of the discriminator key computed for each element in the source sequence.</typeparam>
  114. /// <param name="source">An async-enumerable sequence to retain distinct contiguous elements for, based on a computed key value.</param>
  115. /// <param name="keySelector">A function to compute the comparison key for each element asynchronously.</param>
  116. /// <param name="comparer">Equality comparer for computed key values.</param>
  117. /// <returns>An async-enumerable sequence only containing the distinct contiguous elements, based on a computed key value, from the source sequence.</returns>
  118. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="comparer"/> is null.</exception>
  119. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer)
  120. {
  121. if (source == null)
  122. throw Error.ArgumentNull(nameof(source));
  123. if (keySelector == null)
  124. throw Error.ArgumentNull(nameof(keySelector));
  125. return DistinctUntilChangedCore(source, keySelector, comparer);
  126. }
  127. #if !NO_DEEP_CANCELLATION
  128. /// <summary>
  129. /// Returns an async-enumerable sequence that contains only distinct contiguous elements according to the asynchronous and cancellable keySelector and the comparer.
  130. /// </summary>
  131. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  132. /// <typeparam name="TKey">The type of the discriminator key computed for each element in the source sequence.</typeparam>
  133. /// <param name="source">An async-enumerable sequence to retain distinct contiguous elements for, based on a computed key value.</param>
  134. /// <param name="keySelector">A function to compute the comparison key for each element asynchronously while supporting cancellation.</param>
  135. /// <param name="comparer">Equality comparer for computed key values.</param>
  136. /// <returns>An async-enumerable sequence only containing the distinct contiguous elements, based on a computed key value, from the source sequence.</returns>
  137. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> or <paramref name="comparer"/> is null.</exception>
  138. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  139. {
  140. if (source == null)
  141. throw Error.ArgumentNull(nameof(source));
  142. if (keySelector == null)
  143. throw Error.ArgumentNull(nameof(keySelector));
  144. return DistinctUntilChangedCore(source, keySelector, comparer);
  145. }
  146. #endif
  147. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource>(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource>? comparer)
  148. {
  149. comparer ??= EqualityComparer<TSource>.Default;
  150. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  151. return Core(source, comparer);
  152. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  153. #else
  154. return AsyncEnumerable.Create(Core);
  155. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  156. #endif
  157. {
  158. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  159. if (!await e.MoveNextAsync())
  160. {
  161. yield break;
  162. }
  163. var latest = e.Current;
  164. yield return latest;
  165. while (await e.MoveNextAsync())
  166. {
  167. var item = e.Current;
  168. // REVIEW: Need comparer!.Equals to satisfy nullable reference type warnings.
  169. if (!comparer!.Equals(latest, item))
  170. {
  171. latest = item;
  172. yield return latest;
  173. }
  174. }
  175. }
  176. }
  177. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
  178. {
  179. comparer ??= EqualityComparer<TKey>.Default;
  180. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  181. return Core(source, keySelector, comparer);
  182. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  183. #else
  184. return AsyncEnumerable.Create(Core);
  185. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  186. #endif
  187. {
  188. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  189. if (!await e.MoveNextAsync())
  190. {
  191. yield break;
  192. }
  193. var item = e.Current;
  194. var latestKey = keySelector(item);
  195. yield return item;
  196. while (await e.MoveNextAsync())
  197. {
  198. item = e.Current;
  199. var currentKey = keySelector(item);
  200. // REVIEW: Need comparer!.Equals to satisfy nullable reference type warnings.
  201. if (!comparer!.Equals(latestKey, currentKey))
  202. {
  203. latestKey = currentKey;
  204. yield return item;
  205. }
  206. }
  207. }
  208. }
  209. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer)
  210. {
  211. comparer ??= EqualityComparer<TKey>.Default;
  212. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  213. return Core(source, keySelector, comparer);
  214. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TSource> comparer, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  215. #else
  216. return AsyncEnumerable.Create(Core);
  217. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  218. #endif
  219. {
  220. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  221. if (!await e.MoveNextAsync())
  222. {
  223. yield break;
  224. }
  225. var item = e.Current;
  226. var latestKey = await keySelector(item).ConfigureAwait(false);
  227. yield return item;
  228. while (await e.MoveNextAsync())
  229. {
  230. item = e.Current;
  231. var currentKey = await keySelector(item).ConfigureAwait(false);
  232. // REVIEW: Need comparer!.Equals to satisfy nullable reference type warnings.
  233. if (!comparer!.Equals(latestKey, currentKey))
  234. {
  235. latestKey = currentKey;
  236. yield return item;
  237. }
  238. }
  239. }
  240. }
  241. #if !NO_DEEP_CANCELLATION
  242. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer)
  243. {
  244. comparer ??= EqualityComparer<TKey>.Default;
  245. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  246. return Core(source, keySelector, comparer);
  247. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TSource> comparer, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  248. #else
  249. return AsyncEnumerable.Create(Core);
  250. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  251. #endif
  252. {
  253. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  254. if (!await e.MoveNextAsync())
  255. {
  256. yield break;
  257. }
  258. var item = e.Current;
  259. var latestKey = await keySelector(item, cancellationToken).ConfigureAwait(false);
  260. yield return item;
  261. while (await e.MoveNextAsync())
  262. {
  263. item = e.Current;
  264. var currentKey = await keySelector(item, cancellationToken).ConfigureAwait(false);
  265. // REVIEW: Need comparer!.Equals to satisfy nullable reference type warnings.
  266. if (!comparer!.Equals(latestKey, currentKey))
  267. {
  268. latestKey = currentKey;
  269. yield return item;
  270. }
  271. }
  272. }
  273. }
  274. #endif
  275. }
  276. }