// 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 Error.ArgumentNull(nameof(source)); return DistinctUntilChangedCore(source, comparer: null); } public static IAsyncEnumerable DistinctUntilChanged(this IAsyncEnumerable source, IEqualityComparer comparer) { if (source == null) throw Error.ArgumentNull(nameof(source)); return DistinctUntilChangedCore(source, comparer); } public static IAsyncEnumerable DistinctUntilChanged(this IAsyncEnumerable source, Func keySelector) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (keySelector == null) throw Error.ArgumentNull(nameof(keySelector)); return DistinctUntilChangedCore(source, keySelector, comparer: null); } public static IAsyncEnumerable DistinctUntilChanged(this IAsyncEnumerable source, Func keySelector, IEqualityComparer comparer) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (keySelector == null) throw Error.ArgumentNull(nameof(keySelector)); return DistinctUntilChangedCore(source, keySelector, comparer); } public static IAsyncEnumerable DistinctUntilChanged(this IAsyncEnumerable source, Func> keySelector) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (keySelector == null) throw Error.ArgumentNull(nameof(keySelector)); return DistinctUntilChangedCore(source, keySelector, comparer: null); } public static IAsyncEnumerable DistinctUntilChanged(this IAsyncEnumerable source, Func> keySelector, IEqualityComparer comparer) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (keySelector == null) throw Error.ArgumentNull(nameof(keySelector)); if (comparer == null) throw Error.ArgumentNull(nameof(comparer)); return DistinctUntilChangedCore(source, keySelector, comparer); } private static IAsyncEnumerable DistinctUntilChangedCore(IAsyncEnumerable source, IEqualityComparer comparer) { return new DistinctUntilChangedAsyncIterator(source, comparer); } private static IAsyncEnumerable DistinctUntilChangedCore(IAsyncEnumerable source, Func keySelector, IEqualityComparer comparer) { return new DistinctUntilChangedAsyncIterator(source, keySelector, comparer); } private static IAsyncEnumerable DistinctUntilChangedCore(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(source != null); _source = source; _comparer = comparer ?? EqualityComparer.Default; } public override AsyncIterator Clone() { return new DistinctUntilChangedAsyncIterator(_source, _comparer); } public override async ValueTask DisposeAsync() { if (_enumerator != null) { await _enumerator.DisposeAsync().ConfigureAwait(false); _enumerator = null; _currentValue = default; } await base.DisposeAsync().ConfigureAwait(false); } protected override async ValueTask MoveNextCore() { switch (state) { case AsyncIteratorState.Allocated: _enumerator = _source.GetAsyncEnumerator(cancellationToken); 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) { _source = source; _keySelector = keySelector; _comparer = comparer ?? EqualityComparer.Default; } public override AsyncIterator Clone() { return new DistinctUntilChangedAsyncIterator(_source, _keySelector, _comparer); } public override async ValueTask DisposeAsync() { if (_enumerator != null) { await _enumerator.DisposeAsync().ConfigureAwait(false); _enumerator = null; _currentKeyValue = default; } await base.DisposeAsync().ConfigureAwait(false); } protected override async ValueTask MoveNextCore() { switch (state) { case AsyncIteratorState.Allocated: _enumerator = _source.GetAsyncEnumerator(cancellationToken); 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) { _source = source; _keySelector = keySelector; _comparer = comparer ?? EqualityComparer.Default; } public override AsyncIterator Clone() { return new DistinctUntilChangedAsyncIteratorWithTask(_source, _keySelector, _comparer); } public override async ValueTask DisposeAsync() { if (_enumerator != null) { await _enumerator.DisposeAsync().ConfigureAwait(false); _enumerator = null; _currentKeyValue = default; } await base.DisposeAsync().ConfigureAwait(false); } protected override async ValueTask MoveNextCore() { switch (state) { case AsyncIteratorState.Allocated: _enumerator = _source.GetAsyncEnumerator(cancellationToken); 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; } } } }