Except.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 new ArgumentNullException(nameof(first));
  16. if (second == null)
  17. throw new ArgumentNullException(nameof(second));
  18. return first.Except(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 new ArgumentNullException(nameof(first));
  24. if (second == null)
  25. throw new ArgumentNullException(nameof(second));
  26. if (comparer == null)
  27. throw new ArgumentNullException(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. this.first = first;
  45. this.second = second;
  46. this.comparer = comparer;
  47. }
  48. public override AsyncIterator<TSource> Clone()
  49. {
  50. return new ExceptAsyncIterator<TSource>(first, second, comparer);
  51. }
  52. public override void Dispose()
  53. {
  54. if (firstEnumerator != null)
  55. {
  56. firstEnumerator.Dispose();
  57. firstEnumerator = null;
  58. }
  59. set = null;
  60. base.Dispose();
  61. }
  62. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  63. {
  64. switch (state)
  65. {
  66. case AsyncIteratorState.Allocated:
  67. firstEnumerator = first.GetEnumerator();
  68. set = new Set<TSource>(comparer);
  69. setFilled = false;
  70. fillSetTask = FillSet(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.MoveNext(cancellationToken);
  81. await Task.WhenAll(moveNextTask, fillSetTask)
  82. .ConfigureAwait(false);
  83. setFilled = true;
  84. moveNext = moveNextTask.Result;
  85. }
  86. else
  87. {
  88. moveNext = await firstEnumerator.MoveNext(cancellationToken)
  89. .ConfigureAwait(false);
  90. }
  91. if (moveNext)
  92. {
  93. var item = firstEnumerator.Current;
  94. if (set.Add(item))
  95. {
  96. current = item;
  97. return true;
  98. }
  99. }
  100. } while (moveNext);
  101. Dispose();
  102. break;
  103. }
  104. return false;
  105. }
  106. private async Task FillSet(CancellationToken cancellationToken)
  107. {
  108. var array = await second.ToArray(cancellationToken)
  109. .ConfigureAwait(false);
  110. foreach (var t in array)
  111. {
  112. set.Add(t);
  113. }
  114. }
  115. }
  116. }
  117. }