forked from mpusz/mp-units
feat: expr_pow
extended to remove redundancy in callers
This commit is contained in:
@@ -204,14 +204,8 @@ template<std::intmax_t Num, std::intmax_t Den = 1, Dimension D>
|
|||||||
requires detail::non_zero<Den>
|
requires detail::non_zero<Den>
|
||||||
[[nodiscard]] consteval Dimension auto pow(D d)
|
[[nodiscard]] consteval Dimension auto pow(D d)
|
||||||
{
|
{
|
||||||
if constexpr (detail::BaseDimension<D>) {
|
return detail::expr_pow<Num, Den, derived_dimension, struct dimension_one, detail::type_list_of_base_dimension_less>(
|
||||||
if constexpr (Den == 1)
|
d);
|
||||||
return derived_dimension<power<D, Num>>{};
|
|
||||||
else
|
|
||||||
return derived_dimension<power<D, Num, Den>>{};
|
|
||||||
} else
|
|
||||||
return detail::expr_pow<Num, Den, derived_dimension, struct dimension_one,
|
|
||||||
detail::type_list_of_base_dimension_less>(d);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -475,9 +475,18 @@ template<std::intmax_t Num, std::intmax_t Den, template<typename...> typename To
|
|||||||
template<std::intmax_t Num, std::intmax_t Den, template<typename...> typename To, typename OneType,
|
template<std::intmax_t Num, std::intmax_t Den, template<typename...> typename To, typename OneType,
|
||||||
template<typename, typename> typename Pred, typename T>
|
template<typename, typename> typename Pred, typename T>
|
||||||
requires detail::non_zero<Den>
|
requires detail::non_zero<Den>
|
||||||
[[nodiscard]] consteval auto expr_pow(T)
|
[[nodiscard]] consteval auto expr_pow(T v)
|
||||||
{
|
{
|
||||||
|
if constexpr (Num == 0 || is_same_v<T, OneType>)
|
||||||
|
return OneType{};
|
||||||
|
else if constexpr (detail::ratio{Num, Den} == 1)
|
||||||
|
return v;
|
||||||
|
else if constexpr (is_specialization_of<T, To>)
|
||||||
return expr_pow_impl<Num, Den, To, OneType, Pred>(typename T::_num_{}, typename T::_den_{});
|
return expr_pow_impl<Num, Den, To, OneType, Pred>(typename T::_num_{}, typename T::_den_{});
|
||||||
|
else if constexpr (Den == 1)
|
||||||
|
return To<power<T, Num>>{};
|
||||||
|
else
|
||||||
|
return To<power<T, Num, Den>>{};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -590,18 +590,9 @@ template<std::intmax_t Num, std::intmax_t Den = 1, QuantitySpec Q>
|
|||||||
requires detail::non_zero<Den>
|
requires detail::non_zero<Den>
|
||||||
[[nodiscard]] consteval QuantitySpec auto pow(Q q)
|
[[nodiscard]] consteval QuantitySpec auto pow(Q q)
|
||||||
{
|
{
|
||||||
if constexpr (Num == 0 || Q{} == dimensionless)
|
|
||||||
return dimensionless;
|
|
||||||
else if constexpr (detail::ratio{Num, Den} == 1)
|
|
||||||
return q;
|
|
||||||
else if constexpr (detail::DerivedQuantitySpec<Q>)
|
|
||||||
return detail::clone_kind_of<Q{}>(
|
return detail::clone_kind_of<Q{}>(
|
||||||
detail::expr_pow<Num, Den, derived_quantity_spec, struct dimensionless, detail::type_list_of_quantity_spec_less>(
|
detail::expr_pow<Num, Den, derived_quantity_spec, struct dimensionless, detail::type_list_of_quantity_spec_less>(
|
||||||
detail::remove_kind(q)));
|
detail::remove_kind(q)));
|
||||||
else if constexpr (Den == 1)
|
|
||||||
return detail::clone_kind_of<Q{}>(derived_quantity_spec<power<decltype(detail::remove_kind(Q{})), Num>>{});
|
|
||||||
else
|
|
||||||
return detail::clone_kind_of<Q{}>(derived_quantity_spec<power<decltype(detail::remove_kind(Q{})), Num, Den>>{});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -26,7 +26,6 @@
|
|||||||
#include <mp-units/bits/get_associated_quantity.h>
|
#include <mp-units/bits/get_associated_quantity.h>
|
||||||
#include <mp-units/bits/hacks.h>
|
#include <mp-units/bits/hacks.h>
|
||||||
#include <mp-units/bits/module_macros.h>
|
#include <mp-units/bits/module_macros.h>
|
||||||
#include <mp-units/bits/ratio.h>
|
|
||||||
#include <mp-units/bits/text_tools.h>
|
#include <mp-units/bits/text_tools.h>
|
||||||
#include <mp-units/compat_macros.h>
|
#include <mp-units/compat_macros.h>
|
||||||
#include <mp-units/ext/algorithm.h>
|
#include <mp-units/ext/algorithm.h>
|
||||||
@@ -608,16 +607,7 @@ template<std::intmax_t Num, std::intmax_t Den = 1, Unit U>
|
|||||||
requires detail::non_zero<Den>
|
requires detail::non_zero<Den>
|
||||||
[[nodiscard]] consteval Unit auto pow(U u)
|
[[nodiscard]] consteval Unit auto pow(U u)
|
||||||
{
|
{
|
||||||
if constexpr (Num == 0 || is_same_v<U, struct one>)
|
|
||||||
return one;
|
|
||||||
else if constexpr (detail::ratio{Num, Den} == 1)
|
|
||||||
return u;
|
|
||||||
else if constexpr (is_specialization_of<U, derived_unit>)
|
|
||||||
return detail::expr_pow<Num, Den, derived_unit, struct one, detail::type_list_of_unit_less>(u);
|
return detail::expr_pow<Num, Den, derived_unit, struct one, detail::type_list_of_unit_less>(u);
|
||||||
else if constexpr (Den == 1)
|
|
||||||
return derived_unit<power<U, Num>>{};
|
|
||||||
else
|
|
||||||
return derived_unit<power<U, Num, Den>>{};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user