DistinctUntilChanged.cs 8.9 KB

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