Intersect.cs 3.9 KB

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