DistinctUntilChanged.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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> DistinctUntilChanged<TSource>(this IAsyncEnumerable<TSource> source)
  13. {
  14. if (source == null)
  15. throw new ArgumentNullException(nameof(source));
  16. return DistinctUntilChangedCore(source, EqualityComparer<TSource>.Default);
  17. }
  18. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource>(this IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  19. {
  20. if (source == null)
  21. throw new ArgumentNullException(nameof(source));
  22. if (comparer == null)
  23. throw new ArgumentNullException(nameof(comparer));
  24. return DistinctUntilChangedCore(source, comparer);
  25. }
  26. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  27. {
  28. if (source == null)
  29. throw new ArgumentNullException(nameof(source));
  30. if (keySelector == null)
  31. throw new ArgumentNullException(nameof(keySelector));
  32. return DistinctUntilChangedCore(source, keySelector, EqualityComparer<TKey>.Default);
  33. }
  34. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  35. {
  36. if (source == null)
  37. throw new ArgumentNullException(nameof(source));
  38. if (keySelector == null)
  39. throw new ArgumentNullException(nameof(keySelector));
  40. if (comparer == null)
  41. throw new ArgumentNullException(nameof(comparer));
  42. return DistinctUntilChangedCore(source, keySelector, comparer);
  43. }
  44. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector)
  45. {
  46. if (source == null)
  47. throw new ArgumentNullException(nameof(source));
  48. if (keySelector == null)
  49. throw new ArgumentNullException(nameof(keySelector));
  50. return DistinctUntilChangedCore(source, keySelector, EqualityComparer<TKey>.Default);
  51. }
  52. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  53. {
  54. if (source == null)
  55. throw new ArgumentNullException(nameof(source));
  56. if (keySelector == null)
  57. throw new ArgumentNullException(nameof(keySelector));
  58. if (comparer == null)
  59. throw new ArgumentNullException(nameof(comparer));
  60. return DistinctUntilChangedCore(source, keySelector, comparer);
  61. }
  62. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource>(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  63. {
  64. return new DistinctUntilChangedAsyncIterator<TSource>(source, comparer);
  65. }
  66. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  67. {
  68. return new DistinctUntilChangedAsyncIterator<TSource, TKey>(source, keySelector, comparer);
  69. }
  70. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  71. {
  72. return new DistinctUntilChangedAsyncIteratorWithTask<TSource, TKey>(source, keySelector, comparer);
  73. }
  74. private sealed class DistinctUntilChangedAsyncIterator<TSource> : AsyncIterator<TSource>
  75. {
  76. private readonly IEqualityComparer<TSource> comparer;
  77. private readonly IAsyncEnumerable<TSource> source;
  78. private TSource currentValue;
  79. private IAsyncEnumerator<TSource> enumerator;
  80. private bool hasCurrentValue;
  81. public DistinctUntilChangedAsyncIterator(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  82. {
  83. Debug.Assert(comparer != null);
  84. Debug.Assert(source != null);
  85. this.source = source;
  86. this.comparer = comparer;
  87. }
  88. public override AsyncIterator<TSource> Clone()
  89. {
  90. return new DistinctUntilChangedAsyncIterator<TSource>(source, comparer);
  91. }
  92. public override async ValueTask DisposeAsync()
  93. {
  94. if (enumerator != null)
  95. {
  96. await enumerator.DisposeAsync().ConfigureAwait(false);
  97. enumerator = null;
  98. currentValue = default;
  99. }
  100. await base.DisposeAsync().ConfigureAwait(false);
  101. }
  102. protected override async ValueTask<bool> MoveNextCore(CancellationToken cancellationToken)
  103. {
  104. switch (state)
  105. {
  106. case AsyncIteratorState.Allocated:
  107. enumerator = source.GetAsyncEnumerator(cancellationToken);
  108. state = AsyncIteratorState.Iterating;
  109. goto case AsyncIteratorState.Iterating;
  110. case AsyncIteratorState.Iterating:
  111. while (await enumerator.MoveNextAsync().ConfigureAwait(false))
  112. {
  113. var item = enumerator.Current;
  114. var comparerEquals = false;
  115. if (hasCurrentValue)
  116. {
  117. comparerEquals = comparer.Equals(currentValue, item);
  118. }
  119. if (!hasCurrentValue || !comparerEquals)
  120. {
  121. hasCurrentValue = true;
  122. currentValue = item;
  123. current = item;
  124. return true;
  125. }
  126. }
  127. break;
  128. }
  129. await DisposeAsync().ConfigureAwait(false);
  130. return false;
  131. }
  132. }
  133. private sealed class DistinctUntilChangedAsyncIterator<TSource, TKey> : AsyncIterator<TSource>
  134. {
  135. private readonly IEqualityComparer<TKey> comparer;
  136. private readonly Func<TSource, TKey> keySelector;
  137. private readonly IAsyncEnumerable<TSource> source;
  138. private TKey currentKeyValue;
  139. private IAsyncEnumerator<TSource> enumerator;
  140. private bool hasCurrentKey;
  141. public DistinctUntilChangedAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  142. {
  143. this.source = source;
  144. this.keySelector = keySelector;
  145. this.comparer = comparer;
  146. }
  147. public override AsyncIterator<TSource> Clone()
  148. {
  149. return new DistinctUntilChangedAsyncIterator<TSource, TKey>(source, keySelector, comparer);
  150. }
  151. public override async ValueTask DisposeAsync()
  152. {
  153. if (enumerator != null)
  154. {
  155. await enumerator.DisposeAsync().ConfigureAwait(false);
  156. enumerator = null;
  157. currentKeyValue = default;
  158. }
  159. await base.DisposeAsync().ConfigureAwait(false);
  160. }
  161. protected override async ValueTask<bool> MoveNextCore(CancellationToken cancellationToken)
  162. {
  163. switch (state)
  164. {
  165. case AsyncIteratorState.Allocated:
  166. enumerator = source.GetAsyncEnumerator(cancellationToken);
  167. state = AsyncIteratorState.Iterating;
  168. goto case AsyncIteratorState.Iterating;
  169. case AsyncIteratorState.Iterating:
  170. while (await enumerator.MoveNextAsync().ConfigureAwait(false))
  171. {
  172. var item = enumerator.Current;
  173. var key = keySelector(item);
  174. var comparerEquals = false;
  175. if (hasCurrentKey)
  176. {
  177. comparerEquals = comparer.Equals(currentKeyValue, key);
  178. }
  179. if (!hasCurrentKey || !comparerEquals)
  180. {
  181. hasCurrentKey = true;
  182. currentKeyValue = key;
  183. current = item;
  184. return true;
  185. }
  186. }
  187. break; // case
  188. }
  189. await DisposeAsync().ConfigureAwait(false);
  190. return false;
  191. }
  192. }
  193. private sealed class DistinctUntilChangedAsyncIteratorWithTask<TSource, TKey> : AsyncIterator<TSource>
  194. {
  195. private readonly IEqualityComparer<TKey> comparer;
  196. private readonly Func<TSource, Task<TKey>> keySelector;
  197. private readonly IAsyncEnumerable<TSource> source;
  198. private TKey currentKeyValue;
  199. private IAsyncEnumerator<TSource> enumerator;
  200. private bool hasCurrentKey;
  201. public DistinctUntilChangedAsyncIteratorWithTask(IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  202. {
  203. this.source = source;
  204. this.keySelector = keySelector;
  205. this.comparer = comparer;
  206. }
  207. public override AsyncIterator<TSource> Clone()
  208. {
  209. return new DistinctUntilChangedAsyncIteratorWithTask<TSource, TKey>(source, keySelector, comparer);
  210. }
  211. public override async ValueTask DisposeAsync()
  212. {
  213. if (enumerator != null)
  214. {
  215. await enumerator.DisposeAsync().ConfigureAwait(false);
  216. enumerator = null;
  217. currentKeyValue = default;
  218. }
  219. await base.DisposeAsync().ConfigureAwait(false);
  220. }
  221. protected override async ValueTask<bool> MoveNextCore(CancellationToken cancellationToken)
  222. {
  223. switch (state)
  224. {
  225. case AsyncIteratorState.Allocated:
  226. enumerator = source.GetAsyncEnumerator(cancellationToken);
  227. state = AsyncIteratorState.Iterating;
  228. goto case AsyncIteratorState.Iterating;
  229. case AsyncIteratorState.Iterating:
  230. while (await enumerator.MoveNextAsync().ConfigureAwait(false))
  231. {
  232. var item = enumerator.Current;
  233. var key = await keySelector(item).ConfigureAwait(false);
  234. var comparerEquals = false;
  235. if (hasCurrentKey)
  236. {
  237. comparerEquals = comparer.Equals(currentKeyValue, key);
  238. }
  239. if (!hasCurrentKey || !comparerEquals)
  240. {
  241. hasCurrentKey = true;
  242. currentKeyValue = key;
  243. current = item;
  244. return true;
  245. }
  246. }
  247. break; // case
  248. }
  249. await DisposeAsync().ConfigureAwait(false);
  250. return false;
  251. }
  252. }
  253. }
  254. }