SequenceEqual.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 SequenceEqual_(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> SequenceEqual_<TSource>(IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer,
  48. CancellationToken cancellationToken)
  49. {
  50. if (first is ICollection<TSource> firstCol && second is ICollection<TSource> secondCol && firstCol.Count != secondCol.Count)
  51. {
  52. return false;
  53. }
  54. var e1 = first.GetAsyncEnumerator();
  55. try
  56. {
  57. var e2 = second.GetAsyncEnumerator();
  58. try
  59. {
  60. while (await e1.MoveNextAsync().ConfigureAwait(false))
  61. {
  62. if (!(await e2.MoveNextAsync().ConfigureAwait(false) && comparer.Equals(e1.Current, e2.Current)))
  63. {
  64. return false;
  65. }
  66. }
  67. return !await e2.MoveNextAsync().ConfigureAwait(false);
  68. }
  69. finally
  70. {
  71. await e2.DisposeAsync().ConfigureAwait(false);
  72. }
  73. }
  74. finally
  75. {
  76. await e1.DisposeAsync().ConfigureAwait(false);
  77. }
  78. }
  79. }
  80. }