SequenceEqual.cs 3.9 KB

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