-
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.
- Loading branch information
1 parent
e3dbd28
commit a2be93a
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ version = "0.1.0" | |
edition = "2021" | ||
|
||
[dependencies] | ||
num = "0.4.3" |
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,41 @@ | ||
use num::traits::{FromPrimitive, Num}; | ||
|
||
pub fn binary_search_threshold<T, F>(mut ok: T, mut ng: T, mut check: F, threshold: T) -> T | ||
where | ||
T: Num + PartialOrd + FromPrimitive + Copy, | ||
F: FnMut(T) -> bool, | ||
{ | ||
while if ok > ng { ok - ng } else { ng - ok } > threshold { | ||
let mid = if ok > ng { | ||
ng + (ok - ng) / T::from_usize(2).unwrap() | ||
} else { | ||
ok + (ng - ok) / T::from_usize(2).unwrap() | ||
}; | ||
if check(mid) { | ||
ok = mid; | ||
} else { | ||
ng = mid; | ||
} | ||
} | ||
ok | ||
} | ||
|
||
pub fn binary_search_trial<T, F>(mut ok: T, mut ng: T, mut check: F, trial: usize) -> T | ||
where | ||
T: Num + PartialOrd + FromPrimitive + Copy, | ||
F: FnMut(T) -> bool, | ||
{ | ||
for _ in 0..trial { | ||
let mid = if ok > ng { | ||
ng + (ok - ng) / T::from_usize(2).unwrap() | ||
} else { | ||
ok + (ng - ok) / T::from_usize(2).unwrap() | ||
}; | ||
if check(mid) { | ||
ok = mid; | ||
} else { | ||
ng = mid; | ||
} | ||
} | ||
ok | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod graph; | ||
pub mod algorithm; |