1
0

Except.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. {
  16. throw new ArgumentNullException(nameof(first));
  17. }
  18. if (second == null)
  19. {
  20. throw new ArgumentNullException(nameof(second));
  21. }
  22. return first.Except(second, EqualityComparer<TSource>.Default);
  23. }
  24. public static IAsyncEnumerable<TSource> Except<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  25. {
  26. if (first == null)
  27. {
  28. throw new ArgumentNullException(nameof(first));
  29. }
  30. if (second == null)
  31. {
  32. throw new ArgumentNullException(nameof(second));
  33. }
  34. if (comparer == null)
  35. {
  36. throw new ArgumentNullException(nameof(comparer));
  37. }
  38. return new ExceptAsyncIterator<TSource>(first, second, comparer);
  39. }
  40. private sealed class ExceptAsyncIterator<TSource> : AsyncIterator<TSource>
  41. {
  42. private readonly IEqualityComparer<TSource> comparer;
  43. private readonly IAsyncEnumerable<TSource> first;
  44. private readonly IAsyncEnumerable<TSource> second;
  45. private Task fillSetTask;
  46. private IAsyncEnumerator<TSource> firstEnumerator;
  47. private Set<TSource> set;
  48. private bool setFilled;
  49. public ExceptAsyncIterator(IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  50. {
  51. Debug.Assert(first != null);
  52. Debug.Assert(second != null);
  53. Debug.Assert(comparer != null);
  54. this.first = first;
  55. this.second = second;
  56. this.comparer = comparer;
  57. }
  58. public override AsyncIterator<TSource> Clone()
  59. {
  60. return new ExceptAsyncIterator<TSource>(first, second, comparer);
  61. }
  62. public override void Dispose()
  63. {
  64. if (firstEnumerator != null)
  65. {
  66. firstEnumerator.Dispose();
  67. firstEnumerator = null;
  68. }
  69. set = null;
  70. base.Dispose();
  71. }
  72. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  73. {
  74. switch (state)
  75. {
  76. case AsyncIteratorState.Allocated:
  77. firstEnumerator = first.GetEnumerator();
  78. set = new Set<TSource>(comparer);
  79. setFilled = false;
  80. fillSetTask = FillSet(cancellationToken);
  81. state = AsyncIteratorState.Iterating;
  82. goto case AsyncIteratorState.Iterating;
  83. case AsyncIteratorState.Iterating:
  84. bool moveNext;
  85. do
  86. {
  87. if (!setFilled)
  88. {
  89. // This is here so we don't need to call Task.WhenAll each time after the set is filled
  90. var moveNextTask = firstEnumerator.MoveNext(cancellationToken);
  91. await Task.WhenAll(moveNextTask, fillSetTask)
  92. .ConfigureAwait(false);
  93. setFilled = true;
  94. moveNext = moveNextTask.Result;
  95. }
  96. else
  97. {
  98. moveNext = await firstEnumerator.MoveNext(cancellationToken)
  99. .ConfigureAwait(false);
  100. }
  101. if (moveNext)
  102. {
  103. var item = firstEnumerator.Current;
  104. if (set.Add(item))
  105. {
  106. current = item;
  107. return true;
  108. }
  109. }
  110. } while (moveNext);
  111. Dispose();
  112. break;
  113. }
  114. return false;
  115. }
  116. private async Task FillSet(CancellationToken cancellationToken)
  117. {
  118. var array = await second.ToArray(cancellationToken)
  119. .ConfigureAwait(false);
  120. foreach (var t in array)
  121. {
  122. set.Add(t);
  123. }
  124. }
  125. }
  126. }
  127. }