Except.cs 5.0 KB

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