DistinctUntilChanged.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerableEx
  10. {
  11. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource>(this IAsyncEnumerable<TSource> source)
  12. {
  13. if (source == null)
  14. throw Error.ArgumentNull(nameof(source));
  15. return DistinctUntilChangedCore(source, comparer: null);
  16. }
  17. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource>(this IAsyncEnumerable<TSource> source, IEqualityComparer<TSource>? comparer)
  18. {
  19. if (source == null)
  20. throw Error.ArgumentNull(nameof(source));
  21. return DistinctUntilChangedCore(source, comparer);
  22. }
  23. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  24. {
  25. if (source == null)
  26. throw Error.ArgumentNull(nameof(source));
  27. if (keySelector == null)
  28. throw Error.ArgumentNull(nameof(keySelector));
  29. return DistinctUntilChangedCore(source, keySelector, comparer: null);
  30. }
  31. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
  32. {
  33. if (source == null)
  34. throw Error.ArgumentNull(nameof(source));
  35. if (keySelector == null)
  36. throw Error.ArgumentNull(nameof(keySelector));
  37. return DistinctUntilChangedCore(source, keySelector, comparer);
  38. }
  39. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector)
  40. {
  41. if (source == null)
  42. throw Error.ArgumentNull(nameof(source));
  43. if (keySelector == null)
  44. throw Error.ArgumentNull(nameof(keySelector));
  45. return DistinctUntilChangedCore<TSource, TKey>(source, keySelector, comparer: null);
  46. }
  47. #if !NO_DEEP_CANCELLATION
  48. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector)
  49. {
  50. if (source == null)
  51. throw Error.ArgumentNull(nameof(source));
  52. if (keySelector == null)
  53. throw Error.ArgumentNull(nameof(keySelector));
  54. return DistinctUntilChangedCore<TSource, TKey>(source, keySelector, comparer: null);
  55. }
  56. #endif
  57. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer)
  58. {
  59. if (source == null)
  60. throw Error.ArgumentNull(nameof(source));
  61. if (keySelector == null)
  62. throw Error.ArgumentNull(nameof(keySelector));
  63. return DistinctUntilChangedCore(source, keySelector, comparer);
  64. }
  65. #if !NO_DEEP_CANCELLATION
  66. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  67. {
  68. if (source == null)
  69. throw Error.ArgumentNull(nameof(source));
  70. if (keySelector == null)
  71. throw Error.ArgumentNull(nameof(keySelector));
  72. return DistinctUntilChangedCore(source, keySelector, comparer);
  73. }
  74. #endif
  75. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource>(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource>? comparer)
  76. {
  77. comparer ??= EqualityComparer<TSource>.Default;
  78. return AsyncEnumerable.Create(Core);
  79. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  80. {
  81. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  82. {
  83. if (!await e.MoveNextAsync())
  84. {
  85. yield break;
  86. }
  87. var latest = e.Current;
  88. yield return latest;
  89. while (await e.MoveNextAsync())
  90. {
  91. var item = e.Current;
  92. // REVIEW: Need comparer!.Equals to satisfy nullable reference type warnings.
  93. if (!comparer!.Equals(latest, item))
  94. {
  95. latest = item;
  96. yield return latest;
  97. }
  98. }
  99. }
  100. }
  101. }
  102. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
  103. {
  104. comparer ??= EqualityComparer<TKey>.Default;
  105. return AsyncEnumerable.Create(Core);
  106. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  107. {
  108. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  109. {
  110. if (!await e.MoveNextAsync())
  111. {
  112. yield break;
  113. }
  114. var item = e.Current;
  115. var latestKey = keySelector(item);
  116. yield return item;
  117. while (await e.MoveNextAsync())
  118. {
  119. item = e.Current;
  120. var currentKey = keySelector(item);
  121. // REVIEW: Need comparer!.Equals to satisfy nullable reference type warnings.
  122. if (!comparer!.Equals(latestKey, currentKey))
  123. {
  124. latestKey = currentKey;
  125. yield return item;
  126. }
  127. }
  128. }
  129. }
  130. }
  131. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer)
  132. {
  133. comparer ??= EqualityComparer<TKey>.Default;
  134. return AsyncEnumerable.Create(Core);
  135. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  136. {
  137. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  138. {
  139. if (!await e.MoveNextAsync())
  140. {
  141. yield break;
  142. }
  143. var item = e.Current;
  144. var latestKey = await keySelector(item).ConfigureAwait(false);
  145. yield return item;
  146. while (await e.MoveNextAsync())
  147. {
  148. item = e.Current;
  149. var currentKey = await keySelector(item).ConfigureAwait(false);
  150. // REVIEW: Need comparer!.Equals to satisfy nullable reference type warnings.
  151. if (!comparer!.Equals(latestKey, currentKey))
  152. {
  153. latestKey = currentKey;
  154. yield return item;
  155. }
  156. }
  157. }
  158. }
  159. }
  160. #if !NO_DEEP_CANCELLATION
  161. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer)
  162. {
  163. comparer ??= EqualityComparer<TKey>.Default;
  164. return AsyncEnumerable.Create(Core);
  165. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  166. {
  167. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  168. {
  169. if (!await e.MoveNextAsync())
  170. {
  171. yield break;
  172. }
  173. var item = e.Current;
  174. var latestKey = await keySelector(item, cancellationToken).ConfigureAwait(false);
  175. yield return item;
  176. while (await e.MoveNextAsync())
  177. {
  178. item = e.Current;
  179. var currentKey = await keySelector(item, cancellationToken).ConfigureAwait(false);
  180. // REVIEW: Need comparer!.Equals to satisfy nullable reference type warnings.
  181. if (!comparer!.Equals(latestKey, currentKey))
  182. {
  183. latestKey = currentKey;
  184. yield return item;
  185. }
  186. }
  187. }
  188. }
  189. }
  190. #endif
  191. }
  192. }