1
0

SequenceEqual.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. using (var e1 = first.GetEnumerator())
  60. using (var e2 = second.GetEnumerator())
  61. {
  62. while (await e1.MoveNext(cancellationToken)
  63. .ConfigureAwait(false))
  64. {
  65. if (!(await e2.MoveNext(cancellationToken)
  66. .ConfigureAwait(false) && comparer.Equals(e1.Current, e2.Current)))
  67. {
  68. return false;
  69. }
  70. }
  71. return !await e2.MoveNext(cancellationToken)
  72. .ConfigureAwait(false);
  73. }
  74. }
  75. }
  76. }