SequenceEqual.cs 4.9 KB

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