move POWSolver to external file and deprecate POWChallenge.*solve* methods
This commit is contained in:
parent
a3b47f3e47
commit
e08f890ace
47
src/gen.rs
47
src/gen.rs
|
@ -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)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct POWChallenge {
|
pub struct POWChallenge {
|
||||||
hash: [u8; 32],
|
hash: [u8; 32],
|
||||||
|
@ -78,6 +43,12 @@ impl POWChallenge {
|
||||||
hash_num(num) == self.hash
|
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> {
|
pub fn chunk_solve(self: &POWChallenge, start: u128, end: u128) -> Option<u128> {
|
||||||
for i in start..end {
|
for i in start..end {
|
||||||
if self.check(Num::new(i)) {
|
if self.check(Num::new(i)) {
|
||||||
|
@ -87,6 +58,12 @@ impl POWChallenge {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Solve the challenge.
|
||||||
|
|
||||||
|
This method is deprecated. Use `POWSolver` instead.
|
||||||
|
*/
|
||||||
|
#[deprecated]
|
||||||
pub fn solve_singlethread(self: &POWChallenge) -> u128 {
|
pub fn solve_singlethread(self: &POWChallenge) -> u128 {
|
||||||
match self.chunk_solve(self.range.min, self.range.max) {
|
match self.chunk_solve(self.range.min, self.range.max) {
|
||||||
Some(v) => v,
|
Some(v) => v,
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
pub mod gen;
|
pub mod gen;
|
||||||
pub mod hash;
|
pub mod hash;
|
||||||
pub mod num;
|
pub mod num;
|
||||||
|
pub mod solver;
|
|
@ -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 }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue