Except.cs 4.3 KB

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