Except.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 IAsyncEnumerator<TSource> _firstEnumerator;
  36. private Set<TSource> _set;
  37. public ExceptAsyncIterator(IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  38. {
  39. Debug.Assert(first != null);
  40. Debug.Assert(second != null);
  41. Debug.Assert(comparer != null);
  42. _first = first;
  43. _second = second;
  44. _comparer = comparer;
  45. }
  46. public override AsyncIterator<TSource> Clone()
  47. {
  48. return new ExceptAsyncIterator<TSource>(_first, _second, _comparer);
  49. }
  50. public override async ValueTask DisposeAsync()
  51. {
  52. if (_firstEnumerator != null)
  53. {
  54. await _firstEnumerator.DisposeAsync().ConfigureAwait(false);
  55. _firstEnumerator = null;
  56. }
  57. _set = null;
  58. await base.DisposeAsync().ConfigureAwait(false);
  59. }
  60. protected override async ValueTask<bool> MoveNextCore(CancellationToken cancellationToken)
  61. {
  62. // NB: Earlier implementations of this operator constructed the set for the second source concurrently
  63. // with the first MoveNextAsync call on the first source. This resulted in an unexpected source of
  64. // concurrency, which isn't a great default behavior because it's very hard to suppress or control
  65. // this behavior.
  66. switch (state)
  67. {
  68. case AsyncIteratorState.Allocated:
  69. _set = await AsyncEnumerableHelpers.ToSet(_second, _comparer, cancellationToken).ConfigureAwait(false);
  70. _firstEnumerator = _first.GetAsyncEnumerator(cancellationToken);
  71. state = AsyncIteratorState.Iterating;
  72. goto case AsyncIteratorState.Iterating;
  73. case AsyncIteratorState.Iterating:
  74. bool moveNext;
  75. do
  76. {
  77. moveNext = await _firstEnumerator.MoveNextAsync().ConfigureAwait(false);
  78. if (moveNext)
  79. {
  80. var item = _firstEnumerator.Current;
  81. if (_set.Add(item))
  82. {
  83. current = item;
  84. return true;
  85. }
  86. }
  87. } while (moveNext);
  88. await DisposeAsync().ConfigureAwait(false);
  89. break;
  90. }
  91. return false;
  92. }
  93. }
  94. }
  95. }