1
0

DistinctUntilChanged.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. if (!await e.MoveNextAsync())
  83. {
  84. yield break;
  85. }
  86. var latest = e.Current;
  87. yield return latest;
  88. while (await e.MoveNextAsync())
  89. {
  90. var item = e.Current;
  91. // REVIEW: Need comparer!.Equals to satisfy nullable reference type warnings.
  92. if (!comparer!.Equals(latest, item))
  93. {
  94. latest = item;
  95. yield return latest;
  96. }
  97. }
  98. }
  99. }
  100. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
  101. {
  102. comparer ??= EqualityComparer<TKey>.Default;
  103. return AsyncEnumerable.Create(Core);
  104. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  105. {
  106. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  107. if (!await e.MoveNextAsync())
  108. {
  109. yield break;
  110. }
  111. var item = e.Current;
  112. var latestKey = keySelector(item);
  113. yield return item;
  114. while (await e.MoveNextAsync())
  115. {
  116. item = e.Current;
  117. var currentKey = keySelector(item);
  118. // REVIEW: Need comparer!.Equals to satisfy nullable reference type warnings.
  119. if (!comparer!.Equals(latestKey, currentKey))
  120. {
  121. latestKey = currentKey;
  122. yield return item;
  123. }
  124. }
  125. }
  126. }
  127. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer)
  128. {
  129. comparer ??= EqualityComparer<TKey>.Default;
  130. return AsyncEnumerable.Create(Core);
  131. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  132. {
  133. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  134. if (!await e.MoveNextAsync())
  135. {
  136. yield break;
  137. }
  138. var item = e.Current;
  139. var latestKey = await keySelector(item).ConfigureAwait(false);
  140. yield return item;
  141. while (await e.MoveNextAsync())
  142. {
  143. item = e.Current;
  144. var currentKey = await keySelector(item).ConfigureAwait(false);
  145. // REVIEW: Need comparer!.Equals to satisfy nullable reference type warnings.
  146. if (!comparer!.Equals(latestKey, currentKey))
  147. {
  148. latestKey = currentKey;
  149. yield return item;
  150. }
  151. }
  152. }
  153. }
  154. #if !NO_DEEP_CANCELLATION
  155. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer)
  156. {
  157. comparer ??= EqualityComparer<TKey>.Default;
  158. return AsyncEnumerable.Create(Core);
  159. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  160. {
  161. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  162. if (!await e.MoveNextAsync())
  163. {
  164. yield break;
  165. }
  166. var item = e.Current;
  167. var latestKey = await keySelector(item, cancellationToken).ConfigureAwait(false);
  168. yield return item;
  169. while (await e.MoveNextAsync())
  170. {
  171. item = e.Current;
  172. var currentKey = await keySelector(item, cancellationToken).ConfigureAwait(false);
  173. // REVIEW: Need comparer!.Equals to satisfy nullable reference type warnings.
  174. if (!comparer!.Equals(latestKey, currentKey))
  175. {
  176. latestKey = currentKey;
  177. yield return item;
  178. }
  179. }
  180. }
  181. }
  182. #endif
  183. }
  184. }