Intersect.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 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. /// <summary>
  12. /// Produces the set intersection of two async-enumerable sequences by using the default equality comparer to compare values.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements of the input sequences.</typeparam>
  15. /// <param name="first">An async-enumerable sequence whose distinct elements that also appear in second will be returned.</param>
  16. /// <param name="second">An async-enumerable sequence whose distinct elements that also appear in the first sequence will be returned.</param>
  17. /// <returns>A sequence that contains the elements that form the set intersection of two sequences.</returns>
  18. /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> is null.</exception>
  19. public static IAsyncEnumerable<TSource> Intersect<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second) =>
  20. Intersect(first, second, comparer: null);
  21. /// <summary>
  22. /// Produces the set intersection of two async-enumerable sequences by using the specified equality comparer to compare values.
  23. /// </summary>
  24. /// <typeparam name="TSource">The type of the elements of the input sequences.</typeparam>
  25. /// <param name="first">An async-enumerable sequence whose distinct elements that also appear in second will be returned.</param>
  26. /// <param name="second">An async-enumerable sequence whose distinct elements that also appear in the first sequence will be returned.</param>
  27. /// <param name="comparer">An equality comparer to compare values.</param>
  28. /// <returns>A sequence that contains the elements that form the set intersection of two sequences.</returns>
  29. /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> is null.</exception>
  30. public static IAsyncEnumerable<TSource> Intersect<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource>? comparer)
  31. {
  32. if (first == null)
  33. throw Error.ArgumentNull(nameof(first));
  34. if (second == null)
  35. throw Error.ArgumentNull(nameof(second));
  36. return Create(Core);
  37. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  38. {
  39. var set = new Set<TSource>(comparer);
  40. await foreach (var element in second.WithCancellation(cancellationToken).ConfigureAwait(false))
  41. {
  42. set.Add(element);
  43. }
  44. await foreach (var element in first.WithCancellation(cancellationToken).ConfigureAwait(false))
  45. {
  46. if (set.Remove(element))
  47. {
  48. yield return element;
  49. }
  50. }
  51. }
  52. }
  53. }
  54. }