DistinctUntilChanged.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. #if USE_ASYNC_ITERATOR
  79. if (comparer == null)
  80. {
  81. comparer = EqualityComparer<TSource>.Default;
  82. }
  83. return AsyncEnumerable.Create(Core);
  84. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  85. {
  86. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  87. {
  88. if (!await e.MoveNextAsync())
  89. {
  90. yield break;
  91. }
  92. var latest = e.Current;
  93. yield return latest;
  94. while (await e.MoveNextAsync())
  95. {
  96. var item = e.Current;
  97. if (!comparer.Equals(latest, item))
  98. {
  99. latest = item;
  100. yield return latest;
  101. }
  102. }
  103. }
  104. }
  105. #else
  106. return new DistinctUntilChangedAsyncIterator<TSource>(source, comparer);
  107. #endif
  108. }
  109. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  110. {
  111. #if USE_ASYNC_ITERATOR
  112. if (comparer == null)
  113. {
  114. comparer = EqualityComparer<TKey>.Default;
  115. }
  116. return AsyncEnumerable.Create(Core);
  117. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  118. {
  119. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  120. {
  121. if (!await e.MoveNextAsync())
  122. {
  123. yield break;
  124. }
  125. var item = e.Current;
  126. var latestKey = keySelector(item);
  127. yield return item;
  128. while (await e.MoveNextAsync())
  129. {
  130. item = e.Current;
  131. var currentKey = keySelector(item);
  132. if (!comparer.Equals(latestKey, currentKey))
  133. {
  134. latestKey = currentKey;
  135. yield return item;
  136. }
  137. }
  138. }
  139. }
  140. #else
  141. return new DistinctUntilChangedAsyncIterator<TSource, TKey>(source, keySelector, comparer);
  142. #endif
  143. }
  144. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  145. {
  146. #if USE_ASYNC_ITERATOR
  147. if (comparer == null)
  148. {
  149. comparer = EqualityComparer<TKey>.Default;
  150. }
  151. return AsyncEnumerable.Create(Core);
  152. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  153. {
  154. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  155. {
  156. if (!await e.MoveNextAsync())
  157. {
  158. yield break;
  159. }
  160. var item = e.Current;
  161. var latestKey = await keySelector(item).ConfigureAwait(false);
  162. yield return item;
  163. while (await e.MoveNextAsync())
  164. {
  165. item = e.Current;
  166. var currentKey = await keySelector(item).ConfigureAwait(false);
  167. if (!comparer.Equals(latestKey, currentKey))
  168. {
  169. latestKey = currentKey;
  170. yield return item;
  171. }
  172. }
  173. }
  174. }
  175. #else
  176. return new DistinctUntilChangedAsyncIteratorWithTask<TSource, TKey>(source, keySelector, comparer);
  177. #endif
  178. }
  179. #if !NO_DEEP_CANCELLATION
  180. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  181. {
  182. #if USE_ASYNC_ITERATOR
  183. if (comparer == null)
  184. {
  185. comparer = EqualityComparer<TKey>.Default;
  186. }
  187. return AsyncEnumerable.Create(Core);
  188. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  189. {
  190. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  191. {
  192. if (!await e.MoveNextAsync())
  193. {
  194. yield break;
  195. }
  196. var item = e.Current;
  197. var latestKey = await keySelector(item, cancellationToken).ConfigureAwait(false);
  198. yield return item;
  199. while (await e.MoveNextAsync())
  200. {
  201. item = e.Current;
  202. var currentKey = await keySelector(item, cancellationToken).ConfigureAwait(false);
  203. if (!comparer.Equals(latestKey, currentKey))
  204. {
  205. latestKey = currentKey;
  206. yield return item;
  207. }
  208. }
  209. }
  210. }
  211. #else
  212. return new DistinctUntilChangedAsyncIteratorWithTaskAndCancellation<TSource, TKey>(source, keySelector, comparer);
  213. #endif
  214. }
  215. #endif
  216. #if !USE_ASYNC_ITERATOR
  217. private sealed class DistinctUntilChangedAsyncIterator<TSource> : AsyncIterator<TSource>
  218. {
  219. private readonly IEqualityComparer<TSource> _comparer;
  220. private readonly IAsyncEnumerable<TSource> _source;
  221. private TSource _currentValue;
  222. private IAsyncEnumerator<TSource> _enumerator;
  223. private bool _hasCurrentValue;
  224. public DistinctUntilChangedAsyncIterator(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  225. {
  226. Debug.Assert(source != null);
  227. _source = source;
  228. _comparer = comparer ?? EqualityComparer<TSource>.Default;
  229. }
  230. public override AsyncIteratorBase<TSource> Clone()
  231. {
  232. return new DistinctUntilChangedAsyncIterator<TSource>(_source, _comparer);
  233. }
  234. public override async ValueTask DisposeAsync()
  235. {
  236. if (_enumerator != null)
  237. {
  238. await _enumerator.DisposeAsync().ConfigureAwait(false);
  239. _enumerator = null;
  240. _currentValue = 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 comparerEquals = false;
  257. if (_hasCurrentValue)
  258. {
  259. comparerEquals = _comparer.Equals(_currentValue, item);
  260. }
  261. if (!_hasCurrentValue || !comparerEquals)
  262. {
  263. _hasCurrentValue = true;
  264. _currentValue = item;
  265. _current = item;
  266. return true;
  267. }
  268. }
  269. break;
  270. }
  271. await DisposeAsync().ConfigureAwait(false);
  272. return false;
  273. }
  274. }
  275. private sealed class DistinctUntilChangedAsyncIterator<TSource, TKey> : AsyncIterator<TSource>
  276. {
  277. private readonly IEqualityComparer<TKey> _comparer;
  278. private readonly Func<TSource, TKey> _keySelector;
  279. private readonly IAsyncEnumerable<TSource> _source;
  280. private TKey _currentKeyValue;
  281. private IAsyncEnumerator<TSource> _enumerator;
  282. private bool _hasCurrentKey;
  283. public DistinctUntilChangedAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  284. {
  285. _source = source;
  286. _keySelector = keySelector;
  287. _comparer = comparer ?? EqualityComparer<TKey>.Default;
  288. }
  289. public override AsyncIteratorBase<TSource> Clone()
  290. {
  291. return new DistinctUntilChangedAsyncIterator<TSource, TKey>(_source, _keySelector, _comparer);
  292. }
  293. public override async ValueTask DisposeAsync()
  294. {
  295. if (_enumerator != null)
  296. {
  297. await _enumerator.DisposeAsync().ConfigureAwait(false);
  298. _enumerator = null;
  299. _currentKeyValue = default;
  300. }
  301. await base.DisposeAsync().ConfigureAwait(false);
  302. }
  303. protected override async ValueTask<bool> MoveNextCore()
  304. {
  305. switch (_state)
  306. {
  307. case AsyncIteratorState.Allocated:
  308. _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
  309. _state = AsyncIteratorState.Iterating;
  310. goto case AsyncIteratorState.Iterating;
  311. case AsyncIteratorState.Iterating:
  312. while (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  313. {
  314. var item = _enumerator.Current;
  315. var key = _keySelector(item);
  316. var comparerEquals = false;
  317. if (_hasCurrentKey)
  318. {
  319. comparerEquals = _comparer.Equals(_currentKeyValue, key);
  320. }
  321. if (!_hasCurrentKey || !comparerEquals)
  322. {
  323. _hasCurrentKey = true;
  324. _currentKeyValue = key;
  325. _current = item;
  326. return true;
  327. }
  328. }
  329. break; // case
  330. }
  331. await DisposeAsync().ConfigureAwait(false);
  332. return false;
  333. }
  334. }
  335. private sealed class DistinctUntilChangedAsyncIteratorWithTask<TSource, TKey> : AsyncIterator<TSource>
  336. {
  337. private readonly IEqualityComparer<TKey> _comparer;
  338. private readonly Func<TSource, ValueTask<TKey>> _keySelector;
  339. private readonly IAsyncEnumerable<TSource> _source;
  340. private TKey _currentKeyValue;
  341. private IAsyncEnumerator<TSource> _enumerator;
  342. private bool _hasCurrentKey;
  343. public DistinctUntilChangedAsyncIteratorWithTask(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  344. {
  345. _source = source;
  346. _keySelector = keySelector;
  347. _comparer = comparer ?? EqualityComparer<TKey>.Default;
  348. }
  349. public override AsyncIteratorBase<TSource> Clone()
  350. {
  351. return new DistinctUntilChangedAsyncIteratorWithTask<TSource, TKey>(_source, _keySelector, _comparer);
  352. }
  353. public override async ValueTask DisposeAsync()
  354. {
  355. if (_enumerator != null)
  356. {
  357. await _enumerator.DisposeAsync().ConfigureAwait(false);
  358. _enumerator = null;
  359. _currentKeyValue = default;
  360. }
  361. await base.DisposeAsync().ConfigureAwait(false);
  362. }
  363. protected override async ValueTask<bool> MoveNextCore()
  364. {
  365. switch (_state)
  366. {
  367. case AsyncIteratorState.Allocated:
  368. _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
  369. _state = AsyncIteratorState.Iterating;
  370. goto case AsyncIteratorState.Iterating;
  371. case AsyncIteratorState.Iterating:
  372. while (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  373. {
  374. var item = _enumerator.Current;
  375. var key = await _keySelector(item).ConfigureAwait(false);
  376. var comparerEquals = false;
  377. if (_hasCurrentKey)
  378. {
  379. comparerEquals = _comparer.Equals(_currentKeyValue, key);
  380. }
  381. if (!_hasCurrentKey || !comparerEquals)
  382. {
  383. _hasCurrentKey = true;
  384. _currentKeyValue = key;
  385. _current = item;
  386. return true;
  387. }
  388. }
  389. break; // case
  390. }
  391. await DisposeAsync().ConfigureAwait(false);
  392. return false;
  393. }
  394. }
  395. #if !NO_DEEP_CANCELLATION
  396. private sealed class DistinctUntilChangedAsyncIteratorWithTaskAndCancellation<TSource, TKey> : AsyncIterator<TSource>
  397. {
  398. private readonly IEqualityComparer<TKey> _comparer;
  399. private readonly Func<TSource, CancellationToken, ValueTask<TKey>> _keySelector;
  400. private readonly IAsyncEnumerable<TSource> _source;
  401. private TKey _currentKeyValue;
  402. private IAsyncEnumerator<TSource> _enumerator;
  403. private bool _hasCurrentKey;
  404. public DistinctUntilChangedAsyncIteratorWithTaskAndCancellation(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  405. {
  406. _source = source;
  407. _keySelector = keySelector;
  408. _comparer = comparer ?? EqualityComparer<TKey>.Default;
  409. }
  410. public override AsyncIteratorBase<TSource> Clone()
  411. {
  412. return new DistinctUntilChangedAsyncIteratorWithTaskAndCancellation<TSource, TKey>(_source, _keySelector, _comparer);
  413. }
  414. public override async ValueTask DisposeAsync()
  415. {
  416. if (_enumerator != null)
  417. {
  418. await _enumerator.DisposeAsync().ConfigureAwait(false);
  419. _enumerator = null;
  420. _currentKeyValue = default;
  421. }
  422. await base.DisposeAsync().ConfigureAwait(false);
  423. }
  424. protected override async ValueTask<bool> MoveNextCore()
  425. {
  426. switch (_state)
  427. {
  428. case AsyncIteratorState.Allocated:
  429. _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
  430. _state = AsyncIteratorState.Iterating;
  431. goto case AsyncIteratorState.Iterating;
  432. case AsyncIteratorState.Iterating:
  433. while (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  434. {
  435. var item = _enumerator.Current;
  436. var key = await _keySelector(item, _cancellationToken).ConfigureAwait(false);
  437. var comparerEquals = false;
  438. if (_hasCurrentKey)
  439. {
  440. comparerEquals = _comparer.Equals(_currentKeyValue, key);
  441. }
  442. if (!_hasCurrentKey || !comparerEquals)
  443. {
  444. _hasCurrentKey = true;
  445. _currentKeyValue = key;
  446. _current = item;
  447. return true;
  448. }
  449. }
  450. break; // case
  451. }
  452. await DisposeAsync().ConfigureAwait(false);
  453. return false;
  454. }
  455. }
  456. #endif
  457. #endif
  458. }
  459. }