Except.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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> Except<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second)
  13. {
  14. if (first == null)
  15. throw Error.ArgumentNull(nameof(first));
  16. if (second == null)
  17. throw Error.ArgumentNull(nameof(second));
  18. return new ExceptAsyncIterator<TSource>(first, second, EqualityComparer<TSource>.Default);
  19. }
  20. public static IAsyncEnumerable<TSource> Except<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  21. {
  22. if (first == null)
  23. throw Error.ArgumentNull(nameof(first));
  24. if (second == null)
  25. throw Error.ArgumentNull(nameof(second));
  26. if (comparer == null)
  27. throw Error.ArgumentNull(nameof(comparer));
  28. return new ExceptAsyncIterator<TSource>(first, second, comparer);
  29. }
  30. private sealed class ExceptAsyncIterator<TSource> : AsyncIterator<TSource>
  31. {
  32. private readonly IEqualityComparer<TSource> _comparer;
  33. private readonly IAsyncEnumerable<TSource> _first;
  34. private readonly IAsyncEnumerable<TSource> _second;
  35. private Task _fillSetTask;
  36. private IAsyncEnumerator<TSource> _firstEnumerator;
  37. private Set<TSource> _set;
  38. private bool _setFilled;
  39. public ExceptAsyncIterator(IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  40. {
  41. Debug.Assert(first != null);
  42. Debug.Assert(second != null);
  43. Debug.Assert(comparer != null);
  44. _first = first;
  45. _second = second;
  46. _comparer = comparer;
  47. }
  48. public override AsyncIterator<TSource> Clone()
  49. {
  50. return new ExceptAsyncIterator<TSource>(_first, _second, _comparer);
  51. }
  52. public override async ValueTask DisposeAsync()
  53. {
  54. if (_firstEnumerator != null)
  55. {
  56. await _firstEnumerator.DisposeAsync().ConfigureAwait(false);
  57. _firstEnumerator = null;
  58. }
  59. _set = null;
  60. await base.DisposeAsync().ConfigureAwait(false);
  61. }
  62. protected override async ValueTask<bool> MoveNextCore(CancellationToken cancellationToken)
  63. {
  64. switch (state)
  65. {
  66. case AsyncIteratorState.Allocated:
  67. _firstEnumerator = _first.GetAsyncEnumerator(cancellationToken);
  68. _set = new Set<TSource>(_comparer);
  69. _setFilled = false;
  70. _fillSetTask = FillSetAsync(cancellationToken);
  71. state = AsyncIteratorState.Iterating;
  72. goto case AsyncIteratorState.Iterating;
  73. case AsyncIteratorState.Iterating:
  74. bool moveNext;
  75. do
  76. {
  77. if (!_setFilled)
  78. {
  79. // This is here so we don't need to call Task.WhenAll each time after the set is filled
  80. var moveNextTask = _firstEnumerator.MoveNextAsync();
  81. await Task.WhenAll(moveNextTask.AsTask(), _fillSetTask).ConfigureAwait(false);
  82. _setFilled = true;
  83. moveNext = await moveNextTask.ConfigureAwait(false);
  84. }
  85. else
  86. {
  87. moveNext = await _firstEnumerator.MoveNextAsync().ConfigureAwait(false);
  88. }
  89. if (moveNext)
  90. {
  91. var item = _firstEnumerator.Current;
  92. if (_set.Add(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. private async Task FillSetAsync(CancellationToken cancellationToken)
  105. {
  106. var array = await _second.ToArray(cancellationToken).ConfigureAwait(false);
  107. foreach (var t in array)
  108. {
  109. _set.Add(t);
  110. }
  111. }
  112. }
  113. }
  114. }