123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541 |
- // 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<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (keySelector == null)
- throw Error.ArgumentNull(nameof(keySelector));
- return DistinctCore(source, keySelector, comparer: null);
- }
- public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (keySelector == null)
- throw Error.ArgumentNull(nameof(keySelector));
- return DistinctCore(source, keySelector, comparer);
- }
- public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (keySelector == null)
- throw Error.ArgumentNull(nameof(keySelector));
- return DistinctCore<TSource, TKey>(source, keySelector, comparer: null);
- }
- #if !NO_DEEP_CANCELLATION
- public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (keySelector == null)
- throw Error.ArgumentNull(nameof(keySelector));
- return DistinctCore<TSource, TKey>(source, keySelector, comparer: null);
- }
- #endif
- public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (keySelector == null)
- throw Error.ArgumentNull(nameof(keySelector));
- return DistinctCore(source, keySelector, comparer);
- }
- #if !NO_DEEP_CANCELLATION
- public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (keySelector == null)
- throw Error.ArgumentNull(nameof(keySelector));
- return DistinctCore(source, keySelector, comparer);
- }
- #endif
- private static IAsyncEnumerable<TSource> DistinctCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
- {
- return new DistinctAsyncIterator<TSource, TKey>(source, keySelector, comparer);
- }
- private static IAsyncEnumerable<TSource> DistinctCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer)
- {
- return new DistinctAsyncIteratorWithTask<TSource, TKey>(source, keySelector, comparer);
- }
- #if !NO_DEEP_CANCELLATION
- private static IAsyncEnumerable<TSource> DistinctCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer)
- {
- return new DistinctAsyncIteratorWithTaskAndCancellation<TSource, TKey>(source, keySelector, comparer);
- }
- #endif
- private sealed class DistinctAsyncIterator<TSource, TKey> : AsyncIterator<TSource>, IAsyncIListProvider<TSource>
- {
- private readonly IEqualityComparer<TKey>? _comparer;
- private readonly Func<TSource, TKey> _keySelector;
- private readonly IAsyncEnumerable<TSource> _source;
- private IAsyncEnumerator<TSource>? _enumerator;
- private Set<TKey>? _set;
- public DistinctAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
- {
- Debug.Assert(source != null);
- Debug.Assert(keySelector != null);
- _source = source;
- _keySelector = keySelector;
- _comparer = comparer;
- }
- public async ValueTask<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
- {
- var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
- return s.ToArray();
- }
- public async ValueTask<List<TSource>> ToListAsync(CancellationToken cancellationToken)
- {
- var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
- return s;
- }
- public async ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
- {
- if (onlyIfCheap)
- {
- return -1;
- }
- var count = 0;
- var s = new Set<TKey>(_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 AsyncIteratorBase<TSource> Clone()
- {
- return new DistinctAsyncIterator<TSource, TKey>(_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<bool> MoveNextCore()
- {
- 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<TKey>(_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<List<TSource>> FillSetAsync(CancellationToken cancellationToken)
- {
- var s = new Set<TKey>(_comparer);
- var r = new List<TSource>();
- var enu = _source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (await enu.MoveNextAsync().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<TSource, TKey> : AsyncIterator<TSource>, IAsyncIListProvider<TSource>
- {
- private readonly IEqualityComparer<TKey>? _comparer;
- private readonly Func<TSource, ValueTask<TKey>> _keySelector;
- private readonly IAsyncEnumerable<TSource> _source;
- private IAsyncEnumerator<TSource>? _enumerator;
- private Set<TKey>? _set;
- public DistinctAsyncIteratorWithTask(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer)
- {
- Debug.Assert(source != null);
- Debug.Assert(keySelector != null);
- _source = source;
- _keySelector = keySelector;
- _comparer = comparer;
- }
- public async ValueTask<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
- {
- var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
- return s.ToArray();
- }
- public async ValueTask<List<TSource>> ToListAsync(CancellationToken cancellationToken)
- {
- var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
- return s;
- }
- public async ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
- {
- if (onlyIfCheap)
- {
- return -1;
- }
- var count = 0;
- var s = new Set<TKey>(_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 AsyncIteratorBase<TSource> Clone()
- {
- return new DistinctAsyncIteratorWithTask<TSource, TKey>(_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<bool> MoveNextCore()
- {
- 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<TKey>(_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 ValueTask<List<TSource>> FillSetAsync(CancellationToken cancellationToken)
- {
- var s = new Set<TKey>(_comparer);
- var r = new List<TSource>();
- var enu = _source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (await enu.MoveNextAsync().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;
- }
- }
- #if !NO_DEEP_CANCELLATION
- private sealed class DistinctAsyncIteratorWithTaskAndCancellation<TSource, TKey> : AsyncIterator<TSource>, IAsyncIListProvider<TSource>
- {
- private readonly IEqualityComparer<TKey>? _comparer;
- private readonly Func<TSource, CancellationToken, ValueTask<TKey>> _keySelector;
- private readonly IAsyncEnumerable<TSource> _source;
- private IAsyncEnumerator<TSource>? _enumerator;
- private Set<TKey>? _set;
- public DistinctAsyncIteratorWithTaskAndCancellation(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IEqualityComparer<TKey>? comparer)
- {
- Debug.Assert(source != null);
- Debug.Assert(keySelector != null);
- _source = source;
- _keySelector = keySelector;
- _comparer = comparer;
- }
- public async ValueTask<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
- {
- var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
- return s.ToArray();
- }
- public async ValueTask<List<TSource>> ToListAsync(CancellationToken cancellationToken)
- {
- var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
- return s;
- }
- public async ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
- {
- if (onlyIfCheap)
- {
- return -1;
- }
- var count = 0;
- var s = new Set<TKey>(_comparer);
- var enu = _source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (await enu.MoveNextAsync().ConfigureAwait(false))
- {
- var item = enu.Current;
- if (s.Add(await _keySelector(item, cancellationToken).ConfigureAwait(false)))
- {
- count++;
- }
- }
- }
- finally
- {
- await enu.DisposeAsync().ConfigureAwait(false);
- }
- return count;
- }
- public override AsyncIteratorBase<TSource> Clone()
- {
- return new DistinctAsyncIteratorWithTaskAndCancellation<TSource, TKey>(_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<bool> MoveNextCore()
- {
- 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<TKey>(_comparer);
- _set.Add(await _keySelector(element, _cancellationToken).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, _cancellationToken).ConfigureAwait(false)))
- {
- _current = element;
- return true;
- }
- }
- break;
- }
- await DisposeAsync().ConfigureAwait(false);
- return false;
- }
- private async ValueTask<List<TSource>> FillSetAsync(CancellationToken cancellationToken)
- {
- var s = new Set<TKey>(_comparer);
- var r = new List<TSource>();
- var enu = _source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (await enu.MoveNextAsync().ConfigureAwait(false))
- {
- var item = enu.Current;
- if (s.Add(await _keySelector(item, cancellationToken).ConfigureAwait(false)))
- {
- r.Add(item);
- }
- }
- }
- finally
- {
- await enu.DisposeAsync().ConfigureAwait(false);
- }
- return r;
- }
- }
- #endif
- }
- }
|