From e2d49c5fb642b3e392ebd655ce0dae63f471373c Mon Sep 17 00:00:00 2001 From: Mark Date: Thu, 13 Apr 2023 09:11:20 -0700 Subject: [PATCH] Cleaned up variable names --- src/quantity/quantity.rs | 136 +++++++++++++++++++-------------------- src/quantity/unit.rs | 54 ++++++++-------- 2 files changed, 95 insertions(+), 95 deletions(-) diff --git a/src/quantity/quantity.rs b/src/quantity/quantity.rs index 747fe8b..e0e090d 100644 --- a/src/quantity/quantity.rs +++ b/src/quantity/quantity.rs @@ -15,18 +15,18 @@ use crate::quantity::Scalar; #[derive(Debug)] #[derive(Clone)] pub struct Quantity { - pub v: Scalar, - pub u: Unit + pub scalar: Scalar, + pub unit: Unit } impl ToString for Quantity { fn to_string(&self) -> String { - let n = self.v.to_string(); + let n = self.scalar.to_string(); if self.unitless() { return n; } - let u = self.u.to_string(); + let u = self.unit.to_string(); if self.is_one() { return u; }; return format!("{n} {u}"); @@ -35,10 +35,10 @@ impl ToString for Quantity { impl Quantity { pub fn to_string_outer(&self) -> String { - let n = self.v.to_string(); + let n = self.scalar.to_string(); if self.unitless() { return n; } - let u = self.u.to_string(); + let u = self.unit.to_string(); return format!("{n} {u}"); } @@ -47,8 +47,8 @@ impl Quantity { if v.is_none() { return None; } return Some(Quantity{ - v: v.unwrap(), - u: Unit::new() + scalar: v.unwrap(), + unit: Unit::new() }); } @@ -57,8 +57,8 @@ impl Quantity { if v.is_none() { return None; } return Some(Quantity{ - v: v.unwrap(), - u: Unit::new() + scalar: v.unwrap(), + unit: Unit::new() }); } @@ -67,8 +67,8 @@ impl Quantity { if v.is_none() { return None; } return Some(Quantity{ - v: v.unwrap(), - u: Unit::new() + scalar: v.unwrap(), + unit: Unit::new() }); } @@ -77,29 +77,29 @@ impl Quantity { if v.is_none() { return None; } return Some(Quantity{ - v: v.unwrap(), - u: Unit::new() + scalar: v.unwrap(), + unit: Unit::new() }); } pub fn from_scalar(s: Scalar) -> Quantity { return Quantity{ - v: s, - u: Unit::new() + scalar: s, + unit: Unit::new() }; } - pub fn insert_unit(&mut self, ui: FreeUnit, pi: Scalar) { self.u.insert(ui, pi) } - pub fn set_unit(&mut self, u: Unit) { self.u = u; } + pub fn insert_unit(&mut self, ui: FreeUnit, pi: Scalar) { self.unit.insert(ui, pi) } + pub fn set_unit(&mut self, u: Unit) { self.unit = u; } pub fn convert_to(self, other: Quantity) -> Option { - let fa = self.u.to_base_factor(); - let fb = other.u.to_base_factor(); + let fa = self.unit.to_base_factor(); + let fb = other.unit.to_base_factor(); let r = self * fa / fb; // If this didn't work, units are incompatible - if r.u != other.u { return None; }; + if r.unit != other.unit { return None; }; return Some(r); } @@ -111,8 +111,8 @@ macro_rules! quant_foward { pub fn $x(&self) -> Quantity { if !self.unitless() { panic!() } Quantity { - v: self.v.$x(), - u: self.u.clone() + scalar: self.scalar.$x(), + unit: self.unit.clone() } } } @@ -120,13 +120,13 @@ macro_rules! quant_foward { impl Quantity { - pub fn is_zero(&self) -> bool { self.v.is_zero() } - pub fn is_one(&self) -> bool { self.v.is_one() } - pub fn is_nan(&self) -> bool { self.v.is_nan() } - pub fn is_negative(&self) -> bool { self.v.is_negative() } - pub fn is_positive(&self) -> bool { self.v.is_positive() } - pub fn unitless(&self) -> bool { self.u.unitless() } - pub fn unit(&self) -> &Unit { &self.u } + pub fn is_zero(&self) -> bool { self.scalar.is_zero() } + pub fn is_one(&self) -> bool { self.scalar.is_one() } + pub fn is_nan(&self) -> bool { self.scalar.is_nan() } + pub fn is_negative(&self) -> bool { self.scalar.is_negative() } + pub fn is_positive(&self) -> bool { self.scalar.is_positive() } + pub fn unitless(&self) -> bool { self.unit.unitless() } + pub fn unit(&self) -> &Unit { &self.unit } quant_foward!(fract); quant_foward!(abs); @@ -153,15 +153,15 @@ impl Quantity { pub fn log(&self, base: Quantity) -> Quantity { if !self.unitless() { panic!() } Quantity { - v: self.v.log(base.v), - u: self.u.clone() + scalar: self.scalar.log(base.scalar), + unit: self.unit.clone() } } pub fn pow(&self, pwr: Quantity) -> Quantity { Quantity { - v: self.v.pow(pwr.v.clone()), - u: self.u.pow(pwr.v) + scalar: self.scalar.pow(pwr.scalar.clone()), + unit: self.unit.pow(pwr.scalar) } } } @@ -172,8 +172,8 @@ impl Neg for Quantity where { fn neg(self) -> Self::Output { Quantity { - v: -self.v, - u: self.u + scalar: -self.scalar, + unit: self.unit } } } @@ -182,30 +182,30 @@ impl Add for Quantity { type Output = Self; fn add(self, other: Self) -> Self::Output { - if self.u != other.u { panic!() } + if self.unit != other.unit { panic!() } let mut o = other; - if !o.u.prefixes_match(&self.u) { + if !o.unit.prefixes_match(&self.unit) { o = o.convert_to(self.clone()).unwrap(); } Quantity { - v: self.v + o.v, - u: self.u + scalar: self.scalar + o.scalar, + unit: self.unit } } } impl AddAssign for Quantity where { fn add_assign(&mut self, other: Self) { - if self.u != other.u { panic!() } + if self.unit != other.unit { panic!() } let mut o = other; - if !o.u.prefixes_match(&self.u) { + if !o.unit.prefixes_match(&self.unit) { o = o.convert_to(self.clone()).unwrap(); } - self.v += o.v + self.scalar += o.scalar } } @@ -213,30 +213,30 @@ impl Sub for Quantity { type Output = Self; fn sub(self, other: Self) -> Self::Output { - if self.u != other.u { panic!() } + if self.unit != other.unit { panic!() } let mut o = other; - if !o.u.prefixes_match(&self.u) { + if !o.unit.prefixes_match(&self.unit) { o = o.convert_to(self.clone()).unwrap(); } Quantity { - v: self.v - o.v, - u: self.u + scalar: self.scalar - o.scalar, + unit: self.unit } } } impl SubAssign for Quantity where { fn sub_assign(&mut self, other: Self) { - if self.u != other.u { panic!() } + if self.unit != other.unit { panic!() } let mut o = other; - if !o.u.prefixes_match(&self.u) { + if !o.unit.prefixes_match(&self.unit) { o = o.convert_to(self.clone()).unwrap(); } - self.v -= o.v + self.scalar -= o.scalar; } } @@ -245,21 +245,21 @@ impl Mul for Quantity { fn mul(self, other: Self) -> Self::Output { - let f = self.u.match_prefix_factor(&other.u); + let f = self.unit.match_prefix_factor(&other.unit); Quantity { - v: self.v * other.v * f.v, - u: self.u * other.u * f.u, + scalar: self.scalar * other.scalar * f.scalar, + unit: self.unit * other.unit * f.unit, } } } impl MulAssign for Quantity where { fn mul_assign(&mut self, other: Self) { - let f = self.u.match_prefix_factor(&other.u); + let f = self.unit.match_prefix_factor(&other.unit); - self.v *= other.v * f.v; - self.u *= other.u * f.u; + self.scalar *= other.scalar * f.scalar; + self.unit *= other.unit * f.unit; } } @@ -268,16 +268,16 @@ impl Div for Quantity { fn div(self, other: Self) -> Self::Output { Quantity { - v: self.v / other.v, - u: self.u / other.u + scalar: self.scalar / other.scalar, + unit: self.unit / other.unit } } } impl DivAssign for Quantity where { fn div_assign(&mut self, other: Self) { - self.v /= other.v; - self.u /= other.u; + self.scalar /= other.scalar; + self.unit /= other.unit; } } @@ -285,27 +285,27 @@ impl Rem for Quantity { type Output = Self; fn rem(self, other: Quantity) -> Self::Output { - if !self.u.unitless() { panic!() } - if !other.u.unitless() { panic!() } + if !self.unit.unitless() { panic!() } + if !other.unit.unitless() { panic!() } Quantity { - v: self.v % other.v, - u: self.u + scalar: self.scalar % other.scalar, + unit: self.unit } } } impl PartialEq for Quantity { fn eq(&self, other: &Self) -> bool { - if self.u != other.u {false} else { - self.v == other.v + if self.unit != other.unit {false} else { + self.scalar == other.scalar } } } impl PartialOrd for Quantity { fn partial_cmp(&self, other: &Self) -> Option { - if self.u != other.u { panic!() } - self.v.partial_cmp(&other.v) + if self.unit != other.unit { panic!() } + self.scalar.partial_cmp(&other.scalar) } } \ No newline at end of file diff --git a/src/quantity/unit.rs b/src/quantity/unit.rs index 3cff12e..ad9bec5 100644 --- a/src/quantity/unit.rs +++ b/src/quantity/unit.rs @@ -145,20 +145,20 @@ impl ToString for Prefix { #[derive(Debug)] #[derive(Copy, Clone)] pub struct FreeUnit { - u: UnitBase, - p: Prefix + base: UnitBase, + prefix: Prefix } impl Hash for FreeUnit { fn hash(&self, state: &mut H) { - self.u.hash(state); + self.base.hash(state); } } impl Eq for FreeUnit {} impl PartialEq for FreeUnit { fn eq(&self, other: &Self) -> bool { - self.u.eq(&other.u) + self.base.eq(&other.base) } } @@ -166,8 +166,8 @@ impl PartialEq for FreeUnit { macro_rules! quick_base_factor { (float, $u:expr, $s:expr, $( ($x:expr, $p:expr) ),* ) => { Some(Quantity { - v: Scalar::new_float_from_string($s).unwrap(), - u: Unit::from_array(&[ + scalar: Scalar::new_float_from_string($s).unwrap(), + unit: Unit::from_array(&[ $( (FreeUnit::from_base($x), Scalar::new_rational($p).unwrap()), )* @@ -178,8 +178,8 @@ macro_rules! quick_base_factor { (rational, $u:expr, $s:expr, $( ($x:expr, $p:expr) ),* ) => { Some(Quantity { - v: Scalar::new_float_from_string($s).unwrap(), - u: Unit::from_array(&[ + scalar: Scalar::new_float_from_string($s).unwrap(), + unit: Unit::from_array(&[ $( (FreeUnit::from_base($x), Scalar::new_rational($p).unwrap()), )* @@ -191,21 +191,21 @@ macro_rules! quick_base_factor { impl FreeUnit { - pub fn from_base(u: UnitBase) -> FreeUnit { - return FreeUnit { u, p: Prefix::None } + pub fn from_base(base: UnitBase) -> FreeUnit { + return FreeUnit { base, prefix: Prefix::None } } - pub fn from_base_prefix(u: UnitBase, p: Prefix) -> FreeUnit { FreeUnit {u, p} } - pub fn set_prefix(&mut self, p: Prefix) { self.p = p; } - pub fn get_prefix(&self) -> Prefix { self.p } + pub fn from_base_prefix(base: UnitBase, prefix: Prefix) -> FreeUnit { FreeUnit {base, prefix} } + pub fn set_prefix(&mut self, prefix: Prefix) { self.prefix = prefix; } + pub fn get_prefix(&self) -> Prefix { self.prefix } pub fn same_with_prefix(&self, other: &FreeUnit) -> bool { - self.u.eq(&other.u) && self.p.eq(&other.p) + self.base.eq(&other.base) && self.prefix.eq(&other.prefix) } pub fn to_base_factor(&self) -> Quantity { - let q = match self.u { + let q = match self.base { // Returns the unit we need to multiply by to get a base // unit, or `None` if this is already a base unit. // @@ -258,9 +258,9 @@ impl FreeUnit { let mut q = q.unwrap_or(Quantity::new_rational_from_string("1").unwrap()); - let mut p = self.p.to_ratio(); - p.insert_unit(FreeUnit::from_base(self.u), Scalar::new_rational(1f64).unwrap()); - p.insert_unit(FreeUnit::from_base_prefix(self.u, self.p), Scalar::new_rational(-1f64).unwrap()); + let mut p = self.prefix.to_ratio(); + p.insert_unit(FreeUnit::from_base(self.base), Scalar::new_rational(1f64).unwrap()); + p.insert_unit(FreeUnit::from_base_prefix(self.base, self.prefix), Scalar::new_rational(-1f64).unwrap()); q *= p; return q; @@ -270,7 +270,7 @@ impl FreeUnit { impl ToString for FreeUnit { fn to_string(&self) -> String { - let s = match self.u { + let s = match self.base { UnitBase::Second => "s", UnitBase::Meter => "m", UnitBase::Gram => "g", @@ -288,7 +288,7 @@ impl ToString for FreeUnit { UnitBase::Day => "day", }; - let p = self.p.to_string(); + let p = self.prefix.to_string(); format!("{p}{s}") } @@ -361,6 +361,7 @@ impl Unit { pub fn get_val(&self) -> &HashMap { &self.val } pub fn get_val_mut(&mut self) -> &mut HashMap { &mut self.val } + pub fn unitless(&self) -> bool { self.get_val().len() == 0 } pub fn from_array(a: &[(FreeUnit, Scalar)]) -> Unit { let mut n = Unit::new(); @@ -394,14 +395,14 @@ impl Unit { let (su, _) = k.unwrap(); // Conversion factor ou -> basic - let mut p = ou.p.to_ratio(); - p.insert_unit(FreeUnit::from_base(ou.u), Scalar::new_rational(1f64).unwrap()); - p.insert_unit(FreeUnit::from_base_prefix(ou.u, ou.p), Scalar::new_rational(-1f64).unwrap()); + let mut p = ou.prefix.to_ratio(); + p.insert_unit(FreeUnit::from_base(ou.base), Scalar::new_rational(1f64).unwrap()); + p.insert_unit(FreeUnit::from_base_prefix(ou.base, ou.prefix), Scalar::new_rational(-1f64).unwrap()); // Conversion factor su -> basic - let mut q = su.p.to_ratio(); - q.insert_unit(FreeUnit::from_base(su.u), Scalar::new_rational(1f64).unwrap()); - q.insert_unit(FreeUnit::from_base_prefix(su.u, su.p), Scalar::new_rational(-1f64).unwrap()); + let mut q = su.prefix.to_ratio(); + q.insert_unit(FreeUnit::from_base(su.base), Scalar::new_rational(1f64).unwrap()); + q.insert_unit(FreeUnit::from_base_prefix(su.base, su.prefix), Scalar::new_rational(-1f64).unwrap()); f = f * (p / q).pow(Quantity::from_scalar(op.clone())); } @@ -410,7 +411,6 @@ impl Unit { return f; } - pub fn unitless(&self) -> bool { self.get_val().len() == 0 } pub fn insert(&mut self, u: FreeUnit, p: Scalar) { let v = self.get_val_mut();