SequenceEqual.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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> SequenceEqualAsync<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, CancellationToken cancellationToken = default)
  12. {
  13. if (first == null)
  14. throw Error.ArgumentNull(nameof(first));
  15. if (second == null)
  16. throw Error.ArgumentNull(nameof(second));
  17. return SequenceEqualCore(first, second, comparer: null, cancellationToken);
  18. }
  19. public static Task<bool> SequenceEqualAsync<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken = default)
  20. {
  21. if (first == null)
  22. throw Error.ArgumentNull(nameof(first));
  23. if (second == null)
  24. throw Error.ArgumentNull(nameof(second));
  25. return SequenceEqualCore(first, second, comparer, cancellationToken);
  26. }
  27. private static Task<bool> SequenceEqualCore<TSource>(IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
  28. {
  29. if (comparer == null)
  30. {
  31. comparer = EqualityComparer<TSource>.Default;
  32. }
  33. if (first is ICollection<TSource> firstCol && second is ICollection<TSource> secondCol)
  34. {
  35. if (firstCol.Count != secondCol.Count)
  36. {
  37. return Task.FromResult(false);
  38. }
  39. if (firstCol is IList<TSource> firstList && secondCol is IList<TSource> secondList)
  40. {
  41. int count = firstCol.Count;
  42. for (int i = 0; i < count; i++)
  43. {
  44. if (!comparer.Equals(firstList[i], secondList[i]))
  45. {
  46. return Task.FromResult(false);
  47. }
  48. }
  49. return Task.FromResult(true);
  50. }
  51. }
  52. return Core(first, second, comparer, cancellationToken);
  53. async Task<bool> Core(IAsyncEnumerable<TSource> _first, IAsyncEnumerable<TSource> _second, IEqualityComparer<TSource> _comparer, CancellationToken _cancellationToken)
  54. {
  55. var e1 = _first.GetAsyncEnumerator(_cancellationToken);
  56. try
  57. {
  58. var e2 = _second.GetAsyncEnumerator(_cancellationToken);
  59. try
  60. {
  61. while (await e1.MoveNextAsync().ConfigureAwait(false))
  62. {
  63. if (!(await e2.MoveNextAsync().ConfigureAwait(false) && _comparer.Equals(e1.Current, e2.Current)))
  64. {
  65. return false;
  66. }
  67. }
  68. return !await e2.MoveNextAsync().ConfigureAwait(false);
  69. }
  70. finally
  71. {
  72. await e2.DisposeAsync().ConfigureAwait(false);
  73. }
  74. }
  75. finally
  76. {
  77. await e1.DisposeAsync().ConfigureAwait(false);
  78. }
  79. }
  80. }
  81. }
  82. }