From 5f20b53485fcf3005b97d235ab6ec17e4ac971f1 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Wed, 8 May 2024 11:02:14 -0400 Subject: [PATCH] xo-unit: refactor: basis_unit2_abbrev_type -> bu_abbrev_type --- include/xo/unit/basis_unit.hpp | 12 ++-- include/xo/unit/basis_unit_abbrev.hpp | 98 ++++++++++++++------------- include/xo/unit/bpu_store.hpp | 6 +- utest/basis_unit.test.cpp | 94 +++++++++++++++---------- utest/unit.test.cpp | 2 +- 5 files changed, 117 insertions(+), 95 deletions(-) diff --git a/include/xo/unit/basis_unit.hpp b/include/xo/unit/basis_unit.hpp index dabe91cb..d8a28f65 100644 --- a/include/xo/unit/basis_unit.hpp +++ b/include/xo/unit/basis_unit.hpp @@ -31,7 +31,7 @@ namespace xo { constexpr const scalefactor_ratio_type & scalefactor() const { return scalefactor_; } ///@} - constexpr basis_unit2_abbrev_type abbrev() const { + constexpr bu_abbrev_type abbrev() const { return abbrev::basis_unit2_abbrev(native_dim_, scalefactor_); } @@ -192,20 +192,20 @@ namespace xo { template struct scaled_native_unit2_abbrev { - static constexpr const basis_unit2_abbrev_type value - = (basis_unit2_abbrev_type::from_flatstring + static constexpr const bu_abbrev_type value + = (bu_abbrev_type::from_flatstring (native_unit2_v[static_cast(BasisDim)] .abbrev_str())); }; inline - constexpr basis_unit2_abbrev_type + constexpr bu_abbrev_type bu_fallback_abbrev(dim basis_dim, const scalefactor_ratio_type & scalefactor) { - return (basis_unit2_abbrev_type::from_flatstring + return (bu_abbrev_type::from_flatstring (flatstring_concat - (scalefactor.to_str(), + (scalefactor.to_str(), native_unit2_v[static_cast(basis_dim)].abbrev_str()))); } } diff --git a/include/xo/unit/basis_unit_abbrev.hpp b/include/xo/unit/basis_unit_abbrev.hpp index 1a5346db..bf6682f3 100644 --- a/include/xo/unit/basis_unit_abbrev.hpp +++ b/include/xo/unit/basis_unit_abbrev.hpp @@ -11,21 +11,21 @@ namespace xo { namespace qty { - using basis_unit2_abbrev_type = flatstring<16>; + using bu_abbrev_type = flatstring<16>; using scalefactor_ratio_type = xo::ratio::ratio; namespace abbrev { static - constexpr basis_unit2_abbrev_type + constexpr bu_abbrev_type fallback_unit_abbrev(const scalefactor_ratio_type & scalefactor, dimension native_dim) { /* e.g. unit of '1000 grams' will have abbrev '1000g' in absence * of a specialization for scaled_native_unit_abbrev */ - return (basis_unit2_abbrev_type::from_flatstring + return (bu_abbrev_type::from_flatstring (flatstring_concat - (scalefactor.to_str(), + (scalefactor.to_str(), native_unit2_v[static_cast(native_dim)].abbrev_str()))); } @@ -43,36 +43,36 @@ namespace xo { * @endcode **/ static - constexpr basis_unit2_abbrev_type + constexpr bu_abbrev_type mass_unit2_abbrev(const scalefactor_ratio_type & scalefactor) { if (scalefactor.num() == 1) { switch (scalefactor.den()) { case 1: - return basis_unit2_abbrev_type::from_chars("g"); + return bu_abbrev_type::from_chars("g"); case 1000: - return basis_unit2_abbrev_type::from_chars("mg"); + return bu_abbrev_type::from_chars("mg"); case 1000000: - return basis_unit2_abbrev_type::from_chars("ug"); + return bu_abbrev_type::from_chars("ug"); case 1000000000: - return basis_unit2_abbrev_type::from_chars("ng"); + return bu_abbrev_type::from_chars("ng"); case 1000000000000: - return basis_unit2_abbrev_type::from_chars("pg"); + return bu_abbrev_type::from_chars("pg"); } } if (scalefactor.den() == 1) { switch (scalefactor.num()) { case 1000: - return basis_unit2_abbrev_type::from_chars("kg"); + return bu_abbrev_type::from_chars("kg"); case 1000000: - return basis_unit2_abbrev_type::from_chars("t"); + return bu_abbrev_type::from_chars("t"); case 1000000000: - return basis_unit2_abbrev_type::from_chars("kt"); + return bu_abbrev_type::from_chars("kt"); case 1000000000000: - return basis_unit2_abbrev_type::from_chars("Mt"); + return bu_abbrev_type::from_chars("Mt"); case 1000000000000000: - return basis_unit2_abbrev_type::from_chars("Gt"); + return bu_abbrev_type::from_chars("Gt"); } } @@ -82,36 +82,38 @@ namespace xo { // ----- units for dim::distance ----- static - constexpr basis_unit2_abbrev_type + constexpr bu_abbrev_type distance_unit2_abbrev(const scalefactor_ratio_type & scalefactor) { if (scalefactor.num() == 1) { switch (scalefactor.den()) { case 1: - return basis_unit2_abbrev_type::from_chars("m"); + return bu_abbrev_type::from_chars("m"); case 1000: - return basis_unit2_abbrev_type::from_chars("mm"); + return bu_abbrev_type::from_chars("mm"); case 1000000: - return basis_unit2_abbrev_type::from_chars("um"); + return bu_abbrev_type::from_chars("um"); case 1000000000: - return basis_unit2_abbrev_type::from_chars("nm"); + return bu_abbrev_type::from_chars("nm"); case 1000000000000: - return basis_unit2_abbrev_type::from_chars("pm"); + return bu_abbrev_type::from_chars("pm"); } } if (scalefactor.den() == 1) { switch (scalefactor.num()) { case 1000: - return basis_unit2_abbrev_type::from_chars("km"); + return bu_abbrev_type::from_chars("km"); case 1000000: - return basis_unit2_abbrev_type::from_chars("Mm"); + return bu_abbrev_type::from_chars("Mm"); case 299792458: - return basis_unit2_abbrev_type::from_chars("lsec"); + return bu_abbrev_type::from_chars("lsec"); case 1000000000: - return basis_unit2_abbrev_type::from_chars("Gm"); + return bu_abbrev_type::from_chars("Gm"); case 149597870700: - return basis_unit2_abbrev_type::from_chars("AU"); + return bu_abbrev_type::from_chars("AU"); + } + } } } @@ -121,44 +123,44 @@ namespace xo { // ----- units for dim::time ----- static - constexpr basis_unit2_abbrev_type + constexpr bu_abbrev_type time_unit2_abbrev(const scalefactor_ratio_type & scalefactor) { if (scalefactor.num() == 1) { switch (scalefactor.den()) { case 1: - return basis_unit2_abbrev_type::from_chars("s"); + return bu_abbrev_type::from_chars("s"); case 1000: - return basis_unit2_abbrev_type::from_chars("ms"); + return bu_abbrev_type::from_chars("ms"); case 1000000: - return basis_unit2_abbrev_type::from_chars("us"); + return bu_abbrev_type::from_chars("us"); case 1000000000: - return basis_unit2_abbrev_type::from_chars("ns"); + return bu_abbrev_type::from_chars("ns"); case 1000000000000: - return basis_unit2_abbrev_type::from_chars("ps"); + return bu_abbrev_type::from_chars("ps"); } } if (scalefactor.den() == 1) { switch (scalefactor.num()) { case 60: - return basis_unit2_abbrev_type::from_chars("min"); + return bu_abbrev_type::from_chars("min"); case 3600: - return basis_unit2_abbrev_type::from_chars("hr"); + return bu_abbrev_type::from_chars("hr"); case 24*3600: - return basis_unit2_abbrev_type::from_chars("dy"); + return bu_abbrev_type::from_chars("dy"); case 7*24*3600: - return basis_unit2_abbrev_type::from_chars("wk"); + return bu_abbrev_type::from_chars("wk"); case 30*24*3600: - return basis_unit2_abbrev_type::from_chars("mo"); + return bu_abbrev_type::from_chars("mo"); case 250*24*3600: - return basis_unit2_abbrev_type::from_chars("yr250"); + return bu_abbrev_type::from_chars("yr250"); case 360*24*3600: - return basis_unit2_abbrev_type::from_chars("yr360"); + return bu_abbrev_type::from_chars("yr360"); case 365*24*3600: - return basis_unit2_abbrev_type::from_chars("yr365"); + return bu_abbrev_type::from_chars("yr365"); case 365*24*3600+6*3600: - return basis_unit2_abbrev_type::from_chars("yr"); + return bu_abbrev_type::from_chars("yr"); } } @@ -167,13 +169,13 @@ namespace xo { // ----- units for dim::currency ----- - static constexpr basis_unit2_abbrev_type + static constexpr bu_abbrev_type currency_unit2_abbrev(const scalefactor_ratio_type & scalefactor) { if (scalefactor.num() == 1) { switch(scalefactor.den()) { case 1: - return basis_unit2_abbrev_type::from_chars("ccy"); + return bu_abbrev_type::from_chars("ccy"); } } @@ -182,13 +184,13 @@ namespace xo { // ----- units for dim::price ----- - static constexpr basis_unit2_abbrev_type + static constexpr bu_abbrev_type price_unit2_abbrev(const scalefactor_ratio_type & scalefactor) { if (scalefactor.num() == 1) { switch(scalefactor.den()) { case 1: - return basis_unit2_abbrev_type::from_chars("px"); + return bu_abbrev_type::from_chars("px"); } } @@ -198,7 +200,7 @@ namespace xo { // ----- basis_unit2_abbrev ----- static - constexpr basis_unit2_abbrev_type + constexpr bu_abbrev_type basis_unit2_abbrev(dimension native_dim, const scalefactor_ratio_type & scalefactor) { @@ -215,13 +217,13 @@ namespace xo { return price_unit2_abbrev(scalefactor); case dimension::invalid: case dimension::n_dim: - return basis_unit2_abbrev_type(); + return bu_abbrev_type(); break; } /* unreachable (for well-constructed dim instances) */ - return basis_unit2_abbrev_type(); + return bu_abbrev_type(); } } /*namespace abbrev*/ } /*namespace qty*/ diff --git a/include/xo/unit/bpu_store.hpp b/include/xo/unit/bpu_store.hpp index 04cb086e..4298d977 100644 --- a/include/xo/unit/bpu_store.hpp +++ b/include/xo/unit/bpu_store.hpp @@ -46,10 +46,10 @@ namespace xo { /* e.g. * [(1/1000000000, "nm"), (1/1000000, "um"), (1/1000, "mm"), (1/1, "m"), (1000/1, "km")] */ - using native_scale_v = std::vector>; + using native_scale_v = std::vector>; /** @brief get basis-unit abbreviation at runtime **/ - basis_unit2_abbrev_type bu_abbrev(dim basis_dim, + bu_abbrev_type bu_abbrev(dim basis_dim, const scalefactor_ratio_type & scalefactor) const { const auto & bu_abbrev_v = bu_abbrev_vv_[static_cast(basis_dim)]; @@ -86,7 +86,7 @@ namespace xo { /** @brief establish abbreviation @p abbrev for basis unit @p bu **/ void bu_establish_abbrev(const basis_unit & bu, - const basis_unit2_abbrev_type & abbrev) { + const bu_abbrev_type & abbrev) { auto & bu_abbrev_v = bu_abbrev_vv_[static_cast(bu.native_dim())]; diff --git a/utest/basis_unit.test.cpp b/utest/basis_unit.test.cpp index 326dc453..6cf22a56 100644 --- a/utest/basis_unit.test.cpp +++ b/utest/basis_unit.test.cpp @@ -9,7 +9,7 @@ namespace xo { using xo::qty::scalefactor_ratio_type; using xo::qty::abbrev::basis_unit2_abbrev; using xo::qty::basis_unit; - using xo::qty::basis_unit2_abbrev_type; + using xo::qty::bu_abbrev_type; using xo::qty::native_unit2_v; using xo::qty::dim; namespace bu = xo::qty::detail::bu; @@ -21,7 +21,7 @@ namespace xo { * we will need this for quantity> */ template - constexpr basis_unit2_abbrev_type bu_mpl_abbrev = bu.abbrev(); + constexpr bu_abbrev_type bu_mpl_abbrev = bu.abbrev(); TEST_CASE("basis_unit", "[basis_unit]") { static_assert(bu_mpl_abbrev == bu::gram.abbrev()); @@ -42,7 +42,7 @@ namespace xo { log && log(xtag("mass*10^3", basis_unit2_abbrev(dim::mass, scalefactor_ratio_type(1000, 1)))); static_assert(basis_unit2_abbrev(dim::mass, scalefactor_ratio_type(1000, 1)) - == basis_unit2_abbrev_type::from_chars("kg")); + == bu_abbrev_type::from_chars("kg")); log && log("---------------------"); @@ -50,64 +50,84 @@ namespace xo { # define REQUIRE_x2(x) static_assert(x); REQUIRE(x) - REQUIRE_x2(bu::picogram.abbrev() == basis_unit2_abbrev_type::from_chars("pg")); + log && log(xtag("mass", basis_unit2_abbrev(dim::distance, scalefactor_ratio_type(1, 1)))); + + REQUIRE_x2(bu::picogram.abbrev() == bu_abbrev_type::from_chars("pg")); REQUIRE_x2(bu::nanogram.abbrev() - == basis_unit2_abbrev_type::from_chars("ng")); + == bu_abbrev_type::from_chars("ng")); REQUIRE_x2(bu::microgram.abbrev() - == basis_unit2_abbrev_type::from_chars("ug")); + == bu_abbrev_type::from_chars("ug")); REQUIRE_x2(bu::milligram.abbrev() - == basis_unit2_abbrev_type::from_chars("mg")); + == bu_abbrev_type::from_chars("mg")); REQUIRE_x2(bu::gram.abbrev() - == basis_unit2_abbrev_type::from_chars("g")); + == bu_abbrev_type::from_chars("g")); REQUIRE_x2(bu::kilogram.abbrev() - == basis_unit2_abbrev_type::from_chars("kg")); + == bu_abbrev_type::from_chars("kg")); REQUIRE_x2(bu::tonne.abbrev() - == basis_unit2_abbrev_type::from_chars("t")); + == bu_abbrev_type::from_chars("t")); REQUIRE_x2(bu::kilotonne.abbrev() - == basis_unit2_abbrev_type::from_chars("kt")); + == bu_abbrev_type::from_chars("kt")); REQUIRE_x2(bu::megatonne.abbrev() - == basis_unit2_abbrev_type::from_chars("Mt")); + == bu_abbrev_type::from_chars("Mt")); + REQUIRE_x2(bu::gigatonne.abbrev() + == bu_abbrev_type::from_chars("Gt")); log && log(xtag("distance", basis_unit2_abbrev(dim::distance, scalefactor_ratio_type(1, 1)))); REQUIRE_x2(bu::picometre.abbrev() - == basis_unit2_abbrev_type::from_chars("pm")); + == bu_abbrev_type::from_chars("pm")); REQUIRE_x2(bu::nanometre.abbrev() - == basis_unit2_abbrev_type::from_chars("nm")); + == bu_abbrev_type::from_chars("nm")); REQUIRE_x2(bu::micrometre.abbrev() - == basis_unit2_abbrev_type::from_chars("um")); + == bu_abbrev_type::from_chars("um")); REQUIRE_x2(bu::millimetre.abbrev() - == basis_unit2_abbrev_type::from_chars("mm")); + == bu_abbrev_type::from_chars("mm")); REQUIRE_x2(bu::metre.abbrev() - == basis_unit2_abbrev_type::from_chars("m")); + == bu_abbrev_type::from_chars("m")); REQUIRE_x2(bu::kilometre.abbrev() - == basis_unit2_abbrev_type::from_chars("km")); + == bu_abbrev_type::from_chars("km")); REQUIRE_x2(bu::megametre.abbrev() - == basis_unit2_abbrev_type::from_chars("Mm")); + == bu_abbrev_type::from_chars("Mm")); REQUIRE_x2(bu::gigametre.abbrev() - == basis_unit2_abbrev_type::from_chars("Gm")); + == bu_abbrev_type::from_chars("Gm")); - REQUIRE_x2(bu::lightsecond.abbrev() == basis_unit2_abbrev_type::from_chars("lsec")); - REQUIRE_x2(bu::astronomicalunit.abbrev() == basis_unit2_abbrev_type::from_chars("AU")); + REQUIRE_x2(bu::picometer.abbrev() + == bu_abbrev_type::from_chars("pm")); + REQUIRE_x2(bu::nanometer.abbrev() + == bu_abbrev_type::from_chars("nm")); + REQUIRE_x2(bu::micrometer.abbrev() + == bu_abbrev_type::from_chars("um")); + REQUIRE_x2(bu::millimeter.abbrev() + == bu_abbrev_type::from_chars("mm")); + REQUIRE_x2(bu::meter.abbrev() + == bu_abbrev_type::from_chars("m")); + REQUIRE_x2(bu::kilometer.abbrev() + == bu_abbrev_type::from_chars("km")); + REQUIRE_x2(bu::megameter.abbrev() + == bu_abbrev_type::from_chars("Mm")); + REQUIRE_x2(bu::gigameter.abbrev() + == bu_abbrev_type::from_chars("Gm")); + + REQUIRE_x2(bu::lightsecond.abbrev() == flatstring("lsec")); + REQUIRE_x2(bu::astronomicalunit.abbrev() == flatstring("AU")); log && log(xtag("time", basis_unit2_abbrev(dim::time, scalefactor_ratio_type(1, 1)))); - REQUIRE_x2(bu::second.abbrev() == basis_unit2_abbrev_type::from_chars("s")); - REQUIRE_x2(bu::picosecond.abbrev() == basis_unit2_abbrev_type::from_chars("ps")); - REQUIRE_x2(bu::nanosecond.abbrev() == basis_unit2_abbrev_type::from_chars("ns")); - REQUIRE_x2(bu::microsecond.abbrev() == basis_unit2_abbrev_type::from_chars("us")); - REQUIRE_x2(bu::millisecond.abbrev() == basis_unit2_abbrev_type::from_chars("ms")); - REQUIRE_x2(bu::second.abbrev() == basis_unit2_abbrev_type::from_chars("s")); - REQUIRE_x2(bu::minute.abbrev() == basis_unit2_abbrev_type::from_chars("min")); - REQUIRE_x2(bu::hour.abbrev() == basis_unit2_abbrev_type::from_chars("hr")); - REQUIRE_x2(bu::day.abbrev() == basis_unit2_abbrev_type::from_chars("dy")); - REQUIRE_x2(bu::week.abbrev() == basis_unit2_abbrev_type::from_chars("wk")); - REQUIRE_x2(bu::month.abbrev() == basis_unit2_abbrev_type::from_chars("mo")); + REQUIRE_x2(bu::picosecond.abbrev() == bu_abbrev_type::from_chars("ps")); + REQUIRE_x2(bu::nanosecond.abbrev() == bu_abbrev_type::from_chars("ns")); + REQUIRE_x2(bu::microsecond.abbrev() == bu_abbrev_type::from_chars("us")); + REQUIRE_x2(bu::millisecond.abbrev() == bu_abbrev_type::from_chars("ms")); + REQUIRE_x2(bu::second.abbrev() == bu_abbrev_type::from_chars("s")); + REQUIRE_x2(bu::minute.abbrev() == bu_abbrev_type::from_chars("min")); + REQUIRE_x2(bu::hour.abbrev() == bu_abbrev_type::from_chars("hr")); + REQUIRE_x2(bu::day.abbrev() == bu_abbrev_type::from_chars("dy")); + REQUIRE_x2(bu::week.abbrev() == bu_abbrev_type::from_chars("wk")); + REQUIRE_x2(bu::month.abbrev() == bu_abbrev_type::from_chars("mo")); - REQUIRE_x2(bu::year.abbrev() == basis_unit2_abbrev_type::from_chars("yr")); - REQUIRE_x2(bu::year250.abbrev() == basis_unit2_abbrev_type::from_chars("yr250")); - REQUIRE_x2(bu::year360.abbrev() == basis_unit2_abbrev_type::from_chars("yr360")); - REQUIRE_x2(bu::year365.abbrev() == basis_unit2_abbrev_type::from_chars("yr365")); + REQUIRE_x2(bu::year.abbrev() == bu_abbrev_type::from_chars("yr")); + REQUIRE_x2(bu::year250.abbrev() == bu_abbrev_type::from_chars("yr250")); + REQUIRE_x2(bu::year360.abbrev() == bu_abbrev_type::from_chars("yr360")); + REQUIRE_x2(bu::year365.abbrev() == bu_abbrev_type::from_chars("yr365")); log && log(xtag("currency", basis_unit2_abbrev(dim::currency, scalefactor_ratio_type(1, 1)))); diff --git a/utest/unit.test.cpp b/utest/unit.test.cpp index 437e73f7..b2d9b0eb 100644 --- a/utest/unit.test.cpp +++ b/utest/unit.test.cpp @@ -15,7 +15,7 @@ namespace xo { namespace u = xo::qty::u; using xo::qty::dim; - using xo::qty::basis_unit2_abbrev_type; + using xo::qty::bu_abbrev_type; using xo::qty::scalefactor_ratio_type; using xo::qty::basis_unit2_store; using xo::qty::power_ratio_type;