SequenceEqual.cs 3.6 KB

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