From ca8d5fd235d4392a16ad0d1293c9da32d052c634 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Thu, 2 May 2024 12:02:45 -0400 Subject: [PATCH] xo-unit: refactor: Quantity -> xquantity to match filename --- include/xo/unit/xquantity.hpp | 90 ++++++++++---------- include/xo/unit/xquantity_iostream.hpp | 2 +- utest/xquantity.test.cpp | 110 ++++++++++++------------- 3 files changed, 101 insertions(+), 101 deletions(-) diff --git a/include/xo/unit/xquantity.hpp b/include/xo/unit/xquantity.hpp index 83f64d49..75edf2e9 100644 --- a/include/xo/unit/xquantity.hpp +++ b/include/xo/unit/xquantity.hpp @@ -1,4 +1,4 @@ -/** @file Quantity.hpp +/** @file xquantity.hpp * * Author: Roland Conybeare **/ @@ -29,7 +29,7 @@ namespace xo { template > - class Quantity { + class xquantity { public: using repr_type = Repr; using unit_type = natural_unit; @@ -38,9 +38,9 @@ namespace xo { public: /* zero, dimensionless */ - constexpr Quantity() + constexpr xquantity() : scale_{0}, unit_{natural_unit()} {} - constexpr Quantity(Repr scale, + constexpr xquantity(Repr scale, const natural_unit & unit) : scale_{scale}, unit_{unit} {} @@ -49,9 +49,9 @@ namespace xo { constexpr bool is_dimensionless() const { return unit_.is_dimensionless(); } - constexpr Quantity unit_qty() const { return Quantity(1, unit_); } - constexpr Quantity zero_qty() const { return Quantity(0, unit_); } - constexpr Quantity reciprocal() const { return Quantity(1.0 / scale_, unit_.reciprocal()); } + constexpr xquantity unit_qty() const { return xquantity(1, unit_); } + constexpr xquantity zero_qty() const { return xquantity(0, unit_); } + constexpr xquantity reciprocal() const { return xquantity(1.0 / scale_, unit_.reciprocal()); } constexpr auto rescale(const natural_unit & unit2) const { @@ -63,38 +63,38 @@ namespace xo { repr_type r_scale = (::sqrt(rr.outer_scale_sq_) * rr.outer_scale_factor_.template convert_to() * this->scale_); - return Quantity(r_scale, unit2); + return xquantity(r_scale, unit2); } else { - return Quantity(std::numeric_limits::quiet_NaN(), unit2); + return xquantity(std::numeric_limits::quiet_NaN(), unit2); } } template requires std::is_arithmetic_v constexpr auto scale_by(Dimensionless x) const { - return Quantity(x * this->scale_, this->unit_); + return xquantity(x * this->scale_, this->unit_); } template requires std::is_arithmetic_v constexpr auto divide_by(Dimensionless x) const { - return Quantity(this->scale_ / x, this->unit_); + return xquantity(this->scale_ / x, this->unit_); } template requires std::is_arithmetic_v constexpr auto divide_into(Dimensionless x) const { - return Quantity(x / this->scale_, this->unit_.reciprocal()); + return xquantity(x / this->scale_, this->unit_.reciprocal()); } template static constexpr - auto multiply(const Quantity & x, const Quantity2 & y) { - using r_repr_type = std::common_type_t; - using r_int_type = std::common_type_t; - using r_int2x_type = std::common_type_t; auto rr = detail::su_product(x.unit(), y.unit()); @@ -104,18 +104,18 @@ namespace xo { * static_cast(x.scale()) * static_cast(y.scale())); - return Quantity(r_scale, + return xquantity(r_scale, rr.natural_unit_); } template static constexpr - auto divide(const Quantity & x, const Quantity2 & y) { - using r_repr_type = std::common_type_t; - using r_int_type = std::common_type_t; - using r_int2x_type = std::common_type_t; auto rr = detail::su_ratio(x.unit(), y.unit()); @@ -128,18 +128,18 @@ namespace xo { * static_cast(x.scale()) / static_cast(y.scale())); - return Quantity(r_scale, + return xquantity(r_scale, rr.natural_unit_); } template static constexpr - auto add(const Quantity & x, const Quantity2 & y) { - using r_repr_type = std::common_type_t; - using r_int_type = std::common_type_t; - using r_int2x_type = std::common_type_t; /* conversion to get y in same units as x: multiply by y/x */ @@ -151,22 +151,22 @@ namespace xo { * rr.outer_scale_factor_.template convert_to() * static_cast(y.scale()))); - return Quantity(r_scale, x.unit_.template to_repr()); + return xquantity(r_scale, x.unit_.template to_repr()); } else { /* units don't match! */ - return Quantity(std::numeric_limits::quiet_NaN(), + return xquantity(std::numeric_limits::quiet_NaN(), x.unit_.template to_repr()); } } template static constexpr - auto subtract(const Quantity & x, const Quantity2 & y) { - using r_repr_type = std::common_type_t; - using r_int_type = std::common_type_t; - using r_int2x_type = std::common_type_t; /* conversion to get y in same units as x: multiply by y/x */ @@ -178,36 +178,36 @@ namespace xo { * rr.outer_scale_factor_.template convert_to() * static_cast(y.scale()))); - return Quantity(r_scale, x.unit_.template to_repr()); + return xquantity(r_scale, x.unit_.template to_repr()); } else { /* units don't match! */ - return Quantity(std::numeric_limits::quiet_NaN(), + return xquantity(std::numeric_limits::quiet_NaN(), x.unit_.template to_repr()); } } template static constexpr - auto compare(const Quantity & x, const Quantity2 & y) { - Quantity y2 = y.rescale(x.unit_); + auto compare(const xquantity & x, const Quantity2 & y) { + xquantity y2 = y.rescale(x.unit_); return x.scale() <=> y2.scale(); } - Quantity operator-() const { - return Quantity(-scale_, unit_); + xquantity operator-() const { + return xquantity(-scale_, unit_); } /* also works with Quantity2 = double, int, .. */ template - Quantity & operator*= (const Quantity2 & x) { + xquantity & operator*= (const Quantity2 & x) { *this = *this * x; return *this; } /* also works with Quantity2 = double, int, .. */ template - Quantity & operator/= (const Quantity2 & x) { + xquantity & operator/= (const Quantity2 & x) { *this = *this / x; return *this; } @@ -227,9 +227,9 @@ namespace xo { **/ template - inline constexpr Quantity + inline constexpr xquantity unit_qty(const scaled_unit & u) { - return Quantity + return xquantity (u.outer_scale_factor_.template convert_to() * ::sqrt(u.outer_scale_sq_), u.natural_unit_); } @@ -238,9 +238,9 @@ namespace xo { **/ template - inline constexpr Quantity + inline constexpr xquantity natural_unit_qty(const natural_unit & nu) { - return Quantity(1.0, nu); + return xquantity(1.0, nu); } /** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr) @@ -377,4 +377,4 @@ namespace xo { } /*namespace qty*/ } /*namespace xo*/ -/** end Quantity.hpp **/ +/** end xquantity.hpp **/ diff --git a/include/xo/unit/xquantity_iostream.hpp b/include/xo/unit/xquantity_iostream.hpp index 6b57990e..c79c0bb7 100644 --- a/include/xo/unit/xquantity_iostream.hpp +++ b/include/xo/unit/xquantity_iostream.hpp @@ -15,7 +15,7 @@ namespace xo { typename Int = std::int64_t> inline std::ostream & operator<< (std::ostream & os, - const Quantity & x) + const xquantity & x) { os << x.scale() << x.abbrev(); diff --git a/utest/xquantity.test.cpp b/utest/xquantity.test.cpp index 319edcd6..745963b2 100644 --- a/utest/xquantity.test.cpp +++ b/utest/xquantity.test.cpp @@ -1,4 +1,4 @@ -/* @file Quantity.test.cpp */ +/* @file xquantity.test.cpp */ #include "xquantity.hpp" #include "xquantity_iostream.hpp" @@ -15,7 +15,7 @@ namespace xo { namespace su = xo::qty::su; namespace nu = xo::qty::nu; - using xo::qty::Quantity; + using xo::qty::xquantity; using xo::qty::natural_unit; using xo::qty::power_ratio_type; using xo::qty::scalefactor_ratio_type; @@ -163,8 +163,8 @@ namespace xo { * track relative scale as we go */ - Quantity q1 = natural_unit_qty(nu::dimensionless); - Quantity q2 = natural_unit_qty(nu::dimensionless); + xquantity q1 = natural_unit_qty(nu::dimensionless); + xquantity q2 = natural_unit_qty(nu::dimensionless); static_assert(std::same_as); static_assert(std::same_as); @@ -172,8 +172,8 @@ namespace xo { double k1 = 0.0; /*q1/q2*/ double k2 = 0.0; /*q2/q1*/ { - Quantity q12 = (q1/q2); - Quantity q21 = (q2/q1); + xquantity q12 = (q1/q2); + xquantity q21 = (q2/q1); REQUIRE(q12.is_dimensionless()); REQUIRE(q21.is_dimensionless()); @@ -220,10 +220,10 @@ namespace xo { if (power == -1) nu1_j = nu1_j.reciprocal(); - Quantity q1_j = natural_unit_qty(nu1_j); - Quantity q2_j = q1_j; - Quantity r1; - Quantity r2; + xquantity q1_j = natural_unit_qty(nu1_j); + xquantity q2_j = q1_j; + xquantity r1; + xquantity r2; auto nu2_j = nu1_j; auto nu2_j_ix = rng() % p_nu_v->size(); @@ -366,10 +366,10 @@ namespace xo { * 1. start with a set of basis units in each dimension. * 2. verify +,- by combining quantities with different units */ - TEST_CASE("Quantity.full", "[Quantity.full]") { + TEST_CASE("xquantity.full", "[xquantity.full]") { constexpr bool c_debug_flag = false; - scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.Quantity.full")); + scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.xquantity.full")); // can get bits instead from /dev/random by uncommenting the line below in place of 2nd line //rng::Seed seed; @@ -380,12 +380,12 @@ namespace xo { auto rng = xoshiro256ss(seed); quantity_tests(c_debug_flag, rng); - } /*TEST_CASE(Quantity.full)*/ + } /*TEST_CASE(xquantity.full)*/ - TEST_CASE("Quantity", "[Quantity]") { + TEST_CASE("xquantity", "[xquantity]") { constexpr bool c_debug_flag = false; - scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.Quantity")); + scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.xquantity")); /* not constexpr until c++26 */ auto ng = unit_qty(su::nanogram); @@ -393,44 +393,44 @@ namespace xo { log && log(xtag("ng", ng)); REQUIRE(ng.scale() == 1); - } /*TEST_CASE(Quantity)*/ + } /*TEST_CASE(xquantity)*/ - TEST_CASE("Quantity2", "[Quantity]") { + TEST_CASE("xquantity2", "[xquantity]") { constexpr bool c_debug_flag = false; - scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.Quantity2")); + scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.xquantity2")); /* not constexpr until c++26 */ - Quantity ng = unit_qty(su::nanogram); + xquantity ng = unit_qty(su::nanogram); auto ng2 = ng * ng; log && log(xtag("ng*ng", ng2)); REQUIRE(ng2.scale() == 1); - } /*TEST_CASE(Quantity2)*/ + } /*TEST_CASE(xquantity2)*/ - TEST_CASE("Quantity3", "[Quantity]") { + TEST_CASE("xquantity3", "[xquantity]") { constexpr bool c_debug_flag = false; - scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.Quantity3")); + scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.xquantity3")); /* not constexpr until c++26 */ - Quantity ng = unit_qty(su::nanogram); + xquantity ng = unit_qty(su::nanogram); auto ng0 = ng / ng; log && log(xtag("ng/ng", ng0)); REQUIRE(ng0.scale() == 1); - } /*TEST_CASE(Quantity3)*/ + } /*TEST_CASE(xquantity3)*/ - TEST_CASE("Quantity4", "[Quantity]") { + TEST_CASE("xquantity4", "[xquantity]") { constexpr bool c_debug_flag = false; - scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.Quantity4")); + scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.xquantity4")); /* not constexpr until c++26 */ - Quantity ng = unit_qty(su::nanogram); - Quantity ug = unit_qty(su::microgram); + xquantity ng = unit_qty(su::nanogram); + xquantity ug = unit_qty(su::microgram); { auto prod1 = ng * ug; @@ -457,16 +457,16 @@ namespace xo { } //REQUIRE(ng2.scale() == 1); - } /*TEST_CASE(Quantity4)*/ + } /*TEST_CASE(xquantity4)*/ - TEST_CASE("Quantity5", "[Quantity]") { + TEST_CASE("xquantity5", "[xquantity]") { constexpr bool c_debug_flag = false; - scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.Quantity5")); + scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.xquantity5")); /* not constexpr until c++26 */ - Quantity ng = unit_qty(su::nanogram); - Quantity ug = unit_qty(su::microgram); + xquantity ng = unit_qty(su::nanogram); + xquantity ug = unit_qty(su::microgram); { auto ratio1 = ng / ug; @@ -486,16 +486,16 @@ namespace xo { } //REQUIRE(ng2.scale() == 1); - } /*TEST_CASE(Quantity5)*/ + } /*TEST_CASE(xquantity5)*/ - TEST_CASE("Quantity6", "[Quantity]") { + TEST_CASE("xquantity6", "[xquantity]") { constexpr bool c_debug_flag = false; - scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.Quantity6")); + scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.xquantity6")); /* not constexpr until c++26 */ - Quantity ng = unit_qty(su::nanogram); - Quantity ug = unit_qty(su::microgram); + xquantity ng = unit_qty(su::nanogram); + xquantity ug = unit_qty(su::microgram); { auto sum1 = ng + ug; @@ -518,16 +518,16 @@ namespace xo { } //REQUIRE(ng2.scale() == 1); - } /*TEST_CASE(Quantity6)*/ + } /*TEST_CASE(xquantity6)*/ - TEST_CASE("Quantity7", "[Quantity]") { + TEST_CASE("xquantity7", "[xquantity]") { constexpr bool c_debug_flag = false; - scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.Quantity7")); + scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.xquantity7")); /* not constexpr until c++26 */ - Quantity ng = unit_qty(su::nanogram); - Quantity ug = unit_qty(su::microgram); + xquantity ng = unit_qty(su::nanogram); + xquantity ug = unit_qty(su::microgram); { auto sum1 = ng - ug; @@ -550,16 +550,16 @@ namespace xo { } //REQUIRE(ng2.scale() == 1); - } /*TEST_CASE(Quantity7)*/ + } /*TEST_CASE(xquantity7)*/ - TEST_CASE("Quantity.compare", "[Quantity.compare]") { + TEST_CASE("xquantity.compare", "[xquantity.compare]") { constexpr bool c_debug_flag = false; - scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.Quantity.compare")); + scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.xquantity.compare")); /* not constexpr until c++26 */ - Quantity ng = 1000 * unit_qty(su::nanogram); - Quantity ug = unit_qty(su::microgram); + xquantity ng = 1000 * unit_qty(su::nanogram); + xquantity ug = unit_qty(su::microgram); { auto cmp = (ng == ug); @@ -609,16 +609,16 @@ namespace xo { REQUIRE(cmp == true); } - } /*TEST_CASE(Quantity.compare)*/ + } /*TEST_CASE(xquantity.compare)*/ - TEST_CASE("Quantity.compare2", "[Quantity]") { + TEST_CASE("xquantity.compare2", "[xquantity]") { constexpr bool c_debug_flag = false; - scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.Quantity.compare2")); + scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.xquantity.compare2")); /* not constexpr until c++26 */ - Quantity ng = unit_qty(su::nanogram); - Quantity ug = unit_qty(su::microgram); + xquantity ng = unit_qty(su::nanogram); + xquantity ug = unit_qty(su::microgram); { auto cmp = (ng == ug); @@ -668,9 +668,9 @@ namespace xo { REQUIRE(cmp == false); } - } /*TEST_CASE(Quantity.compare2)*/ + } /*TEST_CASE(xquantity.compare2)*/ } /*namespace ut*/ } /*namespace xo*/ -/* end Quantity.test.cpp */ +/* end xquantity.test.cpp */