Skip to content

Commit

Permalink
Fix upstream agrian_units reducing bug
Browse files Browse the repository at this point in the history
  • Loading branch information
turboladen committed Sep 10, 2024
1 parent 454004e commit 82f5050
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
11 changes: 11 additions & 0 deletions crates/api/src/measurement/ops/mul_div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -975,5 +975,16 @@ mod tests {
term!(Meter, exponent: 2)
)
);

// VolumePerVolume * VolumePerArea
validate_ok!(
volume_per_volume_and_volume_per_area,
parse_unit!("m3/m3"), parse_unit!("m3/m2"),
mul_values: 2.0, 2.0,
mul_unit: parse_unit!("m"),
div_values: 0.5, 2.0,
div_unit1: parse_unit!("/m"),
div_unit2: parse_unit!("m")
);
}
}
2 changes: 2 additions & 0 deletions crates/api/src/unit/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,5 +264,7 @@ mod tests {
METER, METER_PER_SECOND => parse_unit!["m2/s"]);
test_mul!(test_annotatable_mul_different_annotatable:
parse_unit!("42m{foo}"), parse_unit!("42m-1{bar}") => parse_unit!("42m{foo}/42m{bar}"));
test_mul!(test_volume_per_volume_mul_volume_per_area:
parse_unit!("m3/m3"), parse_unit!("m3/m2") => parse_unit!("m"));
}
}
4 changes: 2 additions & 2 deletions crates/api/src/unit/parser/terms/mapper/basic_component.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use pest::iterators::Pair;

use crate::{
term::{variants::FactorAnnotation, Factor},
term::{variants::FactorAnnotation, Factor, UNITY},
unit::parser::{terms::term_parser::Rule, Error, Visit},
Annotation,
};
Expand Down Expand Up @@ -82,7 +82,7 @@ impl Finishable for BasicComponent<'_> {
let mut terms: Vec<Term> = Vec::with_capacity(self.terms.len() + 1);

let self_term = match (self.factor, self.annotatable, self.annotation) {
(None, None, None) => unreachable!("Parsed empty unit string for Term"),
(None, None, None) => UNITY,
(None, None, Some(annotation)) => Term::Annotation(Annotation::from(annotation)),
(None, Some(Annotatable(term)), None) => term,
(None, Some(Annotatable(mut term)), Some(annotation)) => {
Expand Down
42 changes: 35 additions & 7 deletions crates/api/src/unit/term_reducing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,31 @@ pub(super) fn compare_and_cancel(lhs_terms: &[Term], rhs_terms: Vec<Term>) -> Co

output.extend(what_to_keep.new_terms);
remaining_rhs = what_to_keep.kept_terms;

// Break if there is nothing left on the RHS to compare the LHS to.
if remaining_rhs.is_empty() {
break;
}
}

// `remaining_rhs` are the RHS Terms that couldn't be combined with any LHS ones.
output.extend_from_slice(&remaining_rhs);

let output = if output.len() > 1 {
let what_to_keep = simplify_one_to_many(&output[0], &output[1..]);

match (what_to_keep.keep_lhs, what_to_keep.made_new_terms()) {
(true, true) => {
let mut new_output = vec![output[0].clone()];
new_output.extend(what_to_keep.into_terms());
new_output
}
(true, false) => output,
(false, true) => what_to_keep.into_terms(),
(false, false) => {
// NOTE: Shouldn't be able to get here...
what_to_keep.into_terms()
}
}
} else {
output
};

cleanup(output)
}

Expand All @@ -84,6 +99,19 @@ pub(super) struct WhatToKeep {
pub(super) kept_terms: Vec<Term>,
}

impl WhatToKeep {
pub(super) fn made_new_terms(&self) -> bool {
!self.new_terms.is_empty()
}

pub(super) fn into_terms(self) -> Vec<Term> {
let mut output = self.kept_terms;
output.extend(self.new_terms);

output
}
}

impl Default for WhatToKeep {
fn default() -> Self {
Self {
Expand All @@ -103,8 +131,8 @@ pub(super) fn simplify_one_to_many(lhs: &Term, rhs_terms: &[Term]) -> WhatToKeep
what_to_keep.keep_lhs = false;

match rhs_terms.get((i + 1)..) {
Some(things) if !things.is_empty() => {
let wtk = simplify_one_to_many(&new_term, things);
Some(remaining_rhs) if !remaining_rhs.is_empty() => {
let wtk = simplify_one_to_many(&new_term, remaining_rhs);

if wtk.keep_lhs {
what_to_keep.new_terms.push(new_term);
Expand Down

0 comments on commit 82f5050

Please sign in to comment.