Except.cs 4.1 KB

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