123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- // 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<TSource> Distinct<TSource>(this IAsyncEnumerable<TSource> source)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- return source.Distinct(EqualityComparer<TSource>.Default);
- }
- public static IAsyncEnumerable<TSource> Distinct<TSource>(this IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (comparer == null)
- throw new ArgumentNullException(nameof(comparer));
- return new DistinctAsyncIterator<TSource>(source, comparer);
- }
- private sealed class DistinctAsyncIterator<TSource> : AsyncIterator<TSource>, IAsyncIListProvider<TSource>
- {
- private readonly IEqualityComparer<TSource> comparer;
- private readonly IAsyncEnumerable<TSource> source;
- private IAsyncEnumerator<TSource> enumerator;
- private Set<TSource> set;
- public DistinctAsyncIterator(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
- {
- Debug.Assert(source != null);
- this.source = source;
- this.comparer = comparer;
- }
- public async Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
- {
- var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
- return s.ToArray();
- }
- public async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)
- {
- var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
- return s.ToList();
- }
- public async Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
- {
- return onlyIfCheap ? -1 : (await FillSetAsync(cancellationToken).ConfigureAwait(false)).Count;
- }
- public override AsyncIterator<TSource> Clone()
- {
- return new DistinctAsyncIterator<TSource>(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<bool> 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<TSource>(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<Set<TSource>> FillSetAsync(CancellationToken cancellationToken)
- {
- var s = new Set<TSource>(comparer);
- await s.UnionWithAsync(source);
- return s;
- }
- }
- }
- }
|