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;
|
mod unit;
|
||||||
pub use crate::quantity::unit::Unit;
|
pub use crate::quantity::unit::Unit;
|
||||||
pub use crate::quantity::unit::BaseUnit;
|
pub use crate::quantity::unit::BaseUnit;
|
||||||
pub(in crate::quantity) use crate::quantity::unit::CompoundUnit;
|
|
||||||
|
|
||||||
mod quantity;
|
mod quantity;
|
||||||
pub use crate::quantity::quantity::Quantity;
|
pub use crate::quantity::quantity::Quantity;
|
||||||
|
|
|
@ -9,7 +9,6 @@ use std::cmp::Ordering;
|
||||||
|
|
||||||
use crate::quantity::Unit;
|
use crate::quantity::Unit;
|
||||||
use crate::quantity::BaseUnit;
|
use crate::quantity::BaseUnit;
|
||||||
use crate::quantity::CompoundUnit;
|
|
||||||
|
|
||||||
|
|
||||||
use crate::quantity::Scalar;
|
use crate::quantity::Scalar;
|
||||||
|
@ -17,8 +16,8 @@ use crate::quantity::Scalar;
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Quantity {
|
pub struct Quantity {
|
||||||
v: Scalar,
|
pub v: Scalar,
|
||||||
u: Unit
|
pub u: Unit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -97,6 +96,7 @@ impl Quantity {
|
||||||
"k" => Some(BaseUnit::Kelvin),
|
"k" => Some(BaseUnit::Kelvin),
|
||||||
"mol" => Some(BaseUnit::Mole),
|
"mol" => Some(BaseUnit::Mole),
|
||||||
"c" => Some(BaseUnit::Candela),
|
"c" => Some(BaseUnit::Candela),
|
||||||
|
"ft" => Some(BaseUnit::Foot),
|
||||||
_ => { None }
|
_ => { None }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -110,21 +110,6 @@ impl Quantity {
|
||||||
return Some(q);
|
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;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,8 @@ use std::ops::{
|
||||||
|
|
||||||
use crate::quantity::Scalar;
|
use crate::quantity::Scalar;
|
||||||
|
|
||||||
|
use super::Quantity;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[derive(Hash)]
|
#[derive(Hash)]
|
||||||
#[derive(Eq, PartialEq)]
|
#[derive(Eq, PartialEq)]
|
||||||
|
@ -17,43 +19,39 @@ pub enum BaseUnit {
|
||||||
Ampere,
|
Ampere,
|
||||||
Kelvin,
|
Kelvin,
|
||||||
Mole,
|
Mole,
|
||||||
Candela
|
Candela,
|
||||||
|
|
||||||
|
Foot,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CompoundUnit {
|
impl BaseUnit {
|
||||||
coef_str: &'static str,
|
pub fn is_base(&self) -> bool {
|
||||||
rational: bool,
|
match self {
|
||||||
units: &'static[(BaseUnit, f64)],
|
BaseUnit::Second
|
||||||
pub str: &'static str
|
| BaseUnit::Meter
|
||||||
}
|
| BaseUnit::Kilogram
|
||||||
|
| BaseUnit::Ampere
|
||||||
|
| BaseUnit::Kelvin
|
||||||
|
| BaseUnit::Mole
|
||||||
|
| BaseUnit::Candela
|
||||||
|
=> true,
|
||||||
|
|
||||||
impl CompoundUnit {
|
_ => false
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
return n;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn coef(&self) -> Scalar {
|
pub fn to_base(&self) -> Option<Quantity> {
|
||||||
if self.rational {
|
match self {
|
||||||
Scalar::new_rational_from_string(self.coef_str).unwrap()
|
BaseUnit::Foot => Some(Quantity {
|
||||||
} else {
|
v: Scalar::new_float_from_string("0.3048").unwrap(),
|
||||||
Scalar::new_float_from_string(self.coef_str).unwrap()
|
u: Unit::from_array(&[(BaseUnit::Meter, 1f64)])
|
||||||
|
}),
|
||||||
|
_ => None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Unit {
|
pub struct Unit {
|
||||||
|
@ -88,7 +86,9 @@ impl ToString for Unit {
|
||||||
BaseUnit::Ampere => "a",
|
BaseUnit::Ampere => "a",
|
||||||
BaseUnit::Kelvin => "k",
|
BaseUnit::Kelvin => "k",
|
||||||
BaseUnit::Mole => "mol",
|
BaseUnit::Mole => "mol",
|
||||||
BaseUnit::Candela => "c"
|
BaseUnit::Candela => "c",
|
||||||
|
|
||||||
|
BaseUnit::Foot => "ft",
|
||||||
};
|
};
|
||||||
|
|
||||||
if *p == 1f64 {
|
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 unitless(&self) -> bool { self.val.len() == 0 }
|
||||||
|
|
||||||
pub fn insert(&mut self, u: BaseUnit, p: f64) {
|
pub fn insert(&mut self, u: BaseUnit, p: f64) {
|
||||||
|
|
Loading…
Reference in New Issue