From 467d9a55583e90fc39f0fcc38b38d36b16f7de7a Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Mon, 4 Nov 2024 18:04:01 +0100 Subject: [PATCH] refactor: `get_complexity` refactored to be 0-based and not account for a number of arguments in a list --- .../mp-units/framework/quantity_spec.h | 26 ++++++++------ test/static/quantity_spec_test.cpp | 34 +++++++++++-------- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/core/include/mp-units/framework/quantity_spec.h b/src/core/include/mp-units/framework/quantity_spec.h index fe5cc052b..6a7cb9be9 100644 --- a/src/core/include/mp-units/framework/quantity_spec.h +++ b/src/core/include/mp-units/framework/quantity_spec.h @@ -598,9 +598,15 @@ MP_UNITS_EXPORT_END namespace detail { +/** + * @brief @c get_complexity specifies how many type explosions can be done on a quantity + */ template [[nodiscard]] consteval int get_complexity(Q); +// dimensionless should not be exploded to an empty derived quantity +[[nodiscard]] consteval int get_complexity(struct dimensionless) { return 0; } + template [[nodiscard]] consteval int get_complexity(type_list) { @@ -627,7 +633,7 @@ template else if constexpr (requires { Q::_equation_; }) return 1 + get_complexity(Q::_equation_); else - return 1; + return 0; } // dimension_one is always the last one @@ -989,7 +995,7 @@ template 1) { + if constexpr (max_compl > 0) { if constexpr (num_from_compl == max_compl) { constexpr auto res = explode_to_equation(NumFrom{}); return convertible_impl( @@ -1034,7 +1040,7 @@ template 1) { + if constexpr (max_compl > 0) { if constexpr (den_from_compl == max_compl) { constexpr auto res = explode_to_equation(DenFrom{}); return convertible_impl( @@ -1072,7 +1078,7 @@ template 1) { + if constexpr (max_compl > 0) { if constexpr (num_from_compl == max_compl) { constexpr auto res = explode_to_equation(NumFrom{}); return convertible_impl( @@ -1111,7 +1117,7 @@ template 1) { + if constexpr (max_compl > 0) { if constexpr (num_from_compl == max_compl) { constexpr auto res = explode_to_equation(NumFrom{}); return convertible_impl( @@ -1150,7 +1156,7 @@ template 1) { + if constexpr (max_compl > 0) { if constexpr (num_from_compl == max_compl) { constexpr auto res = explode_to_equation(NumFrom{}); return convertible_impl( @@ -1184,7 +1190,7 @@ template 1) { + if constexpr (max_compl > 0) { if constexpr (num_from_compl == max_compl) { constexpr auto res = explode_to_equation(NumFrom{}); return convertible_impl((res.equation * ... * map_power(NumsFrom{})), @@ -1211,7 +1217,7 @@ template 1) { + if constexpr (max_compl > 0) { if constexpr (den_from_compl == max_compl) { constexpr auto res = explode_to_equation(DenFrom{}); return convertible_impl(dimensionless / (res.equation * ... * map_power(DensFrom{})), @@ -1233,7 +1239,7 @@ template 1) { + if constexpr (max_compl > 0) { if constexpr (num_from_compl == max_compl) { constexpr auto res = explode_to_equation(NumFrom{}); return convertible_impl((res.equation * ... * map_power(NumsFrom{})), @@ -1254,7 +1260,7 @@ template 1) { + if constexpr (max_compl > 0) { if constexpr (den_from_compl == max_compl) { constexpr auto res = explode_to_equation(DenFrom{}); return convertible_impl(dimensionless / (res.equation * ... * map_power(DensFrom{})), diff --git a/test/static/quantity_spec_test.cpp b/test/static/quantity_spec_test.cpp index 78cc4e465..63bb95bb6 100644 --- a/test/static/quantity_spec_test.cpp +++ b/test/static/quantity_spec_test.cpp @@ -442,21 +442,25 @@ static_assert(!defines_equation(mechanical_energy)); static_assert(!defines_equation(potential_energy)); // get_complexity -static_assert(get_complexity(dimensionless) == 1); -static_assert(get_complexity(length) == 1); -static_assert(get_complexity(frequency) == 2); -static_assert(get_complexity(area) == 2); -static_assert(get_complexity(volume) == 2); -static_assert(get_complexity(speed) == 3); -static_assert(get_complexity(velocity) == 3); -static_assert(get_complexity(acceleration) == 5); -static_assert(get_complexity(force) == 7); - -static_assert(get_complexity(acceleration * time) == 6); -static_assert(get_complexity(acceleration / time) == 6); - -static_assert(get_complexity(pow<4>(length)) == 1); -static_assert(get_complexity(pow<2>(area)) == 2); +static_assert(get_complexity(length) == 0); +static_assert(get_complexity(pow<4>(length)) == 0); +static_assert(get_complexity(dimensionless) == 0); +static_assert(get_complexity(length / time) == 0); +static_assert(get_complexity(mass * length / time) == 0); +static_assert(get_complexity(frequency) == 1); +static_assert(get_complexity(area) == 1); +static_assert(get_complexity(pow<2>(area)) == 1); +static_assert(get_complexity(volume) == 1); +static_assert(get_complexity(speed) == 1); +static_assert(get_complexity(velocity) == 1); +static_assert(get_complexity(acceleration) == 2); +static_assert(get_complexity(force) == 3); + +static_assert(get_complexity(acceleration * time) == 2); +static_assert(get_complexity(acceleration / time) == 2); +static_assert(get_complexity(speed * area) == 2); +static_assert(get_complexity(speed / frequency) == 2); +static_assert(get_complexity(speed * area / frequency) == 3); // explode static_assert(explode(frequency).quantity == inverse(period_duration));