Initial version

This commit is contained in:
Mateusz Pusz
2018-08-22 12:11:19 +02:00
commit 4b376e3c2e
25 changed files with 1927 additions and 0 deletions
+84
View File
@@ -0,0 +1,84 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include <ratio>
#include <type_traits>
namespace mp {
// Requires
template<bool B>
using Requires = std::enable_if_t<B, bool>;
// static_sign
template<std::intmax_t Pn>
struct static_sign : std::integral_constant<std::intmax_t, (Pn < 0) ? -1 : 1> {
};
// static_abs
template<std::intmax_t Pn>
struct static_abs : std::integral_constant<std::intmax_t, Pn * static_sign<Pn>::value> {
};
// static_gcd
template<std::intmax_t Pn, std::intmax_t Qn>
struct static_gcd : static_gcd<Qn, (Pn % Qn)> {
};
template<std::intmax_t Pn>
struct static_gcd<Pn, 0> : std::integral_constant<std::intmax_t, static_abs<Pn>::value> {
};
template<std::intmax_t Qn>
struct static_gcd<0, Qn> : std::integral_constant<std::intmax_t, static_abs<Qn>::value> {
};
// is_ratio
template<typename T>
struct is_ratio : std::false_type {
};
template<intmax_t Num, intmax_t Den>
struct is_ratio<std::ratio<Num, Den>> : std::true_type {
};
// common_ratio
// todo: simplified
template<typename Ratio1, typename Ratio2>
struct common_ratio {
using gcd_num = static_gcd<Ratio1::num, Ratio2::num>;
using gcd_den = static_gcd<Ratio1::den, Ratio2::den>;
using type = std::ratio<gcd_num::value, (Ratio1::den / gcd_den::value) * Ratio2::den>;
};
template<typename Ratio1, typename Ratio2>
using common_ratio_t = typename common_ratio<Ratio1, Ratio2>::type;
} // namespace mp
+160
View File
@@ -0,0 +1,160 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include "tools.h"
#include <type_traits>
namespace mp {
// type_list
template<typename... Types>
struct type_list;
// push_front
template<typename TypeList, typename... Types>
struct type_list_push_front;
template<typename... OldTypes, typename... NewTypes>
struct type_list_push_front<type_list<OldTypes...>, NewTypes...> {
using type = type_list<NewTypes..., OldTypes...>;
};
template<typename TypeList, typename... Types>
using type_list_push_front_t = typename type_list_push_front<TypeList, Types...>::type;
// push_back
template<typename TypeList, typename... Types>
struct type_list_push_back;
template<typename... OldTypes, typename... NewTypes>
struct type_list_push_back<type_list<OldTypes...>, NewTypes...> {
using type = type_list<OldTypes..., NewTypes...>;
};
template<typename TypeList, typename... Types>
using type_list_push_back_t = typename type_list_push_back<TypeList, Types...>::type;
// split
namespace detail {
template<std::size_t Idx, std::size_t N, typename... Types>
struct split_impl;
template<std::size_t Idx, std::size_t N>
struct split_impl<Idx, N> {
using first_list = type_list<>;
using second_list = type_list<>;
};
template<std::size_t Idx, std::size_t N, typename T, typename... Rest>
struct split_impl<Idx, N, T, Rest...> : split_impl<Idx + 1, N, Rest...> {
using base = split_impl<Idx + 1, N, Rest...>;
using base_first = typename base::first_list;
using base_second = typename base::second_list;
using first_list = std::conditional_t < Idx<N, type_list_push_front_t<base_first, T>, base_first>;
using second_list = std::conditional_t < Idx<N, base_second, type_list_push_front_t<base_second, T>>;
};
} // namespace detail
template<typename T, std::size_t N>
struct type_list_split;
template<std::size_t N, typename... Types>
struct type_list_split<type_list<Types...>, N> {
using split = detail::split_impl<0, N, Types...>;
using first_list = typename split::first_list;
using second_list = typename split::second_list;
};
// split_half
template<typename T>
struct type_list_split_half;
template<typename... Types>
struct type_list_split_half<type_list<Types...>> : type_list_split<type_list<Types...>, (sizeof...(Types) + 1) / 2> {
};
// merge_sorted
template<typename T1, typename T2, template<typename, typename> typename Pred>
struct type_list_merge_sorted;
template<typename T1, typename T2, template<typename, typename> typename Pred>
using type_list_merge_sorted_t = typename type_list_merge_sorted<T1, T2, Pred>::type;
template<typename... Lhs, template<typename, typename> typename Pred>
struct type_list_merge_sorted<type_list<Lhs...>, type_list<>, Pred> {
using type = type_list<Lhs...>;
};
template<typename... Rhs, template<typename, typename> typename Pred>
struct type_list_merge_sorted<type_list<>, type_list<Rhs...>, Pred> {
using type = type_list<Rhs...>;
};
template<typename Lhs1, typename... LhsRest, typename Rhs1, typename... RhsRest,
template<typename, typename> typename Pred>
struct type_list_merge_sorted<type_list<Lhs1, LhsRest...>, type_list<Rhs1, RhsRest...>, Pred> {
using type = std::conditional_t<
Pred<Lhs1, Rhs1>::value,
type_list_push_front_t<type_list_merge_sorted_t<type_list<LhsRest...>, type_list<Rhs1, RhsRest...>, Pred>,
Lhs1>,
type_list_push_front_t<type_list_merge_sorted_t<type_list<Lhs1, LhsRest...>, type_list<RhsRest...>, Pred>,
Rhs1>>;
};
// sort
template<typename T, template<typename, typename> typename Pred>
struct type_list_sort;
template<template<typename, typename> typename Pred>
struct type_list_sort<type_list<>, Pred> {
using type = type_list<>;
};
template<typename T, template<typename, typename> typename Pred>
struct type_list_sort<type_list<T>, Pred> {
using type = type_list<T>;
};
template<typename... Types, template<typename, typename> typename Pred>
struct type_list_sort<type_list<Types...>, Pred> {
using types = type_list<Types...>;
using split = type_list_split_half<type_list<Types...>>;
using left = typename type_list_sort<typename split::first_list, Pred>::type;
using right = typename type_list_sort<typename split::second_list, Pred>::type;
using type = type_list_merge_sorted_t<left, right, Pred>;
};
template<typename T, template<typename, typename> typename Pred>
using type_list_sort_t = typename type_list_sort<T, Pred>::type;
} // namespace mp
+149
View File
@@ -0,0 +1,149 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include "bits/tools.h"
#include "bits/type_list.h"
namespace units {
// dim_id
template<int UniqueValue>
using dim_id = std::integral_constant<int, UniqueValue>;
// dim_id_less
template<typename D1, typename D2>
struct dim_id_less : std::bool_constant<D1::value<D2::value> {
};
// exp
template<typename BaseDimension, int Value>
struct exp {
using dimension = BaseDimension;
static constexpr int value = Value;
};
// exp_less
template<typename E1, typename E2>
struct exp_less : dim_id_less<typename E1::dimension, typename E2::dimension> {
};
// exp_invert
template<typename Exponent>
struct exp_invert;
template<typename BaseDimension, int Value>
struct exp_invert<exp<BaseDimension, Value>> {
using type = exp<BaseDimension, -Value>;
};
template<typename Exponent>
using exp_invert_t = typename exp_invert<Exponent>::type;
// dimension
template<typename... Exponents>
using dimension = mp::type_list<Exponents...>;
// make_dimension
namespace detail {
struct scalar;
template<typename D>
struct dim_consolidate;
template<typename D>
using dim_consolidate_t = typename dim_consolidate<D>::type;
template<>
struct dim_consolidate<dimension<>> {
using type = dimension<scalar>;
};
template<typename E1>
struct dim_consolidate<dimension<E1>> {
using type = dimension<E1>;
};
template<typename... ERest>
struct dim_consolidate<dimension<scalar, ERest...>> {
using type = dim_consolidate_t<dimension<ERest...>>;
};
template<typename D, int V1, int V2, typename... ERest>
struct dim_consolidate<dimension<exp<D, V1>, exp<D, V2>, ERest...>> {
using type = std::conditional_t<V1 + V2 == 0, dim_consolidate_t<dimension<ERest...>>,
dim_consolidate_t<dimension<exp<D, V1 + V2>, ERest...>>>;
};
template<typename E1, typename... ERest>
struct dim_consolidate<dimension<E1, ERest...>> {
using rest = dim_consolidate_t<dimension<ERest...>>;
using type = std::conditional_t<std::is_same_v<rest, dimension<scalar>>, dimension<E1>,
mp::type_list_push_front_t<rest, E1>>;
};
} // namespace detail
template<typename... Exponents>
struct make_dimension {
using type = detail::dim_consolidate_t<mp::type_list_sort_t<mp::type_list<Exponents...>, exp_less>>;
};
template<typename... Exponents>
using make_dimension_t = typename make_dimension<Exponents...>::type;
// dimension_multiply
template<typename D1, typename D2>
struct dimension_multiply;
template<typename... Exp1, typename... Exp2>
struct dimension_multiply<dimension<Exp1...>, dimension<Exp2...>> {
using type = make_dimension_t<Exp1..., Exp2...>;
};
template<typename D1, typename D2>
using dimension_multiply_t = typename dimension_multiply<D1, D2>::type;
// dimension_divide
template<typename D1, typename D2>
struct dimension_divide;
template<typename... Exp1, typename... Exp2>
struct dimension_divide<dimension<Exp1...>, dimension<Exp2...>>
: dimension_multiply<dimension<Exp1...>, dimension<exp_invert_t<Exp2>...>> {
};
template<typename D1, typename D2>
using dimension_divide_t = typename dimension_divide<D1, D2>::type;
} // namespace units
+368
View File
@@ -0,0 +1,368 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include "bits/tools.h"
#include "dimension.h"
#include <limits>
namespace units {
template<typename Dimension, typename Rep, class Ratio>
class quantity;
// is_quantity
namespace detail {
template<typename T>
struct is_quantity : std::false_type {
};
template<typename Dimension, typename Rep, class Ratio>
struct is_quantity<quantity<Dimension, Rep, Ratio>> : std::true_type {
};
}
// treat_as_floating_point
template<class Rep>
struct treat_as_floating_point : std::is_floating_point<Rep> {};
template<class Rep>
inline constexpr bool treat_as_floating_point_v = treat_as_floating_point<Rep>::value;
// quantity_cast
namespace detail {
template<typename To, typename CRatio, typename CRep, bool NumIsOne = false, bool DenIsOne = false>
struct quantity_cast_impl {
template<typename Dimension, typename Rep, typename Ratio>
static constexpr To cast(const quantity<Dimension, Rep, Ratio>& q)
{
return To(static_cast<typename To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(CRatio::num) /
static_cast<CRep>(CRatio::den)));
}
};
template<typename To, typename CRatio, typename CRep>
struct quantity_cast_impl<To, CRatio, CRep, true, true> {
template<typename Dimension, typename Rep, typename Ratio>
static constexpr To cast(const quantity<Dimension, Rep, Ratio>& q)
{
return To(static_cast<typename To::rep>(q.count()));
}
};
template<typename To, typename CRatio, typename CRep>
struct quantity_cast_impl<To, CRatio, CRep, true, false> {
template<typename Dimension, typename Rep, typename Ratio>
static constexpr To cast(const quantity<Dimension, Rep, Ratio>& q)
{
return To(static_cast<typename To::rep>(static_cast<CRep>(q.count()) / static_cast<CRep>(CRatio::den)));
}
};
template<typename To, typename CRatio, typename CRep>
struct quantity_cast_impl<To, CRatio, CRep, false, true> {
template<typename Dimension, typename Rep, typename Ratio>
static constexpr To cast(const quantity<Dimension, Rep, Ratio>& q)
{
return To(static_cast<typename To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(CRatio::num)));
}
};
} // namespace detail
template<typename To, typename Dimension, typename Rep, typename Ratio,
mp::Requires<detail::is_quantity<To>::value && std::is_same_v<typename To::dimension, Dimension>> = true>
constexpr To quantity_cast(const quantity<Dimension, Rep, Ratio>& q)
{
using c_ratio = std::ratio_divide<Ratio, typename To::ratio>;
using c_rep = std::common_type_t<typename To::rep, Rep, intmax_t>;
using cast = detail::quantity_cast_impl<To, c_ratio, c_rep, c_ratio::num == 1, c_ratio::den == 1>;
return cast::cast(q);
}
// quantity_values
template<typename Rep>
struct quantity_values {
static constexpr Rep zero() { return Rep(0); }
static constexpr Rep max() { return std::numeric_limits<Rep>::max(); }
static constexpr Rep min() { return std::numeric_limits<Rep>::lowest(); }
};
// quantity
template<typename Dimension, typename Rep, class Ratio = std::ratio<1>>
class quantity {
Rep value_;
public:
using dimension = Dimension;
using rep = Rep;
using ratio = Ratio;
static_assert(!detail::is_quantity<Rep>::value, "rep cannot be a quantity");
static_assert(mp::is_ratio<ratio>::value, "ratio must be a specialization of std::ratio");
static_assert(ratio::num > 0, "ratio must be positive");
quantity() = default;
quantity(const quantity&) = default;
template<class Rep2, mp::Requires<std::is_convertible_v<Rep2, rep> &&
(treat_as_floating_point_v<rep> || !treat_as_floating_point_v<Rep2>)> = true>
constexpr explicit quantity(const Rep2& r) : value_{static_cast<rep>(r)}
{
}
template<class Rep2, class Ratio2,
mp::Requires<treat_as_floating_point_v<rep> ||
(std::ratio_divide<Ratio2, ratio>::den == 1 && !treat_as_floating_point<Rep2>::value)> = true>
constexpr quantity(const quantity<Dimension, Rep2, Ratio2>& q) : value_{quantity_cast<quantity>(q).count()}
{
}
quantity& operator=(const quantity& other) = default;
constexpr rep count() const noexcept { return value_; }
static constexpr quantity zero() { return quantity(quantity_values<Rep>::zero()); }
static constexpr quantity min() { return quantity(quantity_values<Rep>::min()); }
static constexpr quantity max() { return quantity(quantity_values<Rep>::max()); }
constexpr std::common_type_t<quantity> operator+() const { return quantity(*this); }
constexpr std::common_type_t<quantity> operator-() const { return quantity(-count()); }
constexpr quantity& operator++()
{
++value_;
return *this;
}
constexpr quantity operator++(int) { return quantity(value_++); }
constexpr quantity& operator--()
{
--value_;
return *this;
}
constexpr quantity operator--(int) { return quantity(value_--); }
constexpr quantity& operator+=(const quantity& q)
{
value_ += q.count();
return *this;
}
constexpr quantity& operator-=(const quantity& q)
{
value_ -= q.count();
return *this;
}
constexpr quantity& operator*=(const rep& rhs)
{
value_ *= rhs;
return *this;
}
constexpr quantity& operator/=(const rep& rhs)
{
value_ /= rhs;
return *this;
}
constexpr quantity& operator%=(const rep& rhs)
{
value_ %= rhs;
return *this;
}
constexpr quantity& operator%=(const quantity& q)
{
value_ %= q.count();
return *this;
}
};
// clang-format off
template<typename Dimension, typename Rep1, class Ratio1, typename Rep2, class Ratio2>
std::common_type_t<quantity<Dimension, Rep1, Ratio1>, quantity<Dimension, Rep2, Ratio2>>
constexpr operator+(const quantity<Dimension, Rep1, Ratio1>& lhs,
const quantity<Dimension, Rep2, Ratio2>& rhs)
{
using ret = std::common_type_t<quantity<Dimension, Rep1, Ratio1>, quantity<Dimension, Rep2, Ratio2>>;
return ret(ret(lhs).count() + ret(rhs).count());
}
template<typename Dimension, typename Rep1, class Ratio1, typename Rep2, class Ratio2>
std::common_type_t<quantity<Dimension, Rep1, Ratio1>, quantity<Dimension, Rep2, Ratio2>>
constexpr operator-(const quantity<Dimension, Rep1, Ratio1>& lhs,
const quantity<Dimension, Rep2, Ratio2>& rhs)
{
using ret = std::common_type_t<quantity<Dimension, Rep1, Ratio1>, quantity<Dimension, Rep2, Ratio2>>;
return ret(ret(lhs).count() - ret(rhs).count());
}
template<typename Dimension, typename Rep1, class Ratio, typename Rep2>
quantity<Dimension, std::common_type_t<Rep1, Rep2>, Ratio>
constexpr operator*(const quantity<Dimension, Rep1, Ratio>& q,
const Rep2& v)
{
using ret = quantity<Dimension, std::common_type_t<Rep1, Rep2>, Ratio>;
return ret(ret(q).count() * v);
}
template<typename Dimension, typename Rep1, typename Rep2, class Ratio>
quantity<Dimension, std::common_type_t<Rep1, Rep2>, Ratio>
constexpr operator*(const Rep1& v,
const quantity<Dimension, Rep2, Ratio>& q)
{
return q * v;
}
template<typename Dimension1, typename Rep1, class Ratio1, typename Dimension2, typename Rep2, class Ratio2,
mp::Requires<treat_as_floating_point_v<std::common_type_t<Rep1, Rep2>> || std::ratio_multiply<Ratio1, Ratio2>::den == 1> = true>
quantity<dimension_multiply_t<Dimension1, Dimension2>, std::common_type_t<Rep1, Rep2>, std::ratio_multiply<Ratio1, Ratio2>>
constexpr operator*(const quantity<Dimension1, Rep1, Ratio1>& lhs,
const quantity<Dimension2, Rep2, Ratio2>& rhs)
{
using ret = quantity<dimension_multiply_t<Dimension1, Dimension2>, std::common_type_t<Rep1, Rep2>, std::ratio_multiply<Ratio1, Ratio2>>;
return ret(lhs.count() * rhs.count());
}
template<typename Rep1, typename Exponent, class Ratio, typename Rep2>
quantity<dimension<exp_invert_t<Exponent>>, std::common_type_t<Rep1, Rep2>, Ratio>
constexpr operator/(const Rep1& v,
const quantity<dimension<Exponent>, Rep2, Ratio>& q)
{
using ret = quantity<dimension<exp_invert_t<Exponent>>, std::common_type_t<Rep1, Rep2>, Ratio>;
using den = quantity<dimension<Exponent>, std::common_type_t<Rep1, Rep2>, Ratio>;
return ret(v / den(q).count());
}
template<typename Dimension, typename Rep1, class Ratio, typename Rep2>
quantity<Dimension, std::common_type_t<Rep1, Rep2>, Ratio>
constexpr operator/(const quantity<Dimension, Rep1, Ratio>& q,
const Rep2& v)
{
using ret = quantity<Dimension, std::common_type_t<Rep1, Rep2>, Ratio>;
return ret(ret(q).count() / v);
}
template<typename Dimension, typename Rep1, class Ratio1, typename Rep2, class Ratio2>
std::common_type_t<Rep1, Rep2>
constexpr operator/(const quantity<Dimension, Rep1, Ratio1>& lhs,
const quantity<Dimension, Rep2, Ratio2>& rhs)
{
using cq = std::common_type_t<quantity<Dimension, Rep1, Ratio1>, quantity<Dimension, Rep2, Ratio2>>;
return cq(lhs).count() / cq(rhs).count();
}
template<typename Dimension1, typename Rep1, class Ratio1, typename Dimension2, typename Rep2, class Ratio2,
mp::Requires<treat_as_floating_point_v<std::common_type_t<Rep1, Rep2>> || std::ratio_divide<Ratio1, Ratio2>::den == 1> = true>
quantity<dimension_divide_t<Dimension1, Dimension2>, std::common_type_t<Rep1, Rep2>, std::ratio_divide<Ratio1, Ratio2>>
constexpr operator/(const quantity<Dimension1, Rep1, Ratio1>& lhs,
const quantity<Dimension2, Rep2, Ratio2>& rhs)
{
using ret = quantity<dimension_divide_t<Dimension1, Dimension2>, std::common_type_t<Rep1, Rep2>, std::ratio_divide<Ratio1, Ratio2>>;
return ret(lhs.count() / rhs.count());
}
template<typename Dimension, typename Rep1, class Ratio, typename Rep2>
quantity<Dimension, std::common_type_t<Rep1, Rep2>, Ratio>
constexpr operator%(const quantity<Dimension, Rep1, Ratio>& q,
const Rep2& v)
{
using ret = quantity<Dimension, std::common_type_t<Rep1, Rep2>, Ratio>;
return ret(ret(q).count() % v);
}
template<typename Dimension, typename Rep1, class Ratio1, typename Rep2, class Ratio2>
std::common_type_t<quantity<Dimension, Rep1, Ratio1>, quantity<Dimension, Rep2, Ratio2>>
constexpr operator%(const quantity<Dimension, Rep1, Ratio1>& lhs,
const quantity<Dimension, Rep2, Ratio2>& rhs)
{
using ret = std::common_type_t<quantity<Dimension, Rep1, Ratio1>, quantity<Dimension, Rep2, Ratio2>>;
return ret(ret(lhs).count() % ret(rhs).count());
}
template<typename Dimension, typename Rep1, class Ratio1, typename Rep2, class Ratio2>
constexpr bool operator==(const quantity<Dimension, Rep1, Ratio1>& lhs,
const quantity<Dimension, Rep2, Ratio2>& rhs)
{
using ct = std::common_type_t<quantity<Dimension, Rep1, Ratio1>, quantity<Dimension, Rep2, Ratio2>>;
return ct(lhs).count() == ct(rhs).count();
}
template<typename Dimension, typename Rep1, class Ratio1, typename Rep2, class Ratio2>
constexpr bool operator!=(const quantity<Dimension, Rep1, Ratio1>& lhs,
const quantity<Dimension, Rep2, Ratio2>& rhs)
{
return !(lhs == rhs);
}
template<typename Dimension, typename Rep1, class Ratio1, typename Rep2, class Ratio2>
constexpr bool operator<(const quantity<Dimension, Rep1, Ratio1>& lhs,
const quantity<Dimension, Rep2, Ratio2>& rhs)
{
using ct = std::common_type_t<quantity<Dimension, Rep1, Ratio1>, quantity<Dimension, Rep2, Ratio2>>;
return ct(lhs).count() < ct(rhs).count();
}
template<typename Dimension, typename Rep1, class Ratio1, typename Rep2, class Ratio2>
constexpr bool operator<=(const quantity<Dimension, Rep1, Ratio1>& lhs,
const quantity<Dimension, Rep2, Ratio2>& rhs)
{
return !(rhs < lhs);
}
template<typename Dimension, typename Rep1, class Ratio1, typename Rep2, class Ratio2>
constexpr bool operator>(const quantity<Dimension, Rep1, Ratio1>& lhs,
const quantity<Dimension, Rep2, Ratio2>& rhs)
{
return rhs < lhs;
}
template<typename Dimension, typename Rep1, class Ratio1, typename Rep2, class Ratio2>
constexpr bool operator>=(const quantity<Dimension, Rep1, Ratio1>& lhs,
const quantity<Dimension, Rep2, Ratio2>& rhs)
{
return !(lhs < rhs);
}
// clang-format on
} // namespace units
namespace std {
// todo: simplified
template<typename Dimension, typename Rep1, typename Ratio1, typename Rep2, typename Ratio2>
struct common_type<units::quantity<Dimension, Rep1, Ratio1>, units::quantity<Dimension, Rep2, Ratio2>> {
using type =
units::quantity<Dimension, std::common_type_t<Rep1, Rep2>, mp::common_ratio_t<Ratio1, Ratio2>>;
};
} // namespace std
+38
View File
@@ -0,0 +1,38 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include "../dimension.h"
#include "../quantity.h"
namespace units {
struct base_dim_length : dim_id<0> {};
struct base_dim_mass : dim_id<1> {};
struct base_dim_time : dim_id<2> {};
struct base_dim_electric_current : dim_id<3> {};
struct base_dim_temperature : dim_id<4> {};
struct base_dim_amount_of_substance : dim_id<5> {};
struct base_dim_luminous_intensity : dim_id<6> {};
} // namespace units
+83
View File
@@ -0,0 +1,83 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include "base_dimensions.h"
#include "time.h"
namespace units {
using dimension_frequency = make_dimension_t<exp<base_dim_time, -1>>;
template<typename Rep, class Ratio = std::ratio<1>>
using frequency = quantity<dimension_frequency, Rep, Ratio>;
template<typename Rep>
using millihertzs = frequency<Rep, std::milli>;
template<typename Rep>
using hertzs = frequency<Rep>;
template<typename Rep>
using kilohertzs = frequency<Rep, std::kilo>;
template<typename Rep>
using megahertzs = frequency<Rep, std::mega>;
template<typename Rep>
using gigahertzs = frequency<Rep, std::giga>;
template<typename Rep>
using terahertzs = frequency<Rep, std::tera>;
// ...
namespace literals {
// mHz
constexpr auto operator""_mHz(unsigned long long l) { return millihertzs<std::int64_t>(l); }
constexpr auto operator""_mHz(long double l) { return millihertzs<long double>(l); }
// Hz
constexpr auto operator""_Hz(unsigned long long l) { return hertzs<std::int64_t>(l); }
constexpr auto operator""_Hz(long double l) { return hertzs<long double>(l); }
// kHz
constexpr auto operator""_kHz(unsigned long long l) { return kilohertzs<std::int64_t>(l); }
constexpr auto operator""_kHz(long double l) { return kilohertzs<long double>(l); }
// MHz
constexpr auto operator""_MHz(unsigned long long l) { return megahertzs<std::int64_t>(l); }
constexpr auto operator""_MHz(long double l) { return megahertzs<long double>(l); }
// GHz
constexpr auto operator""_GHz(unsigned long long l) { return gigahertzs<std::int64_t>(l); }
constexpr auto operator""_GHz(long double l) { return gigahertzs<long double>(l); }
// THz
constexpr auto operator""_THz(unsigned long long l) { return terahertzs<std::int64_t>(l); }
constexpr auto operator""_THz(long double l) { return terahertzs<long double>(l); }
} // namespace literals
} // namespace units
+61
View File
@@ -0,0 +1,61 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include "base_dimensions.h"
namespace units {
using dimension_length = make_dimension_t<exp<base_dim_length, 1>>;
template<typename Rep, class Ratio = std::ratio<1>>
using length = quantity<dimension_length, Rep, Ratio>;
template<typename Rep>
using millimeters = length<Rep, std::milli>;
template<typename Rep>
using meters = length<Rep>;
template<typename Rep>
using kilometers = length<Rep, std::kilo>;
// ...
namespace literals {
// mm
constexpr auto operator""_mm(unsigned long long l) { return millimeters<std::int64_t>(l); }
constexpr auto operator""_mm(long double l) { return millimeters<long double>(l); }
// m
constexpr auto operator""_m(unsigned long long l) { return meters<std::int64_t>(l); }
constexpr auto operator""_m(long double l) { return meters<long double>(l); }
// km
constexpr auto operator""_km(unsigned long long l) { return kilometers<std::int64_t>(l); }
constexpr auto operator""_km(long double l) { return kilometers<long double>(l); }
} // namespace literals
} // namespace units
+82
View File
@@ -0,0 +1,82 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include "base_dimensions.h"
namespace units {
using dimension_time = make_dimension_t<exp<base_dim_time, 1>>;
template<typename Rep, class Ratio = std::ratio<1>>
using time = quantity<dimension_time, Rep, Ratio>;
template<typename Rep>
using nanoseconds = time<Rep, std::nano>;
template<typename Rep>
using microseconds = time<Rep, std::micro>;
template<typename Rep>
using milliseconds = time<Rep, std::milli>;
template<typename Rep>
using seconds = time<Rep>;
template<typename Rep>
using minutes = time<Rep, std::ratio<60>>;
template<typename Rep>
using hours = time<Rep, std::ratio<3600>>;
// ...
namespace literals {
// ns
constexpr auto operator""_ns(unsigned long long l) { return nanoseconds<std::int64_t>(l); }
constexpr auto operator""_ns(long double l) { return nanoseconds<long double>(l); }
// us
constexpr auto operator""_us(unsigned long long l) { return microseconds<std::int64_t>(l); }
constexpr auto operator""_us(long double l) { return microseconds<long double>(l); }
// ms
constexpr auto operator""_ms(unsigned long long l) { return milliseconds<std::int64_t>(l); }
constexpr auto operator""_ms(long double l) { return milliseconds<long double>(l); }
// s
constexpr auto operator""_s(unsigned long long l) { return seconds<std::int64_t>(l); }
constexpr auto operator""_s(long double l) { return seconds<long double>(l); }
// min
constexpr auto operator""_min(unsigned long long l) { return minutes<std::int64_t>(l); }
constexpr auto operator""_min(long double l) { return minutes<long double>(l); }
// h
constexpr auto operator""_h(unsigned long long l) { return hours<std::int64_t>(l); }
constexpr auto operator""_h(long double l) { return hours<long double>(l); }
} // namespace literals
} // namespace units
+63
View File
@@ -0,0 +1,63 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include "base_dimensions.h"
#include "length.h"
#include "time.h"
namespace units {
using dimension_velocity = make_dimension_t<exp<base_dim_length, 1>, exp<base_dim_time, -1>>;
template<typename Rep, class Ratio = std::ratio<1>>
using velocity = quantity<dimension_velocity, Rep, Ratio>;
template<typename Rep>
using meters_per_second = velocity<Rep>;
template<typename Rep>
using kilometers_per_hour = velocity<Rep, std::ratio<1000, 3600>>;
template<typename Rep>
using miles_per_hour = velocity<Rep, std::ratio<44'704, 100'000>>;
// ...
namespace literals {
// mps
constexpr auto operator""_mps(unsigned long long l) { return meters_per_second<std::int64_t>(l); }
constexpr auto operator""_mps(long double l) { return meters_per_second<long double>(l); }
// kmph
constexpr auto operator""_kmph(unsigned long long l) { return kilometers_per_hour<std::int64_t>(l); }
constexpr auto operator""_kmph(long double l) { return kilometers_per_hour<long double>(l); }
// mph
constexpr auto operator""_mph(unsigned long long l) { return miles_per_hour<std::int64_t>(l); }
constexpr auto operator""_mph(long double l) { return miles_per_hour<long double>(l); }
} // namespace literals
} // namespace units