// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information. using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace System.Linq { public static partial class AsyncEnumerable { public static Task SequenceEqual(this IAsyncEnumerable first, IAsyncEnumerable second, IEqualityComparer comparer) { if (first == null) { throw new ArgumentNullException(nameof(first)); } if (second == null) { throw new ArgumentNullException(nameof(second)); } if (comparer == null) { throw new ArgumentNullException(nameof(comparer)); } return SequenceEqual(first, second, comparer, CancellationToken.None); } public static Task SequenceEqual(this IAsyncEnumerable first, IAsyncEnumerable second) { if (first == null) { throw new ArgumentNullException(nameof(first)); } if (second == null) { throw new ArgumentNullException(nameof(second)); } return SequenceEqual(first, second, CancellationToken.None); } public static Task SequenceEqual(this IAsyncEnumerable first, IAsyncEnumerable second, IEqualityComparer comparer, CancellationToken cancellationToken) { if (first == null) { throw new ArgumentNullException(nameof(first)); } if (second == null) { throw new ArgumentNullException(nameof(second)); } if (comparer == null) { throw new ArgumentNullException(nameof(comparer)); } return SequenceEqualCore(first, second, comparer, cancellationToken); } public static Task SequenceEqual(this IAsyncEnumerable first, IAsyncEnumerable second, CancellationToken cancellationToken) { if (first == null) { throw new ArgumentNullException(nameof(first)); } if (second == null) { throw new ArgumentNullException(nameof(second)); } return first.SequenceEqual(second, EqualityComparer.Default, cancellationToken); } private static async Task SequenceEqualCore(IAsyncEnumerable first, IAsyncEnumerable second, IEqualityComparer comparer, CancellationToken cancellationToken) { if (first is ICollection firstCol && second is ICollection secondCol && firstCol.Count != secondCol.Count) { return false; } var e1 = first.GetAsyncEnumerator(cancellationToken); try { var e2 = second.GetAsyncEnumerator(cancellationToken); try { while (await e1.MoveNextAsync().ConfigureAwait(false)) { if (!(await e2.MoveNextAsync().ConfigureAwait(false) && comparer.Equals(e1.Current, e2.Current))) { return false; } } return !await e2.MoveNextAsync().ConfigureAwait(false); } finally { await e2.DisposeAsync().ConfigureAwait(false); } } finally { await e1.DisposeAsync().ConfigureAwait(false); } } } }