-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
219 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (c) David Karnok & Contributors. | ||
// Licensed under the Apache 2.0 License. | ||
// See LICENSE file in the project root for full license information. | ||
|
||
using Xunit; | ||
using async_enumerable_dotnet; | ||
using System.Collections.Generic; | ||
|
||
namespace async_enumerable_dotnet_test | ||
{ | ||
public class DistinctUntilChangedTest | ||
{ | ||
[Fact] | ||
public async void Empty() | ||
{ | ||
await AsyncEnumerable.Empty<int>() | ||
.DistinctUntilChanged() | ||
.AssertResult(); | ||
} | ||
|
||
[Fact] | ||
public async void Just() | ||
{ | ||
await AsyncEnumerable.Just(1) | ||
.DistinctUntilChanged() | ||
.AssertResult(1); | ||
} | ||
|
||
[Fact] | ||
public async void Range() | ||
{ | ||
await AsyncEnumerable.Range(1, 5) | ||
.DistinctUntilChanged() | ||
.AssertResult(1, 2, 3, 4, 5); | ||
} | ||
|
||
[Fact] | ||
public async void Redundant() | ||
{ | ||
await AsyncEnumerable.FromArray(1, 2, 3, 3, 2, 1, 1, 4, 5, 4, 4) | ||
.DistinctUntilChanged() | ||
.AssertResult(1, 2, 3, 2, 1, 4, 5, 4); | ||
} | ||
|
||
[Fact] | ||
public async void Redundant_Comparer() | ||
{ | ||
await AsyncEnumerable.FromArray(1, 2, 3, 3, 2, 1, 1, 4, 5, 4, 4) | ||
.DistinctUntilChanged(EqualityComparer<int>.Default) | ||
.AssertResult(1, 2, 3, 2, 1, 4, 5, 4); | ||
} | ||
|
||
[Fact] | ||
public async void KeySelector() | ||
{ | ||
await AsyncEnumerable.Range(1, 10) | ||
.DistinctUntilChanged(k => k / 3) | ||
.AssertResult(1, 3, 6, 9); | ||
} | ||
|
||
[Fact] | ||
public async void KeySelector_KeyComparer() | ||
{ | ||
await AsyncEnumerable.Range(1, 10) | ||
.DistinctUntilChanged(k => k / 3, EqualityComparer<long>.Default) | ||
.AssertResult(1, 3, 6, 9); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright (c) David Karnok & Contributors. | ||
// Licensed under the Apache 2.0 License. | ||
// See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
namespace async_enumerable_dotnet.impl | ||
{ | ||
internal sealed class DistinctUntilChanged<TSource, TKey> : IAsyncEnumerable<TSource> | ||
{ | ||
private readonly IAsyncEnumerable<TSource> _source; | ||
|
||
private readonly Func<TSource, TKey> _keySelector; | ||
|
||
private readonly IEqualityComparer<TKey> _keyComparer; | ||
|
||
public DistinctUntilChanged(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> keyComparer) | ||
{ | ||
_source = source; | ||
_keySelector = keySelector; | ||
_keyComparer = keyComparer; | ||
} | ||
|
||
public IAsyncEnumerator<TSource> GetAsyncEnumerator() | ||
{ | ||
return new DistinctUntilChangedEnumerator(_source.GetAsyncEnumerator(), _keySelector, _keyComparer); | ||
} | ||
|
||
private sealed class DistinctUntilChangedEnumerator : IAsyncEnumerator<TSource> | ||
{ | ||
private readonly IAsyncEnumerator<TSource> _source; | ||
|
||
private readonly Func<TSource, TKey> _keySelector; | ||
|
||
private readonly IEqualityComparer<TKey> _keyComparer; | ||
|
||
private TKey _prevKey; | ||
|
||
public TSource Current => _source.Current; | ||
|
||
private bool _once; | ||
|
||
public DistinctUntilChangedEnumerator(IAsyncEnumerator<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> keyComparer) | ||
{ | ||
_source = source; | ||
_keySelector = keySelector; | ||
_keyComparer = keyComparer; | ||
} | ||
|
||
public ValueTask DisposeAsync() | ||
{ | ||
_prevKey = default; | ||
return _source.DisposeAsync(); | ||
} | ||
|
||
public async ValueTask<bool> MoveNextAsync() | ||
{ | ||
for (; ;) | ||
{ | ||
if (await _source.MoveNextAsync()) | ||
{ | ||
var key = _keySelector(_source.Current); | ||
if (!_once) | ||
{ | ||
_once = true; | ||
_prevKey = key; | ||
return true; | ||
} | ||
if (!_keyComparer.Equals(_prevKey, key)) | ||
{ | ||
_prevKey = key; | ||
return true; | ||
} | ||
|
||
_prevKey = key; | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |