-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add sync `Duplicates` operator * Add async `Duplicates` operator * Simplify `HasDuplicates` operator * Simplify `HasDuplicates` tests * Update front-page readme * Add linqpad example and link example
- Loading branch information
1 parent
27e083f
commit a4ff6ab
Showing
19 changed files
with
365 additions
and
411 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
Docs/SuperLinq.Docs/apidoc/SuperLinq.SuperEnumerable.Duplicates.md
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,6 @@ | ||
--- | ||
uid: SuperLinq.SuperEnumerable.Duplicates``1(System.Collections.Generic.IEnumerable{``0},System.Collections.Generic.IEqualityComparer{``0}) | ||
example: [*content] | ||
--- | ||
The following code example demonstrates how to get the duplicated elements of a sequence using `Duplicates`. | ||
[!code-csharp[](SuperLinq/Duplicates/Duplicates.linq#L6-)] |
17 changes: 17 additions & 0 deletions
17
Docs/SuperLinq.Docs/apidoc/SuperLinq/Duplicates/Duplicates.linq
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,17 @@ | ||
<Query Kind="Statements"> | ||
<NuGetReference>SuperLinq</NuGetReference> | ||
<Namespace>SuperLinq</Namespace> | ||
</Query> | ||
|
||
var sequence = new[] { "foo", "bar", "baz", "foo", }; | ||
|
||
// determine if a sequence has duplicate items | ||
var result = sequence.Duplicates(); | ||
|
||
Console.WriteLine( | ||
"[" + | ||
string.Join(", ", result) + | ||
"]"); | ||
|
||
// This code produces the following output: | ||
// [foo] |
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,55 @@ | ||
namespace SuperLinq.Async; | ||
|
||
public static partial class AsyncSuperEnumerable | ||
{ | ||
/// <summary> | ||
/// Returns all duplicate elements of the given source. | ||
/// </summary> | ||
/// <typeparam name="TSource"> | ||
/// The type of the elements in the source sequence. | ||
/// </typeparam> | ||
/// <param name="source"> | ||
/// The source sequence. | ||
/// </param> | ||
/// <param name="comparer"> | ||
/// The equality comparer to use to determine whether one <typeparamref name="TSource"/> equals another. If <see | ||
/// langword="null"/>, the default equality comparer for <typeparamref name="TSource"/> is used. | ||
/// </param> | ||
/// <returns> | ||
/// All elements that are duplicated. | ||
/// </returns> | ||
/// <exception cref="ArgumentNullException"> | ||
/// <paramref name="source"/> is <see langword="null"/>. | ||
/// </exception> | ||
/// <remarks> | ||
/// This operator uses deferred execution and streams its results. | ||
/// </remarks> | ||
public static IAsyncEnumerable<TSource> Duplicates<TSource>(this IAsyncEnumerable<TSource> source, IEqualityComparer<TSource>? comparer = null) | ||
{ | ||
ArgumentNullException.ThrowIfNull(source); | ||
|
||
comparer ??= EqualityComparer<TSource>.Default; | ||
|
||
return Core(source, comparer); | ||
|
||
static async IAsyncEnumerable<TSource> Core( | ||
IAsyncEnumerable<TSource> source, | ||
IEqualityComparer<TSource> comparer, | ||
[EnumeratorCancellation] CancellationToken token = default) | ||
{ | ||
var counts = new Collections.NullKeyDictionary<TSource, int>(comparer); | ||
await foreach (var element in source.WithCancellation(token).ConfigureAwait(false)) | ||
{ | ||
if (!counts.TryGetValue(element, out var count)) | ||
{ | ||
counts[element] = 1; | ||
} | ||
else if (count == 1) | ||
{ | ||
yield return element; | ||
counts[element] = 2; | ||
} | ||
} | ||
} | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
Source/SuperLinq.Async/PublicAPI/net6.0/PublicAPI.Unshipped.txt
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
1 change: 1 addition & 0 deletions
1
Source/SuperLinq.Async/PublicAPI/net7.0/PublicAPI.Unshipped.txt
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
1 change: 1 addition & 0 deletions
1
Source/SuperLinq.Async/PublicAPI/net8.0/PublicAPI.Unshipped.txt
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
1 change: 1 addition & 0 deletions
1
Source/SuperLinq.Async/PublicAPI/netcoreapp3.1/PublicAPI.Unshipped.txt
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,52 @@ | ||
namespace SuperLinq; | ||
|
||
public static partial class SuperEnumerable | ||
{ | ||
/// <summary> | ||
/// Returns all duplicate elements of the given source. | ||
/// </summary> | ||
/// <typeparam name="TSource"> | ||
/// The type of the elements in the source sequence. | ||
/// </typeparam> | ||
/// <param name="source"> | ||
/// The source sequence. | ||
/// </param> | ||
/// <param name="comparer"> | ||
/// The equality comparer to use to determine whether one <typeparamref name="TSource"/> equals another. If <see | ||
/// langword="null"/>, the default equality comparer for <typeparamref name="TSource"/> is used. | ||
/// </param> | ||
/// <returns> | ||
/// All elements that are duplicated. | ||
/// </returns> | ||
/// <exception cref="ArgumentNullException"> | ||
/// <paramref name="source"/> is <see langword="null"/>. | ||
/// </exception> | ||
/// <remarks> | ||
/// This operator uses deferred execution and streams its results. | ||
/// </remarks> | ||
public static IEnumerable<TSource> Duplicates<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource>? comparer = null) | ||
{ | ||
ArgumentNullException.ThrowIfNull(source); | ||
|
||
comparer ??= EqualityComparer<TSource>.Default; | ||
|
||
return Core(source, comparer); | ||
|
||
static IEnumerable<TSource> Core(IEnumerable<TSource> source, IEqualityComparer<TSource> comparer) | ||
{ | ||
var counts = new Collections.NullKeyDictionary<TSource, int>(comparer); | ||
foreach (var element in source) | ||
{ | ||
if (!counts.TryGetValue(element, out var count)) | ||
{ | ||
counts[element] = 1; | ||
} | ||
else if (count == 1) | ||
{ | ||
yield return element; | ||
counts[element] = 2; | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.