SequenceEqual.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.sequenceequalasync?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. /// Determines whether two sequences are equal by comparing the elements pairwise.
  16. /// </summary>
  17. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  18. /// <param name="first">First async-enumerable sequence to compare.</param>
  19. /// <param name="second">Second async-enumerable sequence to compare.</param>
  20. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  21. /// <returns>An async-enumerable sequence that contains a single element which indicates whether both sequences are of equal length and their corresponding elements are equal according to the default equality comparer for their type.</returns>
  22. /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> is null.</exception>
  23. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  24. public static ValueTask<bool> SequenceEqualAsync<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, CancellationToken cancellationToken = default) =>
  25. SequenceEqualAsync(first, second, comparer: null, cancellationToken);
  26. /// <summary>
  27. /// Determines whether two sequences are equal by comparing the elements pairwise using a specified equality comparer.
  28. /// </summary>
  29. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  30. /// <param name="first">First async-enumerable sequence to compare.</param>
  31. /// <param name="second">Second async-enumerable sequence to compare.</param>
  32. /// <param name="comparer">Comparer used to compare elements of both sequences.</param>
  33. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  34. /// <returns>An async-enumerable sequence that contains a single element which indicates whether both sequences are of equal length and their corresponding elements are equal according to the specified equality comparer.</returns>
  35. /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> or <paramref name="comparer"/> is null.</exception>
  36. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  37. public static ValueTask<bool> SequenceEqualAsync<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource>? comparer, CancellationToken cancellationToken = default)
  38. {
  39. if (first == null)
  40. throw Error.ArgumentNull(nameof(first));
  41. if (second == null)
  42. throw Error.ArgumentNull(nameof(second));
  43. comparer ??= EqualityComparer<TSource>.Default;
  44. if (first is ICollection<TSource> firstCol && second is ICollection<TSource> secondCol)
  45. {
  46. if (firstCol.Count != secondCol.Count)
  47. {
  48. return new ValueTask<bool>(false);
  49. }
  50. if (firstCol is IList<TSource> firstList && secondCol is IList<TSource> secondList)
  51. {
  52. var count = firstCol.Count;
  53. for (var i = 0; i < count; i++)
  54. {
  55. if (!comparer.Equals(firstList[i], secondList[i]))
  56. {
  57. return new ValueTask<bool>(false);
  58. }
  59. }
  60. return new ValueTask<bool>(true);
  61. }
  62. }
  63. return Core(first, second, comparer, cancellationToken);
  64. static async ValueTask<bool> Core(IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
  65. {
  66. await using var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false);
  67. await using var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false);
  68. while (await e1.MoveNextAsync())
  69. {
  70. if (!(await e2.MoveNextAsync() && comparer.Equals(e1.Current, e2.Current)))
  71. {
  72. return false;
  73. }
  74. }
  75. return !await e2.MoveNextAsync();
  76. }
  77. }
  78. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  79. }
  80. }