-
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
5 changed files
with
235 additions
and
0 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
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 DistinctTest | ||
{ | ||
[Fact] | ||
public async void Empty() | ||
{ | ||
await AsyncEnumerable.Empty<int>() | ||
.Distinct() | ||
.AssertResult(); | ||
} | ||
|
||
[Fact] | ||
public async void Normal() | ||
{ | ||
await AsyncEnumerable.Range(1, 5) | ||
.Distinct() | ||
.AssertResult(1, 2, 3, 4, 5); | ||
} | ||
|
||
[Fact] | ||
public async void Redundant() | ||
{ | ||
await AsyncEnumerable.FromArray(1, 2, 3, 2, 1, 4, 5, 1, 5) | ||
.Distinct() | ||
.AssertResult(1, 2, 3, 4, 5); | ||
} | ||
|
||
[Fact] | ||
public async void KeySelector() | ||
{ | ||
await AsyncEnumerable.Range(1, 5) | ||
.Distinct(v => v % 3) | ||
.AssertResult(1, 2, 3); | ||
} | ||
|
||
[Fact] | ||
public async void EqualityComparer() | ||
{ | ||
await AsyncEnumerable.Range(1, 5) | ||
.Distinct(EqualityComparer<int>.Default) | ||
.AssertResult(1, 2, 3, 4, 5); | ||
} | ||
|
||
[Fact] | ||
public async void KeySelector_EqualityComparer() | ||
{ | ||
await AsyncEnumerable.Range(1, 5) | ||
.Distinct(v => v % 3, EqualityComparer<long>.Default) | ||
.AssertResult(1, 2, 3); | ||
} | ||
|
||
[Fact] | ||
public async void Custom_Set() | ||
{ | ||
await AsyncEnumerable.Range(1, 5) | ||
.Distinct(v => (v % 3), () => new HashSet<long>()) | ||
.AssertResult(1, 2, 3); | ||
} | ||
} | ||
} |
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,83 @@ | ||
// 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 Distinct<TSource, TKey> : IAsyncEnumerable<TSource> | ||
{ | ||
private readonly IAsyncEnumerable<TSource> _source; | ||
|
||
private readonly Func<TSource, TKey> _keySelector; | ||
|
||
private readonly Func<ISet<TKey>> _collectionSupplier; | ||
|
||
public Distinct(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<ISet<TKey>> collectionSupplier) | ||
{ | ||
_source = source; | ||
_keySelector = keySelector; | ||
_collectionSupplier = collectionSupplier; | ||
} | ||
|
||
public IAsyncEnumerator<TSource> GetAsyncEnumerator() | ||
{ | ||
ISet<TKey> collection; | ||
|
||
try | ||
{ | ||
collection = _collectionSupplier(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return new Error<TSource>.ErrorEnumerator(ex); | ||
} | ||
return new DistinctEnumerator(_source.GetAsyncEnumerator(), _keySelector, collection); | ||
} | ||
|
||
private sealed class DistinctEnumerator : IAsyncEnumerator<TSource> | ||
{ | ||
private readonly IAsyncEnumerator<TSource> _source; | ||
|
||
private readonly Func<TSource, TKey> _keySelector; | ||
|
||
private ISet<TKey> _collection; | ||
|
||
public TSource Current => _source.Current; | ||
|
||
public DistinctEnumerator(IAsyncEnumerator<TSource> source, Func<TSource, TKey> keySelector, ISet<TKey> collection) | ||
{ | ||
_source = source; | ||
_keySelector = keySelector; | ||
_collection = collection; | ||
} | ||
|
||
public ValueTask DisposeAsync() | ||
{ | ||
_collection = default; | ||
return _source.DisposeAsync(); | ||
} | ||
|
||
public async ValueTask<bool> MoveNextAsync() | ||
{ | ||
for (; ; ) | ||
{ | ||
if (await _source.MoveNextAsync()) | ||
{ | ||
if (_collection.Add(_keySelector(_source.Current))) | ||
{ | ||
return true; | ||
} | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |