Skip to content

Commit

Permalink
refactor: unit comparison functions optimized for the case of the sam…
Browse files Browse the repository at this point in the history
…e unit type
  • Loading branch information
mpusz committed Jun 6, 2024
1 parent e38c7c4 commit 3353f49
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/core/include/mp-units/framework/unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,16 @@ MP_UNITS_EXPORT_END

namespace detail {

[[nodiscard]] consteval auto have_same_canonical_reference_unit(Unit auto u1, Unit auto u2)
template<Unit U1, Unit U2>
[[nodiscard]] consteval auto have_same_canonical_reference_unit(U1 u1, U2 u2)
{
using canonical_lhs = decltype(get_canonical_unit(u1));
using canonical_rhs = decltype(get_canonical_unit(u2));
return std::is_same<decltype(canonical_lhs::reference_unit), decltype(canonical_rhs::reference_unit)>{};
if constexpr (is_same_v<U1, U2>)
return std::true_type{};
else {
using canonical_lhs = decltype(get_canonical_unit(u1));
using canonical_rhs = decltype(get_canonical_unit(u2));
return std::is_same<decltype(canonical_lhs::reference_unit), decltype(canonical_rhs::reference_unit)>{};
}
}

} // namespace detail
Expand All @@ -513,8 +518,11 @@ namespace detail {
MP_UNITS_EXPORT template<Unit U1, Unit U2>
[[nodiscard]] consteval bool operator==(U1 lhs, U2 rhs)
{
return decltype(detail::have_same_canonical_reference_unit(lhs, rhs))::value &&
decltype(get_canonical_unit(lhs))::mag == decltype(get_canonical_unit(rhs))::mag;
if constexpr (is_same_v<U1, U2>)
return true;
else
return decltype(detail::have_same_canonical_reference_unit(lhs, rhs))::value &&
decltype(get_canonical_unit(lhs))::mag == decltype(get_canonical_unit(rhs))::mag;
}

namespace detail {
Expand Down Expand Up @@ -603,9 +611,13 @@ inline constexpr auto ppm = parts_per_million;


// convertible_to
[[nodiscard]] consteval bool convertible(Unit auto from, Unit auto to)
template<Unit U1, Unit U2>
[[nodiscard]] consteval bool convertible(U1 from, U2 to)
{
return decltype(detail::have_same_canonical_reference_unit(from, to))::value;
if constexpr (is_same_v<U1, U2>)
return true;
else
return decltype(detail::have_same_canonical_reference_unit(from, to))::value;
}

// Common unit
Expand All @@ -615,7 +627,9 @@ template<Unit U1, Unit U2>
[[nodiscard]] consteval Unit auto common_unit(U1 u1, U2 u2)
requires(decltype(detail::have_same_canonical_reference_unit(u1, u2))::value)
{
if constexpr (U1{} == U2{}) {
if constexpr (is_same_v<U1, U2>)
return u1;
else if constexpr (U1{} == U2{}) {
if constexpr (std::derived_from<U1, typename U2::_base_type_>)
return u1;
else if constexpr (std::derived_from<U2, typename U1::_base_type_>)
Expand Down

0 comments on commit 3353f49

Please sign in to comment.