DistinctUntilChanged.cs 12 KB

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