Except.cs 4.9 KB

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