From a2be93aa68bf7af02bf09e0a42650575fdfbe841 Mon Sep 17 00:00:00 2001 From: michirakara Date: Sun, 29 Sep 2024 19:05:52 -0700 Subject: [PATCH] refactor less likely to overflow --- Cargo.lock | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/algorithm.rs | 41 ++++++++++++++++++++++++ src/lib.rs | 1 + 4 files changed, 125 insertions(+) create mode 100644 src/algorithm.rs diff --git a/Cargo.lock b/Cargo.lock index 5bbe091..027d4df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,88 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "num" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "rs-mcr-lib" version = "0.1.0" +dependencies = [ + "num", +] diff --git a/Cargo.toml b/Cargo.toml index 185f841..5b152b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] +num = "0.4.3" diff --git a/src/algorithm.rs b/src/algorithm.rs new file mode 100644 index 0000000..0950fda --- /dev/null +++ b/src/algorithm.rs @@ -0,0 +1,41 @@ +use num::traits::{FromPrimitive, Num}; + +pub fn binary_search_threshold(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(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 +} diff --git a/src/lib.rs b/src/lib.rs index 6f94350..e01e136 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,2 @@ pub mod graph; +pub mod algorithm;