SequenceEqual.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 SequenceEqual_(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> SequenceEqual_<TSource>(IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer,
  68. CancellationToken cancellationToken)
  69. {
  70. if (first is ICollection<TSource> firstCol)
  71. {
  72. if (second is ICollection<TSource> secondCol && firstCol.Count != secondCol.Count)
  73. {
  74. return false;
  75. }
  76. }
  77. using (var e1 = first.GetEnumerator())
  78. using (var e2 = second.GetEnumerator())
  79. {
  80. while (await e1.MoveNext(cancellationToken)
  81. .ConfigureAwait(false))
  82. {
  83. if (!(await e2.MoveNext(cancellationToken)
  84. .ConfigureAwait(false) && comparer.Equals(e1.Current, e2.Current)))
  85. {
  86. return false;
  87. }
  88. }
  89. return !await e2.MoveNext(cancellationToken)
  90. .ConfigureAwait(false);
  91. }
  92. }
  93. }
  94. }