Browse Source

Use async iterators for Intersect.

Bart De Smet 6 years ago
parent
commit
41efe2944d
1 changed files with 24 additions and 0 deletions
  1. 24 0
      Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Intersect.cs

+ 24 - 0
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Intersect.cs

@@ -20,9 +20,32 @@ namespace System.Linq
             if (second == null)
                 throw Error.ArgumentNull(nameof(second));
 
+#if CSHARP8 && USE_ASYNC_ITERATOR && ASYNC_ITERATOR_CAN_RETURN_AETOR
+            return Create(Core);
+
+            async IAsyncEnumerable<TSource> Core(CancellationToken cancellationToken)
+            {
+                var set = new Set<TSource>(comparer);
+
+                await foreach (var element in second.WithCancellation(cancellationToken).ConfigureAwait(false))
+                {
+                    set.Add(element);
+                }
+
+                await foreach (var element in first.WithCancellation(cancellationToken).ConfigureAwait(false))
+                {
+                    if (set.Remove(element))
+                    {
+                        yield return element;
+                    }
+                }
+            }
+#else
             return new IntersectAsyncIterator<TSource>(first, second, comparer);
+#endif
         }
 
+#if !(CSHARP8 && USE_ASYNC_ITERATOR && ASYNC_ITERATOR_CAN_RETURN_AETOR)
         private sealed class IntersectAsyncIterator<TSource> : AsyncIterator<TSource>
         {
             private readonly IEqualityComparer<TSource> _comparer;
@@ -101,5 +124,6 @@ namespace System.Linq
                 return false;
             }
         }
+#endif
     }
 }