Merge branch 'master' of github.com:mpusz/mp-units

This commit is contained in:
Mateusz Pusz committed 2024-01-23 21:15:55 +01:00
commit a9b2865950
26 files changed
+269 -91

No files matched your search

+32 -10
View File
@@ -29,6 +29,7 @@
// IWYU pragma: begin_exports
#include <compare>
#include <cstdlib>
#include <ostream>
// IWYU pragma: end_exports
#include <cstddef>
@@ -56,18 +57,18 @@ struct basic_fixed_string {
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
constexpr explicit(false) basic_fixed_string(CharT ch) noexcept
requires(N == 1)
{
data_[0] = ch;
}
constexpr explicit(false) basic_fixed_string(const CharT (&txt)[N + 1]) noexcept
{
if constexpr (N != 0)
for (std::size_t i = 0; i < N; ++i) data_[i] = txt[i];
}
template<std::convertible_to<CharT>... Rest>
requires(1 + sizeof...(Rest) == N)
constexpr explicit basic_fixed_string(CharT first, Rest... rest) noexcept : data_{first, rest..., CharT('\0')}
{
}
constexpr basic_fixed_string(const CharT* ptr, std::integral_constant<std::size_t, N>) noexcept
{
if constexpr (N != 0)
@@ -78,13 +79,18 @@ struct basic_fixed_string {
[[nodiscard]] constexpr size_type size() const noexcept { return N; }
[[nodiscard]] constexpr const_pointer data() const noexcept { return data_; }
[[nodiscard]] constexpr const CharT* c_str() const noexcept { return data(); }
[[nodiscard]] constexpr const_reference operator[](size_type index) const noexcept { return data()[index]; }
[[nodiscard]] constexpr value_type operator[](size_type index) const noexcept { return data()[index]; }
[[nodiscard]] constexpr const_iterator begin() const noexcept { return data(); }
[[nodiscard]] constexpr const_iterator cbegin() const noexcept { return data(); }
[[nodiscard]] constexpr const_iterator end() const noexcept { return data() + size(); }
[[nodiscard]] constexpr const_iterator cend() const noexcept { return data() + size(); }
[[nodiscard]] constexpr std::basic_string_view<CharT> view() const noexcept
{
return std::basic_string_view<CharT>(cbegin(), cend());
}
template<std::size_t N2>
[[nodiscard]] constexpr friend basic_fixed_string<CharT, N + N2> operator+(
const basic_fixed_string& lhs, const basic_fixed_string<CharT, N2>& rhs) noexcept
@@ -116,14 +122,21 @@ struct basic_fixed_string {
// TODO std::lexicographical_compare_three_way(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
return detail::lexicographical_compare_three_way(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
};
template<typename CharT>
basic_fixed_string(CharT) -> basic_fixed_string<CharT, 1>;
template<typename Traits>
friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
const basic_fixed_string<CharT, N>& str)
{
return os << str.c_str();
}
};
template<typename CharT, std::size_t N>
basic_fixed_string(const CharT (&str)[N]) -> basic_fixed_string<CharT, N - 1>;
template<typename CharT, std::convertible_to<CharT>... Rest>
basic_fixed_string(CharT, Rest...) -> basic_fixed_string<CharT, 1 + sizeof...(Rest)>;
template<typename CharT, std::size_t N>
basic_fixed_string(const CharT* ptr, std::integral_constant<std::size_t, N>) -> basic_fixed_string<CharT, N>;
@@ -131,3 +144,12 @@ template<std::size_t N>
using fixed_string = basic_fixed_string<char, N>;
} // namespace mp_units
template<typename CharT, std::size_t N>
struct MP_UNITS_STD_FMT::formatter<mp_units::basic_fixed_string<CharT, N>> : formatter<std::basic_string_view<CharT>> {
template<typename FormatContext>
auto format(const mp_units::basic_fixed_string<CharT, N>& str, FormatContext& ctx)
{
return formatter<std::basic_string_view<CharT>>::format(str.view(), ctx);
}
};
+30
View File
@@ -83,6 +83,36 @@ template<typename T, template<typename...> typename Type>
// inline constexpr bool // TODO: Replace with concept when it works with MSVC
concept is_derived_from_specialization_of = requires(T* t) { detail::to_base_specialization_of<Type>(t); };
namespace detail {
template<typename T>
struct get_value_type {
using type = MP_UNITS_TYPENAME T::value_type;
};
template<typename T>
struct get_element_type {
using type = std::remove_reference_t<typename T::element_type>;
};
} // namespace detail
template<typename T>
struct underlying_type {
using type = T;
};
template<typename T>
requires requires { typename T::value_type; } || requires { typename T::element_type; }
struct underlying_type<T> {
using type = MP_UNITS_TYPENAME
conditional<requires { typename T::value_type; }, detail::get_value_type<T>, detail::get_element_type<T>>::type;
};
template<typename T>
using underlying_type_t = MP_UNITS_TYPENAME underlying_type<T>::type;
template<typename T, typename... Ts>
concept one_of = (false || ... || std::same_as<T, Ts>);
@@ -52,13 +52,13 @@ template<typename T>
concept Quantity = detail::is_derived_from_specialization_of_quantity<T>;
/**
* @brief A concept matching all quantities with provided dimension or quantity spec
* @brief A concept matching all quantities with provided quantity spec
*
* Satisfied by all quantities with a dimension/quantity_spec being the instantiation derived from
* the provided dimension/quantity_spec type.
* Satisfied by all quantities with a quantity_spec being the instantiation derived from
* the provided quantity_spec type.
*/
template<typename Q, auto V>
concept QuantityOf = Quantity<Q> && ReferenceOf<std::remove_const_t<decltype(Q::reference)>, V>;
template<typename Q, auto QS>
concept QuantityOf = Quantity<Q> && QuantitySpecOf<std::remove_const_t<decltype(Q::quantity_spec)>, QS>;
/**
* @brief A concept matching all external quantities like types
@@ -145,15 +145,15 @@ concept SameAbsolutePointOriginAs =
/**
* @brief A concept matching all quantity points with provided dimension or quantity spec
* @brief A concept matching all quantity points with provided quantity spec
*
* Satisfied by all quantity points with a dimension/quantity_spec being the instantiation derived from
* the provided dimension/quantity_spec type, or quantity points having the origin with the same
* Satisfied by all quantity points with a quantity_spec being the instantiation derived from
* the provided quantity_spec type, or quantity points having the origin with the same
* `absolute_point_origin`.
*/
template<typename QP, auto V>
concept QuantityPointOf =
QuantityPoint<QP> && (ReferenceOf<std::remove_const_t<decltype(QP::reference)>, V> ||
QuantityPoint<QP> && (QuantitySpecOf<std::remove_const_t<decltype(QP::quantity_spec)>, V> ||
detail::SameAbsolutePointOriginAs<std::remove_const_t<decltype(QP::absolute_point_origin)>, V>);
/**
@@ -66,14 +66,12 @@ template<auto Q, auto U>
}
/**
* @brief A concept matching all references with provided dimension or quantity spec
* @brief A concept matching all references with provided quantity spec
*
* Satisfied by all references with a dimension/quantity_spec being the instantiation derived from
* the provided dimension/quantity_spec type.
* Satisfied by all references with a quantity_spec being the instantiation derived from
* the provided quantity_spec type.
*/
template<typename T, auto V>
concept ReferenceOf =
Reference<T> && (DimensionOf<std::remove_const_t<decltype(get_quantity_spec(T{}).dimension)>, V> ||
QuantitySpecOf<std::remove_const_t<decltype(get_quantity_spec(T{}))>, V>);
template<typename T, auto QS>
concept ReferenceOf = Reference<T> && QuantitySpecOf<std::remove_const_t<decltype(get_quantity_spec(T{}))>, QS>;
} // namespace mp_units
+27 -18
View File
@@ -30,17 +30,12 @@
namespace mp_units::detail {
// determines the best available representation type
template<Quantity From, Quantity To>
[[nodiscard]] consteval auto common_rep_type(From, To)
{
if constexpr (requires { typename std::common_type_t<typename From::rep, typename To::rep>; })
// returns a common type of two representation types if available
// e.g. `double` and `int` will end up with `double` precision
return std::common_type_t<typename From::rep, typename To::rep>{};
else
return typename From::rep{};
}
template<typename T, typename Other>
struct get_common_type : std::common_type<T, Other> {};
template<typename T, typename Other>
using maybe_common_type = MP_UNITS_TYPENAME std::conditional_t<requires { typename std::common_type_t<T, Other>; },
get_common_type<T, Other>, std::type_identity<T>>::type;
/**
* @brief Explicit cast between different quantity types
@@ -73,15 +68,29 @@ template<Quantity To, typename From>
constexpr Magnitude auto num = numerator(c_mag);
constexpr Magnitude auto den = denominator(c_mag);
constexpr Magnitude auto irr = c_mag * (den / num);
using c_rep_type = decltype(common_rep_type(q, To{}));
using c_rep_type = maybe_common_type<typename std::remove_reference_t<From>::rep, typename To::rep>;
using c_mag_type = common_magnitude_type<c_mag>;
using multiplier_type =
conditional<treat_as_floating_point<c_rep_type>, std::common_type_t<c_mag_type, long double>, c_mag_type>;
using multiplier_type = conditional<treat_as_floating_point<c_rep_type>,
// ensure that the multiplier is also floating-point
conditional<std::is_arithmetic_v<underlying_type_t<c_rep_type>>,
// reuse user's type if possible
std::common_type_t<c_mag_type, underlying_type_t<c_rep_type>>,
std::common_type_t<c_mag_type, double>>,
c_mag_type>;
using c_type = maybe_common_type<c_rep_type, multiplier_type>;
constexpr auto val = [](Magnitude auto m) { return get_value<multiplier_type>(m); };
return {static_cast<MP_UNITS_TYPENAME To::rep>(
static_cast<c_rep_type>(std::forward<From>(q).numerical_value_is_an_implementation_detail_) * val(num) /
val(den) * val(irr)),
To::reference};
if constexpr (std::is_floating_point_v<multiplier_type>) {
// this results in great assembly
constexpr auto ratio = val(num) / val(den) * val(irr);
auto res = static_cast<MP_UNITS_TYPENAME To::rep>(
static_cast<c_type>(q.numerical_value_is_an_implementation_detail_) * ratio);
return {res, To::reference};
} else {
// this is slower but allows conversions like 2000 m -> 2 km without loosing data
auto res = static_cast<MP_UNITS_TYPENAME To::rep>(
static_cast<c_type>(q.numerical_value_is_an_implementation_detail_) * val(num) / val(den) * val(irr));
return {res, To::reference};
}
}
}
+18
View File
@@ -198,6 +198,24 @@ template<auto R, auto S, auto T, typename Rep1, typename Rep2, typename Rep3>
}
/**
* @brief Computes the floating-point remainder of the division operation x / y.
*/
template<auto R1, typename Rep1, auto R2, typename Rep2>
requires requires(Rep1 v1, Rep2 v2) {
common_reference(R1, R2);
requires requires { fmod(v1, v2); } || requires { std::fmod(v1, v2); };
}
[[nodiscard]] constexpr QuantityOf<get_quantity_spec(R1)> auto fmod(const quantity<R1, Rep1>& x,
const quantity<R2, Rep2>& y) noexcept
{
constexpr auto ref = common_reference(R1, R2);
constexpr auto unit = get_unit(ref);
using std::fmod;
return quantity{fmod(x.numerical_value_in(unit), y.numerical_value_in(unit)), ref};
}
/**
* @brief Returns the epsilon of the quantity
*
+2 -2
View File
@@ -362,7 +362,7 @@ public:
return std::forward<Q>(lhs);
}
template<typename Q1, QuantityOf<dimension_one> Q2>
template<typename Q1, QuantityOf<dimensionless> Q2>
requires std::derived_from<std::remove_cvref_t<Q1>, quantity> && (Q2::unit == ::mp_units::one) &&
requires(rep a, const typename Q2::rep b) {
{
@@ -389,7 +389,7 @@ public:
return std::forward<Q>(lhs);
}
template<typename Q1, QuantityOf<dimension_one> Q2>
template<typename Q1, QuantityOf<dimensionless> Q2>
requires std::derived_from<std::remove_cvref_t<Q1>, quantity> && (Q2::unit == ::mp_units::one) &&
requires(rep a, const typename Q2::rep b) {
{
+10 -9
View File
@@ -411,7 +411,11 @@ struct quantity_spec<Self, QS, Eq, Args...> : quantity_spec<Self, QS, Args...> {
*/
template<detail::IntermediateDerivedQuantitySpecExpr... Expr>
struct derived_quantity_spec :
#ifdef __cpp_explicit_this_parameter
detail::quantity_spec_interface,
#else
detail::quantity_spec_interface<derived_quantity_spec<Expr...>>,
#endif
detail::expr_fractions<detail::is_dimensionless, Expr...> {
using _base_ = detail::expr_fractions<detail::is_dimensionless, Expr...>;
@@ -443,9 +447,9 @@ concept QuantitySpecWithNoSpecifiers = detail::NamedQuantitySpec<T> || detail::I
} // namespace detail
#ifdef __cpp_explicit_this_parameter
template<detail::QuantitySpecWithNoSpecifiers auto Q>
requires(get_kind(Q) == Q)
struct kind_of_<Q> : Q {
template<auto Q>
requires(detail::QuantitySpecWithNoSpecifiers<std::remove_const_t<decltype(Q)>>) && (get_kind(Q) == Q)
struct kind_of_<Q> : std::remove_const_t<decltype(Q)> {
static constexpr auto _quantity_spec_ = Q;
};
#else
@@ -1416,12 +1420,9 @@ template<typename Self, NamedQuantitySpec auto QS, auto... Args>
template<QuantitySpec Q>
[[nodiscard]] consteval auto remove_kind(Q q)
{
if constexpr (detail::QuantityKindSpec<Q>) {
if constexpr (requires { Q::_parent_; })
return Q::_parent_;
else
return Q::_equation_;
} else
if constexpr (detail::QuantityKindSpec<Q>)
return Q::_quantity_spec_;
else
return q;
}
+7
View File
@@ -605,6 +605,8 @@ template<std::intmax_t Num, std::intmax_t Den = 1, Unit U>
// clang-format off
inline constexpr struct percent : named_unit<"%", mag<ratio{1, 100}> * one> {} percent;
inline constexpr struct per_mille : named_unit<basic_symbol_text{"‰", "%o"}, mag<ratio(1, 1000)> * one> {} per_mille;
inline constexpr struct parts_per_million : named_unit<"ppm", mag<ratio(1, 1'000'000)> * one> {} parts_per_million;
inline constexpr auto ppm = parts_per_million;
// clang-format on
@@ -836,6 +838,11 @@ constexpr Out unit_symbol_impl(Out out, const derived_unit<Expr...>&, unit_symbo
template<Unit auto U>
inline constexpr bool space_before_unit_symbol = true;
template<>
inline constexpr bool space_before_unit_symbol<percent> = false;
template<>
inline constexpr bool space_before_unit_symbol<per_mille> = false;
template<typename CharT = char, std::output_iterator<CharT> Out, Unit U>
constexpr Out unit_symbol_to(Out out, U u, unit_symbol_formatting fmt = unit_symbol_formatting{})
{
@@ -119,4 +119,24 @@ template<ReferenceOf<dimensionless> auto R, typename Rep>
return quantity{atan(q.numerical_value_in(one)), radian};
}
template<auto R1, typename Rep1, auto R2, typename Rep2>
requires requires(Rep1 v1, Rep2 v2) {
common_reference(R1, R2);
requires requires { atan2(v1, v2); } || requires { std::atan2(v1, v2); };
}
[[nodiscard]] inline QuantityOf<angle> auto atan2(const quantity<R1, Rep1>& y, const quantity<R2, Rep2>& x) noexcept
{
constexpr auto ref = common_reference(R1, R2);
constexpr auto unit = get_unit(ref);
using std::atan2;
if constexpr (!treat_as_floating_point<Rep1> || !treat_as_floating_point<Rep2>) {
// check what is the return type when called with the integral value
using rep = decltype(atan2(y.force_numerical_value_in(unit), x.force_numerical_value_in(unit)));
// use this type ahead of calling the function to prevent narrowing if a unit conversion is needed
return quantity{atan2(value_cast<rep>(y).numerical_value_in(unit), value_cast<rep>(x).numerical_value_in(unit)),
radian};
} else
return quantity{atan2(y.numerical_value_in(unit), x.numerical_value_in(unit)), radian};
}
} // namespace mp_units::angular
@@ -120,4 +120,25 @@ template<ReferenceOf<dimensionless> auto R, typename Rep>
return quantity{atan(q.numerical_value_in(one)), radian};
}
template<auto R1, typename Rep1, auto R2, typename Rep2>
requires requires(Rep1 v1, Rep2 v2) {
common_reference(R1, R2);
requires requires { atan2(v1, v2); } || requires { std::atan2(v1, v2); };
}
[[nodiscard]] inline QuantityOf<isq::angular_measure> auto atan2(const quantity<R1, Rep1>& y,
const quantity<R2, Rep2>& x) noexcept
{
constexpr auto ref = common_reference(R1, R2);
constexpr auto unit = get_unit(ref);
using std::atan2;
if constexpr (!treat_as_floating_point<Rep1> || !treat_as_floating_point<Rep2>) {
// check what is the return type when called with the integral value
using rep = decltype(atan2(y.force_numerical_value_in(unit), x.force_numerical_value_in(unit)));
// use this type ahead of calling the function to prevent narrowing if a unit conversion is needed
return quantity{atan2(value_cast<rep>(y).numerical_value_in(unit), value_cast<rep>(x).numerical_value_in(unit)),
radian};
} else
return quantity{atan2(y.numerical_value_in(unit), x.numerical_value_in(unit)), radian};
}
} // namespace mp_units::si