aboutsummaryrefslogtreecommitdiff
path: root/azalea-core/src/math.rs
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2024-12-27 05:43:35 +0000
committermat <git@matdoes.dev>2024-12-27 05:43:35 +0000
commit3c3952bb0b255a4e03537fbfe3b53634a64c6a7b (patch)
tree85e38de84373d3f83d43ebacf7f754d14f393ceb /azalea-core/src/math.rs
parent33e1a1326a462263aa578b5095c3c37160345c77 (diff)
downloadazalea-drasl-3c3952bb0b255a4e03537fbfe3b53634a64c6a7b.tar.xz
resolve some todos in az-core
Diffstat (limited to 'azalea-core/src/math.rs')
-rw-r--r--azalea-core/src/math.rs23
1 files changed, 16 insertions, 7 deletions
diff --git a/azalea-core/src/math.rs b/azalea-core/src/math.rs
index 62215749..67ece5cc 100644
--- a/azalea-core/src/math.rs
+++ b/azalea-core/src/math.rs
@@ -1,4 +1,8 @@
-use std::{f64::consts::PI, sync::LazyLock};
+use std::{
+ f64::consts::PI,
+ ops::{Add, Div, Sub},
+ sync::LazyLock,
+};
pub const EPSILON: f64 = 1.0E-7;
@@ -24,17 +28,22 @@ pub fn cos(x: f32) -> f32 {
SIN[x]
}
-// TODO: make this generic
-pub fn binary_search(mut min: i32, max: i32, predicate: &dyn Fn(i32) -> bool) -> i32 {
+pub fn binary_search<
+ T: Ord + PartialOrd + Add<Output = T> + Sub<Output = T> + Div<Output = T> + From<u8> + Copy,
+>(
+ mut min: T,
+ max: T,
+ predicate: impl Fn(T) -> bool,
+) -> T {
let mut diff = max - min;
- while diff > 0 {
- let diff_mid = diff / 2;
+ while diff > T::from(0) {
+ let diff_mid = diff / T::from(2);
let mid = min + diff_mid;
if predicate(mid) {
diff = diff_mid;
} else {
- min = mid + 1;
- diff -= diff_mid + 1;
+ min = mid + T::from(1);
+ diff = diff - (diff_mid + T::from(1));
}
}