// 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 AsyncEnumerableEx { public static IAsyncEnumerable Distinct(this IAsyncEnumerable source, Func keySelector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (keySelector == null) throw new ArgumentNullException(nameof(keySelector)); return DistinctCore(source, keySelector, EqualityComparer.Default); } public static IAsyncEnumerable Distinct(this IAsyncEnumerable source, Func keySelector, IEqualityComparer comparer) { if (source == null) throw new ArgumentNullException(nameof(source)); if (keySelector == null) throw new ArgumentNullException(nameof(keySelector)); if (comparer == null) throw new ArgumentNullException(nameof(comparer)); return DistinctCore(source, keySelector, comparer); } public static IAsyncEnumerable Distinct(this IAsyncEnumerable source, Func> keySelector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (keySelector == null) throw new ArgumentNullException(nameof(keySelector)); return DistinctCore(source, keySelector, EqualityComparer.Default); } public static IAsyncEnumerable Distinct(this IAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer) { if (source == null) throw new ArgumentNullException(nameof(source)); if (keySelector == null) throw new ArgumentNullException(nameof(keySelector)); if (comparer == null) throw new ArgumentNullException(nameof(comparer)); return DistinctCore(source, keySelector, comparer); } private static IAsyncEnumerable DistinctCore(IAsyncEnumerable source, Func keySelector, IEqualityComparer comparer) { return new DistinctAsyncIterator(source, keySelector, comparer); } private static IAsyncEnumerable DistinctCore(IAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer) { return new DistinctAsyncIteratorWithTask(source, keySelector, comparer); } private sealed class DistinctAsyncIterator : AsyncIterator, IAsyncIListProvider { private readonly IEqualityComparer comparer; private readonly Func keySelector; private readonly IAsyncEnumerable source; private IAsyncEnumerator enumerator; private Set set; public DistinctAsyncIterator(IAsyncEnumerable source, Func keySelector, IEqualityComparer comparer) { Debug.Assert(source != null); Debug.Assert(keySelector != null); Debug.Assert(comparer != null); this.source = source; this.keySelector = keySelector; 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; } public async Task GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken) { if (onlyIfCheap) { return -1; } var count = 0; var s = new Set(comparer); var enu = source.GetAsyncEnumerator(cancellationToken); try { while (await enu.MoveNextAsync().ConfigureAwait(false)) { var item = enu.Current; if (s.Add(keySelector(item))) { count++; } } } finally { await enu.DisposeAsync().ConfigureAwait(false); } return count; } public override AsyncIterator Clone() { return new DistinctAsyncIterator(source, keySelector, comparer); } public override async ValueTask DisposeAsync() { if (enumerator != null) { await enumerator.DisposeAsync().ConfigureAwait(false); enumerator = null; set = null; } await base.DisposeAsync().ConfigureAwait(false); } protected override async ValueTask MoveNextCore(CancellationToken cancellationToken) { switch (state) { case AsyncIteratorState.Allocated: enumerator = source.GetAsyncEnumerator(cancellationToken); if (!await enumerator.MoveNextAsync().ConfigureAwait(false)) { await DisposeAsync().ConfigureAwait(false); return false; } var element = enumerator.Current; set = new Set(comparer); set.Add(keySelector(element)); current = element; state = AsyncIteratorState.Iterating; return true; case AsyncIteratorState.Iterating: while (await enumerator.MoveNextAsync().ConfigureAwait(false)) { element = enumerator.Current; if (set.Add(keySelector(element))) { current = element; return true; } } break; } await DisposeAsync().ConfigureAwait(false); return false; } private async Task> FillSetAsync(CancellationToken cancellationToken) { var s = new Set(comparer); var r = new List(); var enu = source.GetAsyncEnumerator(cancellationToken); try { while (await enu.MoveNextAsync(cancellationToken).ConfigureAwait(false)) { var item = enu.Current; if (s.Add(keySelector(item))) { r.Add(item); } } } finally { await enu.DisposeAsync().ConfigureAwait(false); } return r; } } private sealed class DistinctAsyncIteratorWithTask : AsyncIterator, IAsyncIListProvider { private readonly IEqualityComparer comparer; private readonly Func> keySelector; private readonly IAsyncEnumerable source; private IAsyncEnumerator enumerator; private Set set; public DistinctAsyncIteratorWithTask(IAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer) { Debug.Assert(source != null); Debug.Assert(keySelector != null); Debug.Assert(comparer != null); this.source = source; this.keySelector = keySelector; 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; } public async Task GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken) { if (onlyIfCheap) { return -1; } var count = 0; var s = new Set(comparer); var enu = source.GetAsyncEnumerator(cancellationToken); try { while (await enu.MoveNextAsync().ConfigureAwait(false)) { var item = enu.Current; if (s.Add(await keySelector(item).ConfigureAwait(false))) { count++; } } } finally { await enu.DisposeAsync().ConfigureAwait(false); } return count; } public override AsyncIterator Clone() { return new DistinctAsyncIteratorWithTask(source, keySelector, comparer); } public override async ValueTask DisposeAsync() { if (enumerator != null) { await enumerator.DisposeAsync().ConfigureAwait(false); enumerator = null; set = null; } await base.DisposeAsync().ConfigureAwait(false); } protected override async ValueTask MoveNextCore(CancellationToken cancellationToken) { switch (state) { case AsyncIteratorState.Allocated: enumerator = source.GetAsyncEnumerator(cancellationToken); if (!await enumerator.MoveNextAsync().ConfigureAwait(false)) { await DisposeAsync().ConfigureAwait(false); return false; } var element = enumerator.Current; set = new Set(comparer); set.Add(await keySelector(element).ConfigureAwait(false)); current = element; state = AsyncIteratorState.Iterating; return true; case AsyncIteratorState.Iterating: while (await enumerator.MoveNextAsync().ConfigureAwait(false)) { element = enumerator.Current; if (set.Add(await keySelector(element).ConfigureAwait(false))) { current = element; return true; } } break; } await DisposeAsync().ConfigureAwait(false); return false; } private async Task> FillSetAsync(CancellationToken cancellationToken) { var s = new Set(comparer); var r = new List(); var enu = source.GetAsyncEnumerator(cancellationToken); try { while (await enu.MoveNextAsync(cancellationToken).ConfigureAwait(false)) { var item = enu.Current; if (s.Add(await keySelector(item).ConfigureAwait(false))) { r.Add(item); } } } finally { await enu.DisposeAsync().ConfigureAwait(false); } return r; } } } }