SequenceEqual.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. public static Task<bool> SequenceEqualAsync<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second)
  12. {
  13. if (first == null)
  14. throw Error.ArgumentNull(nameof(first));
  15. if (second == null)
  16. throw Error.ArgumentNull(nameof(second));
  17. return SequenceEqualCore(first, second, comparer: null, CancellationToken.None);
  18. }
  19. public static Task<bool> SequenceEqualAsync<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, CancellationToken cancellationToken)
  20. {
  21. if (first == null)
  22. throw Error.ArgumentNull(nameof(first));
  23. if (second == null)
  24. throw Error.ArgumentNull(nameof(second));
  25. return SequenceEqualCore(first, second, comparer: null, cancellationToken);
  26. }
  27. public static Task<bool> SequenceEqualAsync<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  28. {
  29. if (first == null)
  30. throw Error.ArgumentNull(nameof(first));
  31. if (second == null)
  32. throw Error.ArgumentNull(nameof(second));
  33. return SequenceEqualCore(first, second, comparer, CancellationToken.None);
  34. }
  35. public static Task<bool> SequenceEqualAsync<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
  36. {
  37. if (first == null)
  38. throw Error.ArgumentNull(nameof(first));
  39. if (second == null)
  40. throw Error.ArgumentNull(nameof(second));
  41. return SequenceEqualCore(first, second, comparer, cancellationToken);
  42. }
  43. private static Task<bool> SequenceEqualCore<TSource>(IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
  44. {
  45. if (comparer == null)
  46. {
  47. comparer = EqualityComparer<TSource>.Default;
  48. }
  49. if (first is ICollection<TSource> firstCol && second is ICollection<TSource> secondCol)
  50. {
  51. if (firstCol.Count != secondCol.Count)
  52. {
  53. return Task.FromResult(false);
  54. }
  55. if (firstCol is IList<TSource> firstList && secondCol is IList<TSource> secondList)
  56. {
  57. int count = firstCol.Count;
  58. for (int i = 0; i < count; i++)
  59. {
  60. if (!comparer.Equals(firstList[i], secondList[i]))
  61. {
  62. return Task.FromResult(false);
  63. }
  64. }
  65. return Task.FromResult(true);
  66. }
  67. }
  68. return Core();
  69. async Task<bool> Core()
  70. {
  71. var e1 = first.GetAsyncEnumerator(cancellationToken);
  72. try
  73. {
  74. var e2 = second.GetAsyncEnumerator(cancellationToken);
  75. try
  76. {
  77. while (await e1.MoveNextAsync().ConfigureAwait(false))
  78. {
  79. if (!(await e2.MoveNextAsync().ConfigureAwait(false) && comparer.Equals(e1.Current, e2.Current)))
  80. {
  81. return false;
  82. }
  83. }
  84. return !await e2.MoveNextAsync().ConfigureAwait(false);
  85. }
  86. finally
  87. {
  88. await e2.DisposeAsync().ConfigureAwait(false);
  89. }
  90. }
  91. finally
  92. {
  93. await e1.DisposeAsync().ConfigureAwait(false);
  94. }
  95. }
  96. }
  97. }
  98. }