Intersect.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 AsyncEnumerable
  11. {
  12. public static IAsyncEnumerable<TSource> Intersect<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second) =>
  13. Intersect(first, second, comparer: null);
  14. public static IAsyncEnumerable<TSource> Intersect<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  15. {
  16. if (first == null)
  17. throw Error.ArgumentNull(nameof(first));
  18. if (second == null)
  19. throw Error.ArgumentNull(nameof(second));
  20. #if USE_ASYNC_ITERATOR
  21. return Create(Core);
  22. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  23. {
  24. var set = new Set<TSource>(comparer);
  25. await foreach (var element in second.WithCancellation(cancellationToken).ConfigureAwait(false))
  26. {
  27. set.Add(element);
  28. }
  29. await foreach (var element in first.WithCancellation(cancellationToken).ConfigureAwait(false))
  30. {
  31. if (set.Remove(element))
  32. {
  33. yield return element;
  34. }
  35. }
  36. }
  37. #else
  38. return new IntersectAsyncIterator<TSource>(first, second, comparer);
  39. #endif
  40. }
  41. #if !USE_ASYNC_ITERATOR
  42. private sealed class IntersectAsyncIterator<TSource> : AsyncIterator<TSource>
  43. {
  44. private readonly IEqualityComparer<TSource> _comparer;
  45. private readonly IAsyncEnumerable<TSource> _first;
  46. private readonly IAsyncEnumerable<TSource> _second;
  47. private IAsyncEnumerator<TSource> _firstEnumerator;
  48. private Set<TSource> _set;
  49. public IntersectAsyncIterator(IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  50. {
  51. Debug.Assert(first != null);
  52. Debug.Assert(second != null);
  53. _first = first;
  54. _second = second;
  55. _comparer = comparer;
  56. }
  57. public override AsyncIteratorBase<TSource> Clone()
  58. {
  59. return new IntersectAsyncIterator<TSource>(_first, _second, _comparer);
  60. }
  61. public override async ValueTask DisposeAsync()
  62. {
  63. if (_firstEnumerator != null)
  64. {
  65. await _firstEnumerator.DisposeAsync().ConfigureAwait(false);
  66. _firstEnumerator = null;
  67. }
  68. _set = null;
  69. await base.DisposeAsync().ConfigureAwait(false);
  70. }
  71. protected override async ValueTask<bool> MoveNextCore()
  72. {
  73. // NB: Earlier implementations of this operator constructed the set for the second source concurrently
  74. // with the first MoveNextAsync call on the first source. This resulted in an unexpected source of
  75. // concurrency, which isn't a great default behavior because it's very hard to suppress or control
  76. // this behavior.
  77. switch (_state)
  78. {
  79. case AsyncIteratorState.Allocated:
  80. _set = await AsyncEnumerableHelpers.ToSet(_second, _comparer, _cancellationToken).ConfigureAwait(false);
  81. _firstEnumerator = _first.GetAsyncEnumerator(_cancellationToken);
  82. _state = AsyncIteratorState.Iterating;
  83. goto case AsyncIteratorState.Iterating;
  84. case AsyncIteratorState.Iterating:
  85. bool moveNext;
  86. do
  87. {
  88. moveNext = await _firstEnumerator.MoveNextAsync().ConfigureAwait(false);
  89. if (moveNext)
  90. {
  91. var item = _firstEnumerator.Current;
  92. if (_set.Remove(item))
  93. {
  94. _current = item;
  95. return true;
  96. }
  97. }
  98. } while (moveNext);
  99. await DisposeAsync().ConfigureAwait(false);
  100. break;
  101. }
  102. return false;
  103. }
  104. }
  105. #endif
  106. }
  107. }