Except.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  12. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.except?view=net-9.0-pp
  13. //
  14. // These two overloads are replaced by the single method above, which takes a comparer, but supplies a default
  15. // value of null.
  16. /// <summary>
  17. /// Produces the set difference of two async-enumerable sequences by using the default equality comparer to compare values.
  18. /// </summary>
  19. /// <typeparam name="TSource">The type of the elements of the input sequences.</typeparam>
  20. /// <param name="first">An async-enumerable sequence whose elements that are not also in second will be returned.</param>
  21. /// <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>
  22. /// <returns>A sequence that contains the set difference of the elements of two sequences.</returns>
  23. /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> is null</exception>
  24. public static IAsyncEnumerable<TSource> Except<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second) =>
  25. Except(first, second, comparer: null);
  26. /// <summary>
  27. /// Produces the set difference of two async-enumerable sequences by using the specified equality comparer to compare values.
  28. /// </summary>
  29. /// <typeparam name="TSource">The type of the elements of the input sequences.</typeparam>
  30. /// <param name="first">An async-enumerable sequence whose elements that are not also in second will be returned.</param>
  31. /// <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>
  32. /// <param name="comparer">An equality comparer to compare values.</param>
  33. /// <returns>A sequence that contains the set difference of the elements of two sequences.</returns>
  34. /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> is null.</exception>
  35. public static IAsyncEnumerable<TSource> Except<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource>? comparer)
  36. {
  37. if (first == null)
  38. throw Error.ArgumentNull(nameof(first));
  39. if (second == null)
  40. throw Error.ArgumentNull(nameof(second));
  41. return Core(first, second, comparer);
  42. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource>? comparer, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  43. {
  44. var set = new Set<TSource>(comparer);
  45. await foreach (var element in second.WithCancellation(cancellationToken).ConfigureAwait(false))
  46. {
  47. set.Add(element);
  48. }
  49. await foreach (var element in first.WithCancellation(cancellationToken).ConfigureAwait(false))
  50. {
  51. if (set.Add(element))
  52. {
  53. yield return element;
  54. }
  55. }
  56. }
  57. }
  58. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  59. }
  60. }