From 827edd8111c29d44cdcdf7b1ed79896495878046 Mon Sep 17 00:00:00 2001 From: Chip Hogg Date: Mon, 2 Dec 2024 13:42:29 -0500 Subject: [PATCH] Simplify common unit if label has just one unit (#338) If we don't do this, it's a weird situation: we end up with a `CommonUnit` type, whose _label_ is just the label for a single _simple_ unit `X`... which is quantity-equivalent to `CommonUnit`. This means that when we print it, it will _look just like it is_ `X`, and people might get confused. With this PR, when we're in this situation, we simply make `CommonUnitT` (note the `T`, i.e., "compute the common unit") return `X` itself! To do this, we _could_ try to piggyback on the label definition. I found that way introduces some really confusing circular dependencies. I find it is much simpler to simply count the _distinct unscaled units_ in the common unit, and do the simplification exactly when we get down to only one. Helps #105. --- au/code/au/unit_of_measure.hh | 37 ++++++++++++++++++++++++++++-- au/code/au/unit_of_measure_test.cc | 35 ++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/au/code/au/unit_of_measure.hh b/au/code/au/unit_of_measure.hh index 45e06463..0f8580c2 100644 --- a/au/code/au/unit_of_measure.hh +++ b/au/code/au/unit_of_measure.hh @@ -516,6 +516,11 @@ struct CommonUnit { template struct InOrderFor : InOrderFor {}; +template +struct UnitList {}; +template +struct InOrderFor : InOrderFor {}; + namespace detail { // This machinery searches a unit list for one that "matches" a target unit. // @@ -524,7 +529,7 @@ namespace detail { // Generic template. template