Skip to content

Commit

Permalink
refactor: get_complexity refactored to be 0-based and not account f…
Browse files Browse the repository at this point in the history
…or a number of arguments in a list
  • Loading branch information
mpusz committed Nov 4, 2024
1 parent 73a003d commit 467d9a5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
26 changes: 16 additions & 10 deletions src/core/include/mp-units/framework/quantity_spec.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<QuantitySpec Q>
[[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<typename... Ts>
[[nodiscard]] consteval int get_complexity(type_list<Ts...>)
{
Expand All @@ -627,7 +633,7 @@ template<QuantitySpec Q>
else if constexpr (requires { Q::_equation_; })
return 1 + get_complexity(Q::_equation_);
else
return 1;
return 0;
}

// dimension_one is always the last one
Expand Down Expand Up @@ -989,7 +995,7 @@ template<typename NumFrom, typename... NumsFrom, typename DenFrom, typename... D
constexpr auto num_to_compl = get_complexity(NumTo{});
constexpr auto den_to_compl = get_complexity(DenTo{});
constexpr auto max_compl = max({num_from_compl, num_to_compl, den_from_compl, den_to_compl});
if constexpr (max_compl > 1) {
if constexpr (max_compl > 0) {
if constexpr (num_from_compl == max_compl) {
constexpr auto res = explode_to_equation(NumFrom{});
return convertible_impl(
Expand Down Expand Up @@ -1034,7 +1040,7 @@ template<typename DenFrom, typename... DensFrom, typename NumTo, typename... Num
constexpr auto num_to_compl = get_complexity(NumTo{});
constexpr auto den_to_compl = get_complexity(DenTo{});
constexpr auto max_compl = max({num_to_compl, den_from_compl, den_to_compl});
if constexpr (max_compl > 1) {
if constexpr (max_compl > 0) {
if constexpr (den_from_compl == max_compl) {
constexpr auto res = explode_to_equation(DenFrom{});
return convertible_impl(
Expand Down Expand Up @@ -1072,7 +1078,7 @@ template<typename NumFrom, typename... NumsFrom, typename NumTo, typename... Num
constexpr auto num_to_compl = get_complexity(NumTo{});
constexpr auto den_to_compl = get_complexity(DenTo{});
constexpr auto max_compl = max({num_from_compl, num_to_compl, den_to_compl});
if constexpr (max_compl > 1) {
if constexpr (max_compl > 0) {
if constexpr (num_from_compl == max_compl) {
constexpr auto res = explode_to_equation(NumFrom{});
return convertible_impl(
Expand Down Expand Up @@ -1111,7 +1117,7 @@ template<typename NumFrom, typename... NumsFrom, typename DenFrom, typename... D
constexpr auto den_from_compl = get_complexity(DenFrom{});
constexpr auto den_to_compl = get_complexity(DenTo{});
constexpr auto max_compl = max({num_from_compl, den_from_compl, den_to_compl});
if constexpr (max_compl > 1) {
if constexpr (max_compl > 0) {
if constexpr (num_from_compl == max_compl) {
constexpr auto res = explode_to_equation(NumFrom{});
return convertible_impl(
Expand Down Expand Up @@ -1150,7 +1156,7 @@ template<typename NumFrom, typename... NumsFrom, typename DenFrom, typename... D
constexpr auto den_from_compl = get_complexity(DenFrom{});
constexpr auto num_to_compl = get_complexity(NumTo{});
constexpr auto max_compl = max({num_from_compl, num_to_compl, den_from_compl});
if constexpr (max_compl > 1) {
if constexpr (max_compl > 0) {
if constexpr (num_from_compl == max_compl) {
constexpr auto res = explode_to_equation(NumFrom{});
return convertible_impl(
Expand Down Expand Up @@ -1184,7 +1190,7 @@ template<typename NumFrom, typename... NumsFrom, typename NumTo, typename... Num
constexpr auto num_from_compl = get_complexity(NumFrom{});
constexpr auto num_to_compl = get_complexity(NumTo{});
constexpr auto max_compl = max(num_from_compl, num_to_compl);
if constexpr (max_compl > 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{})),
Expand All @@ -1211,7 +1217,7 @@ template<typename DenFrom, typename... DensFrom, typename DenTo, typename... Den
constexpr auto den_from_compl = get_complexity(DenFrom{});
constexpr auto den_to_compl = get_complexity(DenTo{});
constexpr auto max_compl = max(den_from_compl, den_to_compl);
if constexpr (max_compl > 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{})),
Expand All @@ -1233,7 +1239,7 @@ template<typename NumFrom, typename... NumsFrom, typename DenTo, typename... Den
constexpr auto num_from_compl = get_complexity(NumFrom{});
constexpr auto den_to_compl = get_complexity(DenTo{});
constexpr auto max_compl = max(num_from_compl, den_to_compl);
if constexpr (max_compl > 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{})),
Expand All @@ -1254,7 +1260,7 @@ template<typename DenFrom, typename... DensFrom, typename NumTo, typename... Num
constexpr auto den_from_compl = get_complexity(DenFrom{});
constexpr auto num_to_compl = get_complexity(NumTo{});
constexpr auto max_compl = max(den_from_compl, num_to_compl);
if constexpr (max_compl > 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{})),
Expand Down
34 changes: 19 additions & 15 deletions test/static/quantity_spec_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<get_complexity(inverse(time))>(frequency).quantity == inverse(period_duration));
Expand Down

0 comments on commit 467d9a5

Please sign in to comment.