move POWSolver to external file and deprecate POWChallenge.*solve* methods

This commit is contained in:
b1ek 2023-08-02 20:37:03 +10:00
parent a3b47f3e47
commit e08f890ace
Signed by: blek
GPG Key ID: 14546221E3595D0C
3 changed files with 72 additions and 35 deletions

View File

@ -15,41 +15,6 @@ impl POWRange {
}
}
pub struct POWSolver {
challenge: POWChallenge
}
impl POWSolver {
/**
* Solve chunk with feedback.
* Feedback is a function that is being
* called at each iteration of a loop,
* which will be useful for progressbars
* and visual feedback.
*
* Example:
* ```rust
let challenge = POWChallenge::make(POWRange::new(0, 16));
let num = POWSolver::new(challenge).chunk_solve_feedback(0, 16, |x| println!("{}", x)).unwrap();
println!("Found number: {}", num)
* ```
*/
pub fn chunk_solve_feedback(self: &POWSolver, start: u128, end: u128, callback: fn(u128)) -> Option<u128> {
for i in start..end {
callback(i);
if self.challenge.check(i.into()) {
return Some(i);
}
}
None
}
pub fn new(challenge: POWChallenge) -> POWSolver {
POWSolver { challenge }
}
}
#[derive(Debug, Clone)]
pub struct POWChallenge {
hash: [u8; 32],
@ -78,6 +43,12 @@ impl POWChallenge {
hash_num(num) == self.hash
}
/**
Solve a chunk of numbers.
This method is deprecated. Use `POWSolver` instead.
*/
#[deprecated]
pub fn chunk_solve(self: &POWChallenge, start: u128, end: u128) -> Option<u128> {
for i in start..end {
if self.check(Num::new(i)) {
@ -87,6 +58,12 @@ impl POWChallenge {
None
}
/**
Solve the challenge.
This method is deprecated. Use `POWSolver` instead.
*/
#[deprecated]
pub fn solve_singlethread(self: &POWChallenge) -> u128 {
match self.chunk_solve(self.range.min, self.range.max) {
Some(v) => v,

View File

@ -1,3 +1,4 @@
pub mod gen;
pub mod hash;
pub mod num;
pub mod solver;

59
src/solver.rs Normal file
View File

@ -0,0 +1,59 @@
use crate::gen::POWChallenge;
pub struct POWSolver {
challenge: POWChallenge,
result: Option<u128>
}
impl POWSolver {
/**
Solve chunk with feedback.
Feedback is a function that is
being called at each iteration
of a loop, which will be useful
for progressbars and visual feedback.
Example:
```rust
let challenge = POWChallenge::make(POWRange::new(0, 16));
let num = POWSolver::new(challenge).chunk_solve_feedback(0, 16, |x| println!("{}", x)).unwrap();
println!("Found number: {}", num)
```
*/
pub fn chunk_solve_feedback(self: &mut POWSolver, start: u128, end: u128, callback: fn(u128)) -> Option<u128> {
for i in start..end {
callback(i);
if self.challenge.check(i.into()) {
self.result = Some(i);
return Some(i);
}
}
None
}
/**
* Solve a chunk.
* This function is the same as `POWRange.chunk_solve`, the only difference is that it doesnt call a function each iteration which could possibly save some time
*
* Example:
* ```rust
let challenge = POWChallenge::make(POWRange::new(0, 16));
let num = POWSolver::new(challenge).chunk_solve(0, 16).unwrap();
println!("Found number: {}", num)
* ```
*/
pub fn chunk_solve(self: &mut POWSolver, start: u128, end: u128) -> Option<u128> {
for i in start..end {
if self.challenge.check(i.into()) {
self.result = Some(i);
return Some(i);
}
}
None
}
pub fn new(challenge: POWChallenge) -> POWSolver {
POWSolver { challenge, result: None }
}
}