SequenceEqual.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. var firstCol = first as ICollection<TSource>;
  53. if (firstCol != null)
  54. {
  55. var secondCol = second as ICollection<TSource>;
  56. if (secondCol != null && firstCol.Count != secondCol.Count)
  57. {
  58. return false;
  59. }
  60. }
  61. using (var e1 = first.GetEnumerator())
  62. using (var e2 = second.GetEnumerator())
  63. {
  64. while (await e1.MoveNext(cancellationToken)
  65. .ConfigureAwait(false))
  66. {
  67. if (!(await e2.MoveNext(cancellationToken)
  68. .ConfigureAwait(false) && comparer.Equals(e1.Current, e2.Current)))
  69. {
  70. return false;
  71. }
  72. }
  73. return !await e2.MoveNext(cancellationToken)
  74. .ConfigureAwait(false);
  75. }
  76. }
  77. }
  78. }