SequenceEqual.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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("first");
  17. if (second == null)
  18. throw new ArgumentNullException("second");
  19. if (comparer == null)
  20. throw new ArgumentNullException("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("first");
  27. if (second == null)
  28. throw new ArgumentNullException("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. using (var e1 = first.GetEnumerator())
  53. using (var e2 = second.GetEnumerator())
  54. {
  55. while (await e1.MoveNext(cancellationToken)
  56. .ConfigureAwait(false))
  57. {
  58. if (!(await e2.MoveNext(cancellationToken)
  59. .ConfigureAwait(false) && comparer.Equals(e1.Current, e2.Current)))
  60. {
  61. return false;
  62. }
  63. }
  64. return !await e2.MoveNext(cancellationToken)
  65. .ConfigureAwait(false);
  66. }
  67. }
  68. }
  69. }