-
Notifications
You must be signed in to change notification settings - Fork 5k
Add LINQ Shuffle #112173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add LINQ Shuffle #112173
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8508fe9
Add LINQ Shuffle
stephentoub e445b55
Merge branch 'main' into addshuffle
stephentoub a284332
Remove bespoke Shuffler
stephentoub 8d8c34a
Remove check for ShuffleIterator
stephentoub 5bec834
Optimize Shuffle().Take() with reservoir sampling
stephentoub af70be1
Merge branch 'main' into addshuffle
stephentoub c876731
Improve impl and tests
stephentoub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
59 changes: 59 additions & 0 deletions
59
src/libraries/System.Linq.AsyncEnumerable/src/System/Linq/Shuffle.cs
This file contains hidden or 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,59 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading; | ||
|
||
namespace System.Linq | ||
{ | ||
public static partial class AsyncEnumerable | ||
{ | ||
#if !NET | ||
[ThreadStatic] | ||
private static Random? t_random; | ||
#endif | ||
|
||
/// <summary>Shuffles the order of the elements of a sequence.</summary> | ||
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> | ||
/// <param name="source">A sequence of values to shuffle.</param> | ||
/// <returns>A sequence whose elements correspond to those of the input sequence in randomized order.</returns> | ||
/// <remarks>Randomization is performed using a non-cryptographically-secure random number generator.</remarks> | ||
public static IAsyncEnumerable<TSource> Shuffle<TSource>( | ||
this IAsyncEnumerable<TSource> source) | ||
{ | ||
ThrowHelper.ThrowIfNull(source); | ||
|
||
return Impl(source, default); | ||
|
||
static async IAsyncEnumerable<TSource> Impl( | ||
IAsyncEnumerable<TSource> source, | ||
[EnumeratorCancellation] CancellationToken cancellationToken) | ||
{ | ||
TSource[] array = await source.ToArrayAsync(cancellationToken).ConfigureAwait(false); | ||
|
||
#if NET | ||
Random.Shared.Shuffle(array); | ||
#else | ||
Random random = t_random ??= new Random(Environment.TickCount ^ Environment.CurrentManagedThreadId); | ||
int n = array.Length; | ||
for (int i = 0; i < n - 1; i++) | ||
{ | ||
int j = random.Next(i, n); | ||
if (j != i) | ||
{ | ||
TSource temp = array[i]; | ||
array[i] = array[j]; | ||
array[j] = temp; | ||
} | ||
} | ||
#endif | ||
|
||
for (int i = 0; i < array.Length; i++) | ||
{ | ||
yield return array[i]; | ||
} | ||
} | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/libraries/System.Linq.AsyncEnumerable/tests/ShuffleTests.cs
This file contains hidden or 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 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace System.Linq.Tests | ||
{ | ||
public class ShuffleTests : AsyncEnumerableTests | ||
{ | ||
[Fact] | ||
public void InvalidInputs_Throws() | ||
{ | ||
AssertExtensions.Throws<ArgumentNullException>("source", () => AsyncEnumerable.Shuffle<int>(null)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(new int[0])] | ||
[InlineData(new int[] { 1 })] | ||
[InlineData(new int[] { 2, 4, 8 })] | ||
[InlineData(new int[] { -1, 2, 5, 6, 7, 8 })] | ||
public async Task VariousValues_ContainsAllInputValues(int[] values) | ||
{ | ||
foreach (IAsyncEnumerable<int> source in CreateSources(values)) | ||
{ | ||
int[] shuffled = await source.Shuffle().ToArrayAsync(); | ||
Array.Sort(shuffled); | ||
Assert.Equal(values, shuffled); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task ToArrayAsync_ElementsAreRandomized() | ||
{ | ||
// The chance that shuffling a thousand elements produces the same order twice is infinitesimal. | ||
int length = 1000; | ||
foreach (IAsyncEnumerable<int> source in CreateSources(Enumerable.Range(0, length).ToArray())) | ||
{ | ||
int[] first = await source.Shuffle().ToArrayAsync(); | ||
int[] second = await source.Shuffle().ToArrayAsync(); | ||
Assert.Equal(length, first.Length); | ||
Assert.Equal(length, second.Length); | ||
Assert.NotEqual(first, second); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task Cancellation_Cancels() | ||
{ | ||
IAsyncEnumerable<int> source = CreateSource(2, 4, 8, 16); | ||
await Assert.ThrowsAsync<OperationCanceledException>(async () => | ||
{ | ||
await ConsumeAsync(source.Shuffle().WithCancellation(new CancellationToken(true))); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public async Task InterfaceCalls_ExpectedCounts() | ||
{ | ||
TrackingAsyncEnumerable<int> source = CreateSource(2, 4, 8, 16).Track(); | ||
await ConsumeAsync(source.Shuffle()); | ||
Assert.Equal(5, source.MoveNextAsyncCount); | ||
Assert.Equal(4, source.CurrentCount); | ||
Assert.Equal(1, source.DisposeAsyncCount); | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,37 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Xunit; | ||
|
||
namespace System.Linq.Tests | ||
{ | ||
public class ShuffleTests : EnumerableBasedTests | ||
{ | ||
[Fact] | ||
public void InvalidArguments() | ||
{ | ||
AssertExtensions.Throws<ArgumentNullException>("source", () => ((IQueryable<string>)null).Shuffle()); | ||
} | ||
|
||
[Fact] | ||
public void ProducesAllElements() | ||
{ | ||
int[] shuffled = Enumerable.Range(0, 1000).AsQueryable().Shuffle().ToArray(); | ||
Array.Sort(shuffled); | ||
Assert.Equal(Enumerable.Range(0, shuffled.Length), shuffled); | ||
} | ||
|
||
[Fact] | ||
public void ElementsAreRandomized() | ||
{ | ||
// The chance that shuffling a thousand elements produces the same order twice is infinitesimal. | ||
const int Length = 1000; | ||
IQueryable<int> source = Enumerable.Range(0, Length).AsQueryable().Shuffle(); | ||
int[] first = source.ToArray(); | ||
int[] second = source.ToArray(); | ||
Assert.Equal(Length, first.Length); | ||
Assert.Equal(Length, second.Length); | ||
Assert.NotEqual(first, second); | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.