From 857774f8d8405c7763ed890f62b0c511a7a5d309 Mon Sep 17 00:00:00 2001 From: Justin Lovinger Date: Sun, 22 Sep 2024 14:48:38 +0000 Subject: [PATCH] optimal-binary: stop cloning `to_min` in `scale` --- optimal-binary/src/lib.rs | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/optimal-binary/src/lib.rs b/optimal-binary/src/lib.rs index 0174184..37be68a 100644 --- a/optimal-binary/src/lib.rs +++ b/optimal-binary/src/lib.rs @@ -3,6 +3,7 @@ use num_traits::{pow, AsPrimitive}; use optimal_compute_core::{ arg, arg1, + control_flow::Then, enumerate::Enumerate, math::{Add, Div, Mul, Pow, SameOrZero, Sub}, peano::{One, Zero}, @@ -48,7 +49,7 @@ pub fn chunks_to_real_le( bits: Bits, ) -> ChunksToRealLe where - ToMin: Clone + Computation, + ToMin: Computation, ToMax: ComputationFn, Bits: ComputationFn, T: 'static @@ -145,7 +146,17 @@ where ) } -pub type Scale = Add, FromMax>, Num>, ToMin>; +pub type Scale = Then< + ToMin, + &'static str, + Add< + Mul< + Div::Dim, ::Item>>, FromMax>, + Num, + >, + Arg<::Dim, ::Item>, + >, +>; /// Scale numbers from `0..=from_max` to `to_min..=to_max`. /// @@ -156,11 +167,6 @@ pub type Scale = Add, From /// `to_max` must be >= `to_min`. /// Passing `0` for `from_max` is an error. /// Passing a number > `from_max` will result in an output > `to_max`. -/// -/// Note, -/// `to_min` will be cloned, -/// so it should be a simple computation -/// or an arg. pub fn scale( from_max: FromMax, to_min: ToMin, @@ -169,7 +175,7 @@ pub fn scale( ) -> Scale where FromMax: ComputationFn, - ToMin: Clone + Computation, + ToMin: Computation, ToMax: ComputationFn, Num: ComputationFn, Num::Item: ops::Add @@ -182,7 +188,11 @@ where SameOrZero, Num::Dim: SameOrZero, { - (((to_max.sub(to_min.clone())).div(from_max)).mul(num)).add(to_min) + to_min.then( + "to_min", + (((to_max.sub(Arg::::new("to_min"))).div(from_max)).mul(num)) + .add(Arg::::new("to_min")), + ) } /// Return the largest integer `to_int_...` can return.