mirror of https://github.com/rm-dr/daisy
Unit cleanup
parent
175261b5c0
commit
84f2d6e30c
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Quantity> {
|
||||
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) {
|
||||
|
|
Loading…
Reference in New Issue