This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The generator can now be enumerated synchronously and asynchronously. The async generator can now be cancelled abruptly (throw) or gracefully (just stop). The generator is now in a separate library. Tests and web app feature three scenarios: 1. Multiply first N primes synchronously, no fault handling. 2. Multiply first N primes asynchronously, fail if time limit exceeded. 3. Multiply as many of the first N primes as possible in time limit.
- Loading branch information
Showing
14 changed files
with
254 additions
and
150 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,7 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> |
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,88 @@ | ||
namespace ClassLibrary1 | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
public sealed partial class PrimeGenerator | ||
{ | ||
private sealed class PrimeAsyncEnumerator : IAsyncEnumerator<int> | ||
{ | ||
private const int notInitialised = 1; | ||
private readonly PrimeGeneratorOptions options; | ||
private readonly CancellationToken cancellationToken; | ||
private int current = notInitialised; | ||
|
||
internal PrimeAsyncEnumerator(PrimeGeneratorOptions options = PrimeGeneratorOptions.None, CancellationToken cancellationToken = default) | ||
{ | ||
this.options = options; | ||
this.cancellationToken = cancellationToken; | ||
} | ||
|
||
int IAsyncEnumerator<int>.Current | ||
{ | ||
get | ||
{ | ||
if (this.current == notInitialised) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
|
||
return this.current; | ||
} | ||
} | ||
|
||
private bool IsPrime | ||
{ | ||
get | ||
{ | ||
for (int i = 2; i * i <= this.current; i++) | ||
{ | ||
this.ThrowIfNeeded(); | ||
|
||
if (this.current % i == 0) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
private bool ShouldStop => | ||
!this.ShouldThrowOnCancel && this.cancellationToken.IsCancellationRequested; | ||
|
||
ValueTask IAsyncDisposable.DisposeAsync() => | ||
default; | ||
|
||
ValueTask<bool> IAsyncEnumerator<int>.MoveNextAsync() | ||
{ | ||
do | ||
{ | ||
if (this.ShouldStop) | ||
{ | ||
return new ValueTask<bool>(false); | ||
} | ||
|
||
this.current++; | ||
|
||
} while (!this.IsPrime); | ||
|
||
return new ValueTask<bool>(true); | ||
} | ||
|
||
private void ThrowIfNeeded() | ||
{ | ||
if (this.ShouldThrowOnCancel) | ||
{ | ||
this.cancellationToken.ThrowIfCancellationRequested(); | ||
} | ||
} | ||
|
||
private bool ShouldThrowOnCancel => | ||
this.options.HasFlag(PrimeGeneratorOptions.ThrowOnCancel); | ||
} | ||
} | ||
} |
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,32 @@ | ||
namespace ClassLibrary1 | ||
{ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
public sealed partial class PrimeGenerator | ||
{ | ||
private sealed class PrimeEnumerator : IEnumerator<int> | ||
{ | ||
private readonly IAsyncEnumerator<int> primeAsyncEnumerator; | ||
|
||
internal PrimeEnumerator(PrimeAsyncEnumerator primeAsyncEnumerator) => | ||
this.primeAsyncEnumerator = primeAsyncEnumerator; | ||
|
||
public int Current => | ||
this.primeAsyncEnumerator.Current; | ||
|
||
object IEnumerator.Current => | ||
this.Current; | ||
|
||
void IDisposable.Dispose() => | ||
this.primeAsyncEnumerator.DisposeAsync().GetAwaiter().GetResult(); | ||
|
||
bool IEnumerator.MoveNext() => | ||
this.primeAsyncEnumerator.MoveNextAsync().GetAwaiter().GetResult(); | ||
|
||
void IEnumerator.Reset() => | ||
throw new InvalidOperationException(); | ||
} | ||
} | ||
} |
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,22 @@ | ||
namespace ClassLibrary1 | ||
{ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
|
||
public sealed partial class PrimeGenerator : IAsyncEnumerable<int>, IEnumerable<int> | ||
{ | ||
private readonly PrimeGeneratorOptions options; | ||
|
||
public PrimeGenerator(PrimeGeneratorOptions options = PrimeGeneratorOptions.None) => | ||
this.options = options; | ||
|
||
public IAsyncEnumerator<int> GetAsyncEnumerator(CancellationToken cancellationToken = default) => | ||
new PrimeAsyncEnumerator(this.options, cancellationToken); | ||
|
||
public IEnumerator<int> GetEnumerator() => | ||
new PrimeEnumerator(new PrimeAsyncEnumerator()); | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator(); | ||
} | ||
} |
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,11 @@ | ||
namespace ClassLibrary1 | ||
{ | ||
using System; | ||
|
||
[Flags] | ||
public enum PrimeGeneratorOptions | ||
{ | ||
None = 0b_0, | ||
ThrowOnCancel = 0b_1 | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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.