| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508 | // 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, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> 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 new DistinctAsyncIterator<TSource, TKey>(source, keySelector, comparer);        }        public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)        {            if (source == null)                throw new ArgumentNullException(nameof(source));            if (keySelector == null)                throw new ArgumentNullException(nameof(keySelector));            return source.Distinct(keySelector, EqualityComparer<TKey>.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);        }        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> DistinctUntilChanged<TSource>(this IAsyncEnumerable<TSource> source)        {            if (source == null)                throw new ArgumentNullException(nameof(source));            return source.DistinctUntilChanged(EqualityComparer<TSource>.Default);        }        public static IAsyncEnumerable<TSource> DistinctUntilChanged<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 DistinctUntilChangedAsyncIterator<TSource>(source, comparer);        }        public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)        {            if (source == null)                throw new ArgumentNullException(nameof(source));            if (keySelector == null)                throw new ArgumentNullException(nameof(keySelector));            return source.DistinctUntilChanged_(keySelector, EqualityComparer<TKey>.Default);        }        public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> 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 source.DistinctUntilChanged_(keySelector, comparer);        }        private static IAsyncEnumerable<TSource> DistinctUntilChanged_<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)        {            return new DistinctUntilChangedAsyncIterator<TSource, TKey>(source, keySelector, comparer);        }        private sealed class DistinctAsyncIterator<TSource, TKey> : AsyncIterator<TSource>, IIListProvider<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);                Debug.Assert(comparer != null);                this.source = source;                this.keySelector = keySelector;                this.comparer = comparer;            }            public async Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken)            {                var s = await FillSet(cancellationToken)                            .ConfigureAwait(false);                return s.ToArray();            }            public async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)            {                var s = await FillSet(cancellationToken)                            .ConfigureAwait(false);                return s;            }            public async Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)            {                if (onlyIfCheap)                {                    return -1;                }                var count = 0;                var s = new Set<TKey>(comparer);                var enu = source.GetAsyncEnumerator();                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<TSource> Clone()            {                return new DistinctAsyncIterator<TSource, TKey>(source, keySelector, 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<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>> FillSet(CancellationToken cancellationToken)            {                var s = new Set<TKey>(comparer);                var r = new List<TSource>();                var enu = source.GetAsyncEnumerator();                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 DistinctAsyncIterator<TSource> : AsyncIterator<TSource>, IIListProvider<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 FillSet(cancellationToken)                            .ConfigureAwait(false);                return s.ToArray();            }            public async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)            {                var s = await FillSet(cancellationToken)                            .ConfigureAwait(false);                return s.ToList();            }            public async Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)            {                return onlyIfCheap ? -1 : (await FillSet(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>> FillSet(CancellationToken cancellationToken)            {                var s = new Set<TSource>(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;            }        }        private sealed class DistinctUntilChangedAsyncIterator<TSource> : AsyncIterator<TSource>        {            private readonly IEqualityComparer<TSource> comparer;            private readonly IAsyncEnumerable<TSource> source;            private TSource currentValue;            private IAsyncEnumerator<TSource> enumerator;            private bool hasCurrentValue;            public DistinctUntilChangedAsyncIterator(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)            {                Debug.Assert(comparer != null);                Debug.Assert(source != null);                this.source = source;                this.comparer = comparer;            }            public override AsyncIterator<TSource> Clone()            {                return new DistinctUntilChangedAsyncIterator<TSource>(source, comparer);            }            public override async Task DisposeAsync()            {                if (enumerator != null)                {                    await enumerator.DisposeAsync().ConfigureAwait(false);                    enumerator = null;                    currentValue = default(TSource);                }                await base.DisposeAsync().ConfigureAwait(false);            }            protected override async Task<bool> MoveNextCore()            {                switch (state)                {                    case AsyncIteratorState.Allocated:                        enumerator = source.GetAsyncEnumerator();                        state = AsyncIteratorState.Iterating;                        goto case AsyncIteratorState.Iterating;                    case AsyncIteratorState.Iterating:                        while (await enumerator.MoveNextAsync()                                            .ConfigureAwait(false))                        {                            var item = enumerator.Current;                            var comparerEquals = false;                            if (hasCurrentValue)                            {                                comparerEquals = comparer.Equals(currentValue, item);                            }                            if (!hasCurrentValue || !comparerEquals)                            {                                hasCurrentValue = true;                                currentValue = item;                                current = item;                                return true;                            }                        }                        break;                }                await DisposeAsync().ConfigureAwait(false);                return false;            }        }        private sealed class DistinctUntilChangedAsyncIterator<TSource, TKey> : AsyncIterator<TSource>        {            private readonly IEqualityComparer<TKey> comparer;            private readonly Func<TSource, TKey> keySelector;            private readonly IAsyncEnumerable<TSource> source;            private TKey currentKeyValue;            private IAsyncEnumerator<TSource> enumerator;            private bool hasCurrentKey;            public DistinctUntilChangedAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)            {                this.source = source;                this.keySelector = keySelector;                this.comparer = comparer;            }            public override AsyncIterator<TSource> Clone()            {                return new DistinctUntilChangedAsyncIterator<TSource, TKey>(source, keySelector, comparer);            }            public override async Task DisposeAsync()            {                if (enumerator != null)                {                    await enumerator.DisposeAsync().ConfigureAwait(false);                    enumerator = null;                    currentKeyValue = default(TKey);                }                await base.DisposeAsync().ConfigureAwait(false);            }            protected override async Task<bool> MoveNextCore()            {                switch (state)                {                    case AsyncIteratorState.Allocated:                        enumerator = source.GetAsyncEnumerator();                        state = AsyncIteratorState.Iterating;                        goto case AsyncIteratorState.Iterating;                    case AsyncIteratorState.Iterating:                        while (await enumerator.MoveNextAsync()                                               .ConfigureAwait(false))                        {                            var item = enumerator.Current;                            var key = keySelector(item);                            var comparerEquals = false;                            if (hasCurrentKey)                            {                                comparerEquals = comparer.Equals(currentKeyValue, key);                            }                            if (!hasCurrentKey || !comparerEquals)                            {                                hasCurrentKey = true;                                currentKeyValue = key;                                current = item;                                return true;                            }                        }                        break; // case                }                await DisposeAsync().ConfigureAwait(false);                return false;            }        }    }}
 |