Intersect.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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.intersect?view=net-9.0-pp
  13. // The method above covers the next two overloads because it supplies a default null value for comparer.
  14. /// <summary>
  15. /// Produces the set intersection of two async-enumerable sequences by using the default equality comparer to compare values.
  16. /// </summary>
  17. /// <typeparam name="TSource">The type of the elements of the input sequences.</typeparam>
  18. /// <param name="first">An async-enumerable sequence whose distinct elements that also appear in second will be returned.</param>
  19. /// <param name="second">An async-enumerable sequence whose distinct elements that also appear in the first sequence will be returned.</param>
  20. /// <returns>A sequence that contains the elements that form the set intersection of two sequences.</returns>
  21. /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> is null.</exception>
  22. public static IAsyncEnumerable<TSource> Intersect<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second) =>
  23. Intersect(first, second, comparer: null);
  24. /// <summary>
  25. /// Produces the set intersection of two async-enumerable sequences by using the specified equality comparer to compare values.
  26. /// </summary>
  27. /// <typeparam name="TSource">The type of the elements of the input sequences.</typeparam>
  28. /// <param name="first">An async-enumerable sequence whose distinct elements that also appear in second will be returned.</param>
  29. /// <param name="second">An async-enumerable sequence whose distinct elements that also appear in the first sequence will be returned.</param>
  30. /// <param name="comparer">An equality comparer to compare values.</param>
  31. /// <returns>A sequence that contains the elements that form the set intersection of two sequences.</returns>
  32. /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> is null.</exception>
  33. public static IAsyncEnumerable<TSource> Intersect<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource>? comparer)
  34. {
  35. if (first == null)
  36. throw Error.ArgumentNull(nameof(first));
  37. if (second == null)
  38. throw Error.ArgumentNull(nameof(second));
  39. return Core(first, second, comparer);
  40. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource>? comparer, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  41. {
  42. var set = new Set<TSource>(comparer);
  43. await foreach (var element in second.WithCancellation(cancellationToken).ConfigureAwait(false))
  44. {
  45. set.Add(element);
  46. }
  47. await foreach (var element in first.WithCancellation(cancellationToken).ConfigureAwait(false))
  48. {
  49. if (set.Remove(element))
  50. {
  51. yield return element;
  52. }
  53. }
  54. }
  55. }
  56. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  57. }
  58. }