1
0

Intersect.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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> Intersect<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  14. {
  15. if (first == null)
  16. throw new ArgumentNullException(nameof(first));
  17. if (second == null)
  18. throw new ArgumentNullException(nameof(second));
  19. if (comparer == null)
  20. throw new ArgumentNullException(nameof(comparer));
  21. return new IntersectAsyncIterator<TSource>(first, second, comparer);
  22. }
  23. public static IAsyncEnumerable<TSource> Intersect<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second)
  24. {
  25. if (first == null)
  26. throw new ArgumentNullException(nameof(first));
  27. if (second == null)
  28. throw new ArgumentNullException(nameof(second));
  29. return first.Intersect(second, EqualityComparer<TSource>.Default);
  30. }
  31. private sealed class IntersectAsyncIterator<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 IntersectAsyncIterator(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 IntersectAsyncIterator<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. do
  74. {
  75. if (!setFilled)
  76. {
  77. // This is here so we don't need to call Task.WhenAll each time after the set is filled
  78. var moveNextTask = firstEnumerator.MoveNext(cancellationToken);
  79. await Task.WhenAll(moveNextTask, fillSetTask)
  80. .ConfigureAwait(false);
  81. setFilled = true;
  82. moveNext = moveNextTask.Result;
  83. }
  84. else
  85. {
  86. moveNext = await firstEnumerator.MoveNext(cancellationToken)
  87. .ConfigureAwait(false);
  88. }
  89. if (moveNext)
  90. {
  91. var item = firstEnumerator.Current;
  92. if (set.Remove(item))
  93. {
  94. current = item;
  95. return true;
  96. }
  97. }
  98. } while (moveNext);
  99. Dispose();
  100. break;
  101. }
  102. return false;
  103. }
  104. private async Task FillSet(CancellationToken cancellationToken)
  105. {
  106. var array = await second.ToArray(cancellationToken)
  107. .ConfigureAwait(false);
  108. for (var i = 0; i < array.Length; i++)
  109. {
  110. set.Add(array[i]);
  111. }
  112. }
  113. }
  114. }
  115. }