-
Notifications
You must be signed in to change notification settings - Fork 6
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
6 changed files
with
91 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use rand_core::CryptoRngCore; | ||
|
||
use crate::SieveFactory; | ||
|
||
/// Sieves through the results of `sieve_factory` and returns the first item for which `predicate` is `true`. | ||
/// | ||
/// If `sieve_factory` signals that no more results can be created, returns `None`. | ||
pub fn sieve_and_find<R, S, T>(rng: &mut R, sieve_factory: &S, predicate: impl Fn(&mut R, &T) -> bool) -> Option<T> | ||
where | ||
S: SieveFactory<T>, | ||
R: CryptoRngCore, | ||
{ | ||
let mut sieve = sieve_factory.make_sieve(rng, None)?; | ||
|
||
loop { | ||
let prime = sieve.find(|num| predicate(rng, num)); | ||
if prime.is_some() { | ||
return prime; | ||
} | ||
if let Some(new_sieve) = sieve_factory.make_sieve(rng, Some(sieve)) { | ||
sieve = new_sieve; | ||
} else { | ||
return None; | ||
} | ||
} | ||
} |
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
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