Except.cs 3.9 KB

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