From 84f2d6e30c9627e25e54db327d8a4902464854b0 Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 11 Apr 2023 08:21:30 -0700 Subject: [PATCH] Unit cleanup --- src/quantity/mod.rs | 1 - src/quantity/quantity.rs | 21 ++------------ src/quantity/unit.rs | 62 +++++++++++++++++++++++----------------- 3 files changed, 38 insertions(+), 46 deletions(-) diff --git a/src/quantity/mod.rs b/src/quantity/mod.rs index c962230..f065af0 100644 --- a/src/quantity/mod.rs +++ b/src/quantity/mod.rs @@ -22,7 +22,6 @@ pub(in crate::quantity) use crate::quantity::scalar::Scalar; mod unit; pub use crate::quantity::unit::Unit; pub use crate::quantity::unit::BaseUnit; -pub(in crate::quantity) use crate::quantity::unit::CompoundUnit; mod quantity; pub use crate::quantity::quantity::Quantity; diff --git a/src/quantity/quantity.rs b/src/quantity/quantity.rs index 233afff..20c97c4 100644 --- a/src/quantity/quantity.rs +++ b/src/quantity/quantity.rs @@ -9,7 +9,6 @@ use std::cmp::Ordering; use crate::quantity::Unit; use crate::quantity::BaseUnit; -use crate::quantity::CompoundUnit; use crate::quantity::Scalar; @@ -17,8 +16,8 @@ use crate::quantity::Scalar; #[derive(Debug)] #[derive(Clone)] pub struct Quantity { - v: Scalar, - u: Unit + pub v: Scalar, + pub u: Unit } @@ -97,6 +96,7 @@ impl Quantity { "k" => Some(BaseUnit::Kelvin), "mol" => Some(BaseUnit::Mole), "c" => Some(BaseUnit::Candela), + "ft" => Some(BaseUnit::Foot), _ => { None } }; @@ -110,21 +110,6 @@ impl Quantity { return Some(q); }; - // Compound units - let b = match s { - "ft" => Some(CompoundUnit::FOOT), - _ => { None } - }; - - if b.is_some() { - let b = b.unwrap(); - let q = Quantity{ - v: b.coef(), - u: b.unit() - }; - return Some(q); - }; - return None; } diff --git a/src/quantity/unit.rs b/src/quantity/unit.rs index 493b68f..1081dc0 100644 --- a/src/quantity/unit.rs +++ b/src/quantity/unit.rs @@ -6,6 +6,8 @@ use std::ops::{ use crate::quantity::Scalar; +use super::Quantity; + #[derive(Debug)] #[derive(Hash)] #[derive(Eq, PartialEq)] @@ -17,43 +19,39 @@ pub enum BaseUnit { Ampere, Kelvin, Mole, - Candela + Candela, + + Foot, } -pub struct CompoundUnit { - coef_str: &'static str, - rational: bool, - units: &'static[(BaseUnit, f64)], - pub str: &'static str -} +impl BaseUnit { + pub fn is_base(&self) -> bool { + match self { + BaseUnit::Second + | BaseUnit::Meter + | BaseUnit::Kilogram + | BaseUnit::Ampere + | BaseUnit::Kelvin + | BaseUnit::Mole + | BaseUnit::Candela + => true, -impl CompoundUnit { - pub const FOOT: CompoundUnit = CompoundUnit { - coef_str: "0.3048", - rational: false, - units: &[(BaseUnit::Meter, 1f64)], - str: "ft" - }; - - pub fn unit(&self) -> Unit { - let mut n = Unit::new(); - for (u, p) in self.units.iter() { - n.insert(*u, *p); + _ => false } - return n; } - pub fn coef(&self) -> Scalar { - if self.rational { - Scalar::new_rational_from_string(self.coef_str).unwrap() - } else { - Scalar::new_float_from_string(self.coef_str).unwrap() + pub fn to_base(&self) -> Option { + match self { + BaseUnit::Foot => Some(Quantity { + v: Scalar::new_float_from_string("0.3048").unwrap(), + u: Unit::from_array(&[(BaseUnit::Meter, 1f64)]) + }), + _ => None } } } - #[derive(Debug)] #[derive(Clone)] pub struct Unit { @@ -88,7 +86,9 @@ impl ToString for Unit { BaseUnit::Ampere => "a", BaseUnit::Kelvin => "k", BaseUnit::Mole => "mol", - BaseUnit::Candela => "c" + BaseUnit::Candela => "c", + + BaseUnit::Foot => "ft", }; if *p == 1f64 { @@ -129,6 +129,14 @@ impl Unit { } } + pub fn from_array(a: &[(BaseUnit, f64)]) -> Unit { + let mut n = Unit::new(); + for (u, p) in a.iter() { + n.insert(*u, *p); + } + return n; + } + pub fn unitless(&self) -> bool { self.val.len() == 0 } pub fn insert(&mut self, u: BaseUnit, p: f64) {