DistinctUntilChanged.cs 17 KB

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