// 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.Diagnostics; using System.Threading; using System.Threading.Tasks; namespace System.Linq { public static partial class AsyncEnumerable { public static IAsyncEnumerable Distinct(this IAsyncEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); return source.Distinct(EqualityComparer.Default); } public static IAsyncEnumerable Distinct(this IAsyncEnumerable source, IEqualityComparer comparer) { if (source == null) throw new ArgumentNullException(nameof(source)); if (comparer == null) throw new ArgumentNullException(nameof(comparer)); return new DistinctAsyncIterator(source, comparer); } private sealed class DistinctAsyncIterator : AsyncIterator, IIListProvider { private readonly IEqualityComparer comparer; private readonly IAsyncEnumerable source; private IAsyncEnumerator enumerator; private Set set; public DistinctAsyncIterator(IAsyncEnumerable source, IEqualityComparer comparer) { Debug.Assert(source != null); this.source = source; this.comparer = comparer; } public async Task ToArrayAsync(CancellationToken cancellationToken) { var s = await FillSetAsync(cancellationToken).ConfigureAwait(false); return s.ToArray(); } public async Task> ToListAsync(CancellationToken cancellationToken) { var s = await FillSetAsync(cancellationToken).ConfigureAwait(false); return s.ToList(); } public async Task GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken) { return onlyIfCheap ? -1 : (await FillSetAsync(cancellationToken).ConfigureAwait(false)).Count; } public override AsyncIterator Clone() { return new DistinctAsyncIterator(source, comparer); } public override async Task DisposeAsync() { if (enumerator != null) { await enumerator.DisposeAsync().ConfigureAwait(false); enumerator = null; set = null; } await base.DisposeAsync().ConfigureAwait(false); } protected override async Task MoveNextCore() { switch (state) { case AsyncIteratorState.Allocated: enumerator = source.GetAsyncEnumerator(); if (!await enumerator.MoveNextAsync().ConfigureAwait(false)) { await DisposeAsync().ConfigureAwait(false); return false; } var element = enumerator.Current; set = new Set(comparer); set.Add(element); current = element; state = AsyncIteratorState.Iterating; return true; case AsyncIteratorState.Iterating: while (await enumerator.MoveNextAsync().ConfigureAwait(false)) { element = enumerator.Current; if (set.Add(element)) { current = element; return true; } } break; } await DisposeAsync().ConfigureAwait(false); return false; } private async Task> FillSetAsync(CancellationToken cancellationToken) { var s = new Set(comparer); var enu = source.GetAsyncEnumerator(); try { while (await enu.MoveNextAsync(cancellationToken).ConfigureAwait(false)) { s.Add(enu.Current); } } finally { await enu.DisposeAsync().ConfigureAwait(false); } return s; } } } }