daisy/src/quantity/unit.rs

113 lines
1.7 KiB
Rust

use std::{collections::HashMap, hash::Hash};
use std::ops::{
Mul, Div,
MulAssign, DivAssign
};
#[derive(Debug)]
#[derive(Hash)]
#[derive(Eq, PartialEq)]
#[derive(Copy, Clone)]
pub enum BaseUnit {
Second,
Meter,
Kilogram,
Ampere,
Kelvin,
Mole,
Candela
}
#[derive(Debug)]
#[derive(Clone)]
pub struct Unit {
// Unit, power.
pub val: HashMap<BaseUnit, f64>
}
impl ToString for Unit {
fn to_string(&self) -> String {
format!("{:?}", self)
}
}
impl Unit {
pub fn new() -> Unit {
return Unit{
val: HashMap::new()
}
}
pub fn unitless(&self) -> bool { self.val.len() == 0 }
pub fn insert(&mut self, u: BaseUnit, p: f64) {
match self.val.get_mut(&u) {
Some(i) => {
let n = *i + p;
if n == 0f64 {
self.val.remove(&u);
} else { *i = n; }
},
None => { self.val.insert(u, p); }
};
}
pub fn pow(&mut self, pwr: f64) {
for (_, p) in &mut self.val {
*p *= pwr;
}
}
}
impl PartialEq for Unit {
fn eq(&self, other: &Self) -> bool {
for (u, p) in &other.val {
match self.val.get(u) {
Some(i) => { if i != p { return false; } },
None => { return false; }
};
}
return true;
}
}
impl Mul for Unit {
type Output = Self;
fn mul(self, other: Self) -> Self::Output {
let mut o = self.clone();
for (u, p) in &other.val { o.insert(*u, *p); }
return o;
}
}
impl MulAssign for Unit where {
fn mul_assign(&mut self, other: Self) {
for (u, p) in &other.val { self.insert(*u, *p); }
}
}
impl Div for Unit {
type Output = Self;
fn div(self, other: Self) -> Self::Output {
let mut o = self.clone();
for (u, p) in &other.val { o.insert(*u, -*p); }
return o;
}
}
impl DivAssign for Unit where {
fn div_assign(&mut self, other: Self) {
for (u, p) in &other.val { self.insert(*u, -*p); }
}
}