mirror of
https://github.com/mpusz/mp-units.git
synced 2025-07-31 02:47:16 +02:00
refactor: get_complexity
refactored to be 0-based and not account for a number of arguments in a list
This commit is contained in:
@ -598,9 +598,15 @@ MP_UNITS_EXPORT_END
|
|||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief @c get_complexity specifies how many type explosions can be done on a quantity
|
||||||
|
*/
|
||||||
template<QuantitySpec Q>
|
template<QuantitySpec Q>
|
||||||
[[nodiscard]] consteval int get_complexity(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>
|
template<typename... Ts>
|
||||||
[[nodiscard]] consteval int get_complexity(type_list<Ts...>)
|
[[nodiscard]] consteval int get_complexity(type_list<Ts...>)
|
||||||
{
|
{
|
||||||
@ -627,7 +633,7 @@ template<QuantitySpec Q>
|
|||||||
else if constexpr (requires { Q::_equation_; })
|
else if constexpr (requires { Q::_equation_; })
|
||||||
return 1 + get_complexity(Q::_equation_);
|
return 1 + get_complexity(Q::_equation_);
|
||||||
else
|
else
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// dimension_one is always the last one
|
// dimension_one is always the last one
|
||||||
@ -989,7 +995,7 @@ template<typename NumFrom, typename... NumsFrom, typename DenFrom, typename... D
|
|||||||
constexpr auto num_to_compl = get_complexity(NumTo{});
|
constexpr auto num_to_compl = get_complexity(NumTo{});
|
||||||
constexpr auto den_to_compl = get_complexity(DenTo{});
|
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});
|
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) {
|
if constexpr (num_from_compl == max_compl) {
|
||||||
constexpr auto res = explode_to_equation(NumFrom{});
|
constexpr auto res = explode_to_equation(NumFrom{});
|
||||||
return convertible_impl(
|
return convertible_impl(
|
||||||
@ -1034,7 +1040,7 @@ template<typename DenFrom, typename... DensFrom, typename NumTo, typename... Num
|
|||||||
constexpr auto num_to_compl = get_complexity(NumTo{});
|
constexpr auto num_to_compl = get_complexity(NumTo{});
|
||||||
constexpr auto den_to_compl = get_complexity(DenTo{});
|
constexpr auto den_to_compl = get_complexity(DenTo{});
|
||||||
constexpr auto max_compl = max({num_to_compl, den_from_compl, den_to_compl});
|
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) {
|
if constexpr (den_from_compl == max_compl) {
|
||||||
constexpr auto res = explode_to_equation(DenFrom{});
|
constexpr auto res = explode_to_equation(DenFrom{});
|
||||||
return convertible_impl(
|
return convertible_impl(
|
||||||
@ -1072,7 +1078,7 @@ template<typename NumFrom, typename... NumsFrom, typename NumTo, typename... Num
|
|||||||
constexpr auto num_to_compl = get_complexity(NumTo{});
|
constexpr auto num_to_compl = get_complexity(NumTo{});
|
||||||
constexpr auto den_to_compl = get_complexity(DenTo{});
|
constexpr auto den_to_compl = get_complexity(DenTo{});
|
||||||
constexpr auto max_compl = max({num_from_compl, num_to_compl, den_to_compl});
|
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) {
|
if constexpr (num_from_compl == max_compl) {
|
||||||
constexpr auto res = explode_to_equation(NumFrom{});
|
constexpr auto res = explode_to_equation(NumFrom{});
|
||||||
return convertible_impl(
|
return convertible_impl(
|
||||||
@ -1111,7 +1117,7 @@ template<typename NumFrom, typename... NumsFrom, typename DenFrom, typename... D
|
|||||||
constexpr auto den_from_compl = get_complexity(DenFrom{});
|
constexpr auto den_from_compl = get_complexity(DenFrom{});
|
||||||
constexpr auto den_to_compl = get_complexity(DenTo{});
|
constexpr auto den_to_compl = get_complexity(DenTo{});
|
||||||
constexpr auto max_compl = max({num_from_compl, den_from_compl, den_to_compl});
|
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) {
|
if constexpr (num_from_compl == max_compl) {
|
||||||
constexpr auto res = explode_to_equation(NumFrom{});
|
constexpr auto res = explode_to_equation(NumFrom{});
|
||||||
return convertible_impl(
|
return convertible_impl(
|
||||||
@ -1150,7 +1156,7 @@ template<typename NumFrom, typename... NumsFrom, typename DenFrom, typename... D
|
|||||||
constexpr auto den_from_compl = get_complexity(DenFrom{});
|
constexpr auto den_from_compl = get_complexity(DenFrom{});
|
||||||
constexpr auto num_to_compl = get_complexity(NumTo{});
|
constexpr auto num_to_compl = get_complexity(NumTo{});
|
||||||
constexpr auto max_compl = max({num_from_compl, num_to_compl, den_from_compl});
|
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) {
|
if constexpr (num_from_compl == max_compl) {
|
||||||
constexpr auto res = explode_to_equation(NumFrom{});
|
constexpr auto res = explode_to_equation(NumFrom{});
|
||||||
return convertible_impl(
|
return convertible_impl(
|
||||||
@ -1184,7 +1190,7 @@ template<typename NumFrom, typename... NumsFrom, typename NumTo, typename... Num
|
|||||||
constexpr auto num_from_compl = get_complexity(NumFrom{});
|
constexpr auto num_from_compl = get_complexity(NumFrom{});
|
||||||
constexpr auto num_to_compl = get_complexity(NumTo{});
|
constexpr auto num_to_compl = get_complexity(NumTo{});
|
||||||
constexpr auto max_compl = max(num_from_compl, num_to_compl);
|
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) {
|
if constexpr (num_from_compl == max_compl) {
|
||||||
constexpr auto res = explode_to_equation(NumFrom{});
|
constexpr auto res = explode_to_equation(NumFrom{});
|
||||||
return convertible_impl((res.equation * ... * map_power(NumsFrom{})),
|
return convertible_impl((res.equation * ... * map_power(NumsFrom{})),
|
||||||
@ -1211,7 +1217,7 @@ template<typename DenFrom, typename... DensFrom, typename DenTo, typename... Den
|
|||||||
constexpr auto den_from_compl = get_complexity(DenFrom{});
|
constexpr auto den_from_compl = get_complexity(DenFrom{});
|
||||||
constexpr auto den_to_compl = get_complexity(DenTo{});
|
constexpr auto den_to_compl = get_complexity(DenTo{});
|
||||||
constexpr auto max_compl = max(den_from_compl, den_to_compl);
|
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) {
|
if constexpr (den_from_compl == max_compl) {
|
||||||
constexpr auto res = explode_to_equation(DenFrom{});
|
constexpr auto res = explode_to_equation(DenFrom{});
|
||||||
return convertible_impl(dimensionless / (res.equation * ... * map_power(DensFrom{})),
|
return convertible_impl(dimensionless / (res.equation * ... * map_power(DensFrom{})),
|
||||||
@ -1233,7 +1239,7 @@ template<typename NumFrom, typename... NumsFrom, typename DenTo, typename... Den
|
|||||||
constexpr auto num_from_compl = get_complexity(NumFrom{});
|
constexpr auto num_from_compl = get_complexity(NumFrom{});
|
||||||
constexpr auto den_to_compl = get_complexity(DenTo{});
|
constexpr auto den_to_compl = get_complexity(DenTo{});
|
||||||
constexpr auto max_compl = max(num_from_compl, den_to_compl);
|
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) {
|
if constexpr (num_from_compl == max_compl) {
|
||||||
constexpr auto res = explode_to_equation(NumFrom{});
|
constexpr auto res = explode_to_equation(NumFrom{});
|
||||||
return convertible_impl((res.equation * ... * map_power(NumsFrom{})),
|
return convertible_impl((res.equation * ... * map_power(NumsFrom{})),
|
||||||
@ -1254,7 +1260,7 @@ template<typename DenFrom, typename... DensFrom, typename NumTo, typename... Num
|
|||||||
constexpr auto den_from_compl = get_complexity(DenFrom{});
|
constexpr auto den_from_compl = get_complexity(DenFrom{});
|
||||||
constexpr auto num_to_compl = get_complexity(NumTo{});
|
constexpr auto num_to_compl = get_complexity(NumTo{});
|
||||||
constexpr auto max_compl = max(den_from_compl, num_to_compl);
|
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) {
|
if constexpr (den_from_compl == max_compl) {
|
||||||
constexpr auto res = explode_to_equation(DenFrom{});
|
constexpr auto res = explode_to_equation(DenFrom{});
|
||||||
return convertible_impl(dimensionless / (res.equation * ... * map_power(DensFrom{})),
|
return convertible_impl(dimensionless / (res.equation * ... * map_power(DensFrom{})),
|
||||||
|
@ -442,21 +442,25 @@ static_assert(!defines_equation(mechanical_energy));
|
|||||||
static_assert(!defines_equation(potential_energy));
|
static_assert(!defines_equation(potential_energy));
|
||||||
|
|
||||||
// get_complexity
|
// get_complexity
|
||||||
static_assert(get_complexity(dimensionless) == 1);
|
static_assert(get_complexity(length) == 0);
|
||||||
static_assert(get_complexity(length) == 1);
|
static_assert(get_complexity(pow<4>(length)) == 0);
|
||||||
static_assert(get_complexity(frequency) == 2);
|
static_assert(get_complexity(dimensionless) == 0);
|
||||||
static_assert(get_complexity(area) == 2);
|
static_assert(get_complexity(length / time) == 0);
|
||||||
static_assert(get_complexity(volume) == 2);
|
static_assert(get_complexity(mass * length / time) == 0);
|
||||||
static_assert(get_complexity(speed) == 3);
|
static_assert(get_complexity(frequency) == 1);
|
||||||
static_assert(get_complexity(velocity) == 3);
|
static_assert(get_complexity(area) == 1);
|
||||||
static_assert(get_complexity(acceleration) == 5);
|
static_assert(get_complexity(pow<2>(area)) == 1);
|
||||||
static_assert(get_complexity(force) == 7);
|
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) == 6);
|
static_assert(get_complexity(acceleration * time) == 2);
|
||||||
static_assert(get_complexity(acceleration / time) == 6);
|
static_assert(get_complexity(acceleration / time) == 2);
|
||||||
|
static_assert(get_complexity(speed * area) == 2);
|
||||||
static_assert(get_complexity(pow<4>(length)) == 1);
|
static_assert(get_complexity(speed / frequency) == 2);
|
||||||
static_assert(get_complexity(pow<2>(area)) == 2);
|
static_assert(get_complexity(speed * area / frequency) == 3);
|
||||||
|
|
||||||
// explode
|
// explode
|
||||||
static_assert(explode<get_complexity(inverse(time))>(frequency).quantity == inverse(period_duration));
|
static_assert(explode<get_complexity(inverse(time))>(frequency).quantity == inverse(period_duration));
|
||||||
|
Reference in New Issue
Block a user