DistinctUntilChanged.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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, ValueTask<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. #if !NO_DEEP_CANCELLATION
  49. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector)
  50. {
  51. if (source == null)
  52. throw Error.ArgumentNull(nameof(source));
  53. if (keySelector == null)
  54. throw Error.ArgumentNull(nameof(keySelector));
  55. return DistinctUntilChangedCore<TSource, TKey>(source, keySelector, comparer: null);
  56. }
  57. #endif
  58. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  59. {
  60. if (source == null)
  61. throw Error.ArgumentNull(nameof(source));
  62. if (keySelector == null)
  63. throw Error.ArgumentNull(nameof(keySelector));
  64. return DistinctUntilChangedCore(source, keySelector, comparer);
  65. }
  66. #if !NO_DEEP_CANCELLATION
  67. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  68. {
  69. if (source == null)
  70. throw Error.ArgumentNull(nameof(source));
  71. if (keySelector == null)
  72. throw Error.ArgumentNull(nameof(keySelector));
  73. return DistinctUntilChangedCore(source, keySelector, comparer);
  74. }
  75. #endif
  76. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource>(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  77. {
  78. return new DistinctUntilChangedAsyncIterator<TSource>(source, comparer);
  79. }
  80. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  81. {
  82. return new DistinctUntilChangedAsyncIterator<TSource, TKey>(source, keySelector, comparer);
  83. }
  84. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  85. {
  86. return new DistinctUntilChangedAsyncIteratorWithTask<TSource, TKey>(source, keySelector, comparer);
  87. }
  88. #if !NO_DEEP_CANCELLATION
  89. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  90. {
  91. return new DistinctUntilChangedAsyncIteratorWithTaskAndCancellation<TSource, TKey>(source, keySelector, comparer);
  92. }
  93. #endif
  94. private sealed class DistinctUntilChangedAsyncIterator<TSource> : AsyncIterator<TSource>
  95. {
  96. private readonly IEqualityComparer<TSource> _comparer;
  97. private readonly IAsyncEnumerable<TSource> _source;
  98. private TSource _currentValue;
  99. private IAsyncEnumerator<TSource> _enumerator;
  100. private bool _hasCurrentValue;
  101. public DistinctUntilChangedAsyncIterator(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  102. {
  103. Debug.Assert(source != null);
  104. _source = source;
  105. _comparer = comparer ?? EqualityComparer<TSource>.Default;
  106. }
  107. public override AsyncIteratorBase<TSource> Clone()
  108. {
  109. return new DistinctUntilChangedAsyncIterator<TSource>(_source, _comparer);
  110. }
  111. public override async ValueTask DisposeAsync()
  112. {
  113. if (_enumerator != null)
  114. {
  115. await _enumerator.DisposeAsync().ConfigureAwait(false);
  116. _enumerator = null;
  117. _currentValue = default;
  118. }
  119. await base.DisposeAsync().ConfigureAwait(false);
  120. }
  121. protected override async ValueTask<bool> MoveNextCore()
  122. {
  123. switch (_state)
  124. {
  125. case AsyncIteratorState.Allocated:
  126. _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
  127. _state = AsyncIteratorState.Iterating;
  128. goto case AsyncIteratorState.Iterating;
  129. case AsyncIteratorState.Iterating:
  130. while (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  131. {
  132. TSource item = _enumerator.Current;
  133. var comparerEquals = false;
  134. if (_hasCurrentValue)
  135. {
  136. comparerEquals = _comparer.Equals(_currentValue, item);
  137. }
  138. if (!_hasCurrentValue || !comparerEquals)
  139. {
  140. _hasCurrentValue = true;
  141. _currentValue = item;
  142. _current = item;
  143. return true;
  144. }
  145. }
  146. break;
  147. }
  148. await DisposeAsync().ConfigureAwait(false);
  149. return false;
  150. }
  151. }
  152. private sealed class DistinctUntilChangedAsyncIterator<TSource, TKey> : AsyncIterator<TSource>
  153. {
  154. private readonly IEqualityComparer<TKey> _comparer;
  155. private readonly Func<TSource, TKey> _keySelector;
  156. private readonly IAsyncEnumerable<TSource> _source;
  157. private TKey _currentKeyValue;
  158. private IAsyncEnumerator<TSource> _enumerator;
  159. private bool _hasCurrentKey;
  160. public DistinctUntilChangedAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  161. {
  162. _source = source;
  163. _keySelector = keySelector;
  164. _comparer = comparer ?? EqualityComparer<TKey>.Default;
  165. }
  166. public override AsyncIteratorBase<TSource> Clone()
  167. {
  168. return new DistinctUntilChangedAsyncIterator<TSource, TKey>(_source, _keySelector, _comparer);
  169. }
  170. public override async ValueTask DisposeAsync()
  171. {
  172. if (_enumerator != null)
  173. {
  174. await _enumerator.DisposeAsync().ConfigureAwait(false);
  175. _enumerator = null;
  176. _currentKeyValue = default;
  177. }
  178. await base.DisposeAsync().ConfigureAwait(false);
  179. }
  180. protected override async ValueTask<bool> MoveNextCore()
  181. {
  182. switch (_state)
  183. {
  184. case AsyncIteratorState.Allocated:
  185. _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
  186. _state = AsyncIteratorState.Iterating;
  187. goto case AsyncIteratorState.Iterating;
  188. case AsyncIteratorState.Iterating:
  189. while (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  190. {
  191. TSource item = _enumerator.Current;
  192. TKey key = _keySelector(item);
  193. var comparerEquals = false;
  194. if (_hasCurrentKey)
  195. {
  196. comparerEquals = _comparer.Equals(_currentKeyValue, key);
  197. }
  198. if (!_hasCurrentKey || !comparerEquals)
  199. {
  200. _hasCurrentKey = true;
  201. _currentKeyValue = key;
  202. _current = item;
  203. return true;
  204. }
  205. }
  206. break; // case
  207. }
  208. await DisposeAsync().ConfigureAwait(false);
  209. return false;
  210. }
  211. }
  212. private sealed class DistinctUntilChangedAsyncIteratorWithTask<TSource, TKey> : AsyncIterator<TSource>
  213. {
  214. private readonly IEqualityComparer<TKey> _comparer;
  215. private readonly Func<TSource, ValueTask<TKey>> _keySelector;
  216. private readonly IAsyncEnumerable<TSource> _source;
  217. private TKey _currentKeyValue;
  218. private IAsyncEnumerator<TSource> _enumerator;
  219. private bool _hasCurrentKey;
  220. public DistinctUntilChangedAsyncIteratorWithTask(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  221. {
  222. _source = source;
  223. _keySelector = keySelector;
  224. _comparer = comparer ?? EqualityComparer<TKey>.Default;
  225. }
  226. public override AsyncIteratorBase<TSource> Clone()
  227. {
  228. return new DistinctUntilChangedAsyncIteratorWithTask<TSource, TKey>(_source, _keySelector, _comparer);
  229. }
  230. public override async ValueTask DisposeAsync()
  231. {
  232. if (_enumerator != null)
  233. {
  234. await _enumerator.DisposeAsync().ConfigureAwait(false);
  235. _enumerator = null;
  236. _currentKeyValue = default;
  237. }
  238. await base.DisposeAsync().ConfigureAwait(false);
  239. }
  240. protected override async ValueTask<bool> MoveNextCore()
  241. {
  242. switch (_state)
  243. {
  244. case AsyncIteratorState.Allocated:
  245. _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
  246. _state = AsyncIteratorState.Iterating;
  247. goto case AsyncIteratorState.Iterating;
  248. case AsyncIteratorState.Iterating:
  249. while (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  250. {
  251. TSource item = _enumerator.Current;
  252. TKey key = await _keySelector(item).ConfigureAwait(false);
  253. var comparerEquals = false;
  254. if (_hasCurrentKey)
  255. {
  256. comparerEquals = _comparer.Equals(_currentKeyValue, key);
  257. }
  258. if (!_hasCurrentKey || !comparerEquals)
  259. {
  260. _hasCurrentKey = true;
  261. _currentKeyValue = key;
  262. _current = item;
  263. return true;
  264. }
  265. }
  266. break; // case
  267. }
  268. await DisposeAsync().ConfigureAwait(false);
  269. return false;
  270. }
  271. }
  272. #if !NO_DEEP_CANCELLATION
  273. private sealed class DistinctUntilChangedAsyncIteratorWithTaskAndCancellation<TSource, TKey> : AsyncIterator<TSource>
  274. {
  275. private readonly IEqualityComparer<TKey> _comparer;
  276. private readonly Func<TSource, CancellationToken, ValueTask<TKey>> _keySelector;
  277. private readonly IAsyncEnumerable<TSource> _source;
  278. private TKey _currentKeyValue;
  279. private IAsyncEnumerator<TSource> _enumerator;
  280. private bool _hasCurrentKey;
  281. public DistinctUntilChangedAsyncIteratorWithTaskAndCancellation(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  282. {
  283. _source = source;
  284. _keySelector = keySelector;
  285. _comparer = comparer ?? EqualityComparer<TKey>.Default;
  286. }
  287. public override AsyncIteratorBase<TSource> Clone()
  288. {
  289. return new DistinctUntilChangedAsyncIteratorWithTaskAndCancellation<TSource, TKey>(_source, _keySelector, _comparer);
  290. }
  291. public override async ValueTask DisposeAsync()
  292. {
  293. if (_enumerator != null)
  294. {
  295. await _enumerator.DisposeAsync().ConfigureAwait(false);
  296. _enumerator = null;
  297. _currentKeyValue = default;
  298. }
  299. await base.DisposeAsync().ConfigureAwait(false);
  300. }
  301. protected override async ValueTask<bool> MoveNextCore()
  302. {
  303. switch (_state)
  304. {
  305. case AsyncIteratorState.Allocated:
  306. _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
  307. _state = AsyncIteratorState.Iterating;
  308. goto case AsyncIteratorState.Iterating;
  309. case AsyncIteratorState.Iterating:
  310. while (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  311. {
  312. TSource item = _enumerator.Current;
  313. TKey key = await _keySelector(item, _cancellationToken).ConfigureAwait(false);
  314. var comparerEquals = false;
  315. if (_hasCurrentKey)
  316. {
  317. comparerEquals = _comparer.Equals(_currentKeyValue, key);
  318. }
  319. if (!_hasCurrentKey || !comparerEquals)
  320. {
  321. _hasCurrentKey = true;
  322. _currentKeyValue = key;
  323. _current = item;
  324. return true;
  325. }
  326. }
  327. break; // case
  328. }
  329. await DisposeAsync().ConfigureAwait(false);
  330. return false;
  331. }
  332. }
  333. #endif
  334. }
  335. }