12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT License.
- // See the LICENSE file in the project root for more information.
- using System.Collections.Generic;
- using System.Threading;
- using System.Threading.Tasks;
- namespace System.Linq
- {
- public static partial class AsyncEnumerable
- {
- #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
- // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.except?view=net-9.0-pp
- //
- // These two overloads are replaced by the single method above, which takes a comparer, but supplies a default
- // value of null.
- /// <summary>
- /// Produces the set difference of two async-enumerable sequences by using the default equality comparer to compare values.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements of the input sequences.</typeparam>
- /// <param name="first">An async-enumerable sequence whose elements that are not also in second will be returned.</param>
- /// <param name="second">An async-enumerable sequence whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence.</param>
- /// <returns>A sequence that contains the set difference of the elements of two sequences.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> is null</exception>
- public static IAsyncEnumerable<TSource> Except<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second) =>
- Except(first, second, comparer: null);
- /// <summary>
- /// Produces the set difference of two async-enumerable sequences by using the specified equality comparer to compare values.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements of the input sequences.</typeparam>
- /// <param name="first">An async-enumerable sequence whose elements that are not also in second will be returned.</param>
- /// <param name="second">An async-enumerable sequence whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence.</param>
- /// <param name="comparer">An equality comparer to compare values.</param>
- /// <returns>A sequence that contains the set difference of the elements of two sequences.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> is null.</exception>
- public static IAsyncEnumerable<TSource> Except<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource>? comparer)
- {
- if (first == null)
- throw Error.ArgumentNull(nameof(first));
- if (second == null)
- throw Error.ArgumentNull(nameof(second));
- return Core(first, second, comparer);
- static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource>? comparer, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
- {
- 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.Add(element))
- {
- yield return element;
- }
- }
- }
- }
- #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
- }
- }
|