SequenceEqual.cs 4.0 KB

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