Except.cs 3.6 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. /// <summary>
  12. /// Produces the set difference 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 elements that are not also in second will be returned.</param>
  16. /// <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>
  17. /// <returns>A sequence that contains the set difference of the elements of two sequences.</returns>
  18. /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> is null</exception>
  19. public static IAsyncEnumerable<TSource> Except<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second) =>
  20. Except(first, second, comparer: null);
  21. /// <summary>
  22. /// Produces the set difference 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 elements that are not also in second will be returned.</param>
  26. /// <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>
  27. /// <param name="comparer">An equality comparer to compare values.</param>
  28. /// <returns>A sequence that contains the set difference of the elements of two sequences.</returns>
  29. /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> is null.</exception>
  30. public static IAsyncEnumerable<TSource> Except<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. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  37. return Core(first, second, comparer);
  38. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource>? comparer, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  39. #else
  40. return Create(Core);
  41. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  42. #endif
  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. }
  59. }