// 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 DistinctUntilChanged(this IAsyncEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); return source.DistinctUntilChanged(EqualityComparer.Default); } public static IAsyncEnumerable DistinctUntilChanged(this IAsyncEnumerable source, IEqualityComparer comparer) { if (source == null) throw new ArgumentNullException(nameof(source)); if (comparer == null) throw new ArgumentNullException(nameof(comparer)); return new DistinctUntilChangedAsyncIterator(source, comparer); } public static IAsyncEnumerable DistinctUntilChanged(this IAsyncEnumerable source, Func keySelector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (keySelector == null) throw new ArgumentNullException(nameof(keySelector)); return source.DistinctUntilChanged_(keySelector, EqualityComparer.Default); } public static IAsyncEnumerable DistinctUntilChanged(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 source.DistinctUntilChanged_(keySelector, comparer); } public static IAsyncEnumerable DistinctUntilChanged(this IAsyncEnumerable source, Func> keySelector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (keySelector == null) throw new ArgumentNullException(nameof(keySelector)); return source.DistinctUntilChanged_(keySelector, EqualityComparer.Default); } public static IAsyncEnumerable DistinctUntilChanged(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 source.DistinctUntilChanged_(keySelector, comparer); } private static IAsyncEnumerable DistinctUntilChanged_(this IAsyncEnumerable source, Func keySelector, IEqualityComparer comparer) { return new DistinctUntilChangedAsyncIterator(source, keySelector, comparer); } private static IAsyncEnumerable DistinctUntilChanged_(this IAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer) { return new DistinctUntilChangedAsyncIteratorWithTask(source, keySelector, comparer); } private sealed class DistinctUntilChangedAsyncIterator : AsyncIterator { private readonly IEqualityComparer comparer; private readonly IAsyncEnumerable source; private TSource currentValue; private IAsyncEnumerator enumerator; private bool hasCurrentValue; public DistinctUntilChangedAsyncIterator(IAsyncEnumerable source, IEqualityComparer comparer) { Debug.Assert(comparer != null); Debug.Assert(source != null); this.source = source; this.comparer = comparer; } public override AsyncIterator Clone() { return new DistinctUntilChangedAsyncIterator(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 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 : AsyncIterator { private readonly IEqualityComparer comparer; private readonly Func keySelector; private readonly IAsyncEnumerable source; private TKey currentKeyValue; private IAsyncEnumerator enumerator; private bool hasCurrentKey; public DistinctUntilChangedAsyncIterator(IAsyncEnumerable source, Func keySelector, IEqualityComparer comparer) { this.source = source; this.keySelector = keySelector; this.comparer = comparer; } public override AsyncIterator Clone() { return new DistinctUntilChangedAsyncIterator(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 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; } } private sealed class DistinctUntilChangedAsyncIteratorWithTask : AsyncIterator { private readonly IEqualityComparer comparer; private readonly Func> keySelector; private readonly IAsyncEnumerable source; private TKey currentKeyValue; private IAsyncEnumerator enumerator; private bool hasCurrentKey; public DistinctUntilChangedAsyncIteratorWithTask(IAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer) { this.source = source; this.keySelector = keySelector; this.comparer = comparer; } public override AsyncIterator Clone() { return new DistinctUntilChangedAsyncIteratorWithTask(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 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 = await keySelector(item).ConfigureAwait(false); 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; } } } }