mirror of
https://github.com/mpusz/mp-units.git
synced 2025-07-29 18:07:16 +02:00
make_dimension metabench test added
This commit is contained in:
@ -40,5 +40,6 @@ enable_testing()
|
||||
|
||||
add_custom_target(metabench)
|
||||
|
||||
add_subdirectory(ratio)
|
||||
add_subdirectory(list)
|
||||
add_subdirectory(make_dimension)
|
||||
add_subdirectory(ratio)
|
||||
|
36
test/metabench/make_dimension/CMakeLists.txt
Normal file
36
test/metabench/make_dimension/CMakeLists.txt
Normal file
@ -0,0 +1,36 @@
|
||||
# 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.
|
||||
|
||||
|
||||
add_metabench_test(metabench.data.make_dimension.no_concepts "no concepts" no_concepts.cpp.erb "[1, 2, 3, 4, 6, 8, 10]")
|
||||
add_metabench_test(metabench.data.make_dimension.concepts_iface "concepts iface" concepts_iface.cpp.erb "[1, 2, 3, 4, 6, 8, 10]")
|
||||
add_metabench_test(metabench.data.make_dimension.concepts_all "concepts all" concepts_all.cpp.erb "[1, 2, 3, 4, 6, 8, 10]")
|
||||
metabench_add_chart(metabench.chart.make_dimension
|
||||
TITLE "100 x make_dimension"
|
||||
SUBTITLE "(lower is better)"
|
||||
DATASETS
|
||||
metabench.data.make_dimension.no_concepts
|
||||
metabench.data.make_dimension.concepts_iface
|
||||
metabench.data.make_dimension.concepts_all
|
||||
)
|
||||
|
||||
add_dependencies(metabench metabench.chart.make_dimension)
|
25
test/metabench/make_dimension/concepts_all.cpp.erb
Normal file
25
test/metabench/make_dimension/concepts_all.cpp.erb
Normal file
@ -0,0 +1,25 @@
|
||||
#include "dimension_concepts_all.h"
|
||||
|
||||
namespace stde = std::experimental;
|
||||
|
||||
<% (1..100).each do |k| %>
|
||||
struct test<%= k %> {
|
||||
<% (1..n).each do |i| %>
|
||||
static constexpr stde::units::base_dimension dim<%= i %>{"dim<%= i %>"};
|
||||
<% end %>
|
||||
|
||||
#if defined(METABENCH)
|
||||
using dim = stde::units::make_dimension_t<<%=
|
||||
xs = ((1)..(n)).map { |j| "stde::units::exp<dim#{j}, 1>" }
|
||||
rng = Random.new(k)
|
||||
xs.shuffle(random: rng).join(', ')
|
||||
%>>;
|
||||
#else
|
||||
using dim = void;
|
||||
#endif
|
||||
};
|
||||
<% end %>
|
||||
|
||||
int main()
|
||||
{
|
||||
}
|
25
test/metabench/make_dimension/concepts_iface.cpp.erb
Normal file
25
test/metabench/make_dimension/concepts_iface.cpp.erb
Normal file
@ -0,0 +1,25 @@
|
||||
#include "dimension_concepts_iface.h"
|
||||
|
||||
namespace stde = std::experimental;
|
||||
|
||||
<% (1..100).each do |k| %>
|
||||
struct test<%= k %> {
|
||||
<% (1..n).each do |i| %>
|
||||
static constexpr stde::units::base_dimension dim<%= i %>{"dim<%= i %>"};
|
||||
<% end %>
|
||||
|
||||
#if defined(METABENCH)
|
||||
using dim = stde::units::make_dimension_t<<%=
|
||||
xs = ((1)..(n)).map { |j| "stde::units::exp<dim#{j}, 1>" }
|
||||
rng = Random.new(k)
|
||||
xs.shuffle(random: rng).join(', ')
|
||||
%>>;
|
||||
#else
|
||||
using dim = void;
|
||||
#endif
|
||||
};
|
||||
<% end %>
|
||||
|
||||
int main()
|
||||
{
|
||||
}
|
216
test/metabench/make_dimension/dimension_concepts_all.h
Normal file
216
test/metabench/make_dimension/dimension_concepts_all.h
Normal file
@ -0,0 +1,216 @@
|
||||
// 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 "type_list_concepts_all.h"
|
||||
#include "downcasting_concepts_all.h"
|
||||
#include "ratio_concepts_all.h"
|
||||
#include <ratio>
|
||||
|
||||
namespace std::experimental::units {
|
||||
|
||||
struct base_dimension {
|
||||
const char* name;
|
||||
};
|
||||
|
||||
constexpr bool operator==(const base_dimension& lhs, const base_dimension& rhs)
|
||||
{
|
||||
const char* p1 = lhs.name;
|
||||
const char* p2 = rhs.name;
|
||||
for(; (*p1 != '\0') && (*p2 != '\0'); ++p1, (void)++p2) {
|
||||
if(*p1 != *p2) return false;
|
||||
}
|
||||
return *p1 == *p2;
|
||||
}
|
||||
|
||||
constexpr bool operator<(const base_dimension& lhs, const base_dimension& rhs)
|
||||
{
|
||||
const char* p1 = lhs.name;
|
||||
const char* p2 = rhs.name;
|
||||
for(; (*p1 != '\0') && (*p2 != '\0'); ++p1, (void)++p2) {
|
||||
if(*p1 < *p2) return true;
|
||||
if(*p2 < *p1) return false;
|
||||
}
|
||||
return (*p1 == '\0') && (*p2 != '\0');
|
||||
}
|
||||
|
||||
// base_dimension_less
|
||||
|
||||
template<const base_dimension& D1, const base_dimension& D2>
|
||||
struct base_dimension_less : std::bool_constant<D1 < D2> {
|
||||
};
|
||||
|
||||
// exp
|
||||
|
||||
template<const base_dimension& BaseDimension, int Num, int Den = 1>
|
||||
struct exp {
|
||||
static constexpr const base_dimension& dimension = BaseDimension;
|
||||
static constexpr int num = Num;
|
||||
static constexpr int den = Den;
|
||||
};
|
||||
|
||||
// is_exp
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
inline constexpr bool is_exp = false;
|
||||
|
||||
template<const base_dimension& BaseDimension, int Num, int Den>
|
||||
inline constexpr bool is_exp<exp<BaseDimension, Num, Den>> = true;
|
||||
} // namespace detail
|
||||
|
||||
template<typename T>
|
||||
concept bool Exponent = detail::is_exp<T>;
|
||||
|
||||
// exp_dim_id_less
|
||||
|
||||
template<Exponent E1, Exponent E2>
|
||||
struct exp_less : base_dimension_less<E1::dimension, E2::dimension> {
|
||||
};
|
||||
|
||||
// exp_invert
|
||||
|
||||
template<Exponent E>
|
||||
struct exp_invert;
|
||||
|
||||
template<const base_dimension& BaseDimension, int Num, int Den>
|
||||
struct exp_invert<exp<BaseDimension, Num, Den>> {
|
||||
using type = exp<BaseDimension, -Num, Den>;
|
||||
};
|
||||
|
||||
template<Exponent E>
|
||||
using exp_invert_t = exp_invert<E>::type;
|
||||
|
||||
// dimension
|
||||
|
||||
template<Exponent... Es>
|
||||
struct dimension : downcast_base<dimension<Es...>> {};
|
||||
|
||||
// is_dimension
|
||||
namespace detail {
|
||||
|
||||
template<typename T>
|
||||
inline constexpr bool is_dimension = false;
|
||||
|
||||
template<typename... Es>
|
||||
inline constexpr bool is_dimension<dimension<Es...>> = true;
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<typename T>
|
||||
concept bool Dimension =
|
||||
std::is_empty_v<T> &&
|
||||
detail::is_dimension<downcast_from<T>>;
|
||||
|
||||
|
||||
// dim_invert
|
||||
|
||||
template<Dimension E>
|
||||
struct dim_invert;
|
||||
|
||||
template<Exponent... Es>
|
||||
struct dim_invert<dimension<Es...>> : std::type_identity<downcasting_traits_t<dimension<exp_invert_t<Es>...>>> {};
|
||||
|
||||
template<Dimension D>
|
||||
using dim_invert_t = dim_invert<typename D::base_type>::type;
|
||||
|
||||
|
||||
// todo: force as the only user interface to create dimensions through modules
|
||||
// make_dimension
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<Dimension D>
|
||||
struct dim_consolidate;
|
||||
|
||||
template<Dimension D>
|
||||
using dim_consolidate_t = dim_consolidate<D>::type;
|
||||
|
||||
template<>
|
||||
struct dim_consolidate<dimension<>> {
|
||||
using type = dimension<>;
|
||||
};
|
||||
|
||||
template<Exponent E>
|
||||
struct dim_consolidate<dimension<E>> {
|
||||
using type = dimension<E>;
|
||||
};
|
||||
|
||||
template<Exponent E1, Exponent... ERest>
|
||||
struct dim_consolidate<dimension<E1, ERest...>> {
|
||||
using rest = dim_consolidate_t<dimension<ERest...>>;
|
||||
using type = conditional<std::is_same_v<rest, dimension<>>, dimension<E1>, type_list_push_front<rest, E1>>;
|
||||
};
|
||||
|
||||
template<const base_dimension& D, int Num1, int Den1, int Num2, int Den2, Exponent... ERest>
|
||||
struct dim_consolidate<dimension<exp<D, Num1, Den1>, exp<D, Num2, Den2>, ERest...>> {
|
||||
// todo: provide custom implementation for ratio_add
|
||||
using r1 = std::ratio<Num1, Den1>;
|
||||
using r2 = std::ratio<Num2, Den2>;
|
||||
using r = std::ratio_add<r1, r2>;
|
||||
using type = conditional<r::num == 0, dim_consolidate_t<dimension<ERest...>>,
|
||||
dim_consolidate_t<dimension<exp<D, r::num, r::den>, ERest...>>>;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<Exponent... Es>
|
||||
struct make_dimension {
|
||||
using type = detail::dim_consolidate_t<type_list_sort<dimension<Es...>, exp_less>>;
|
||||
};
|
||||
|
||||
template<Exponent... Es>
|
||||
using make_dimension_t = make_dimension<Es...>::type;
|
||||
|
||||
template<Dimension D1, Dimension D2>
|
||||
struct merge_dimension {
|
||||
using type = detail::dim_consolidate_t<type_list_merge_sorted<D1, D2, exp_less>>;
|
||||
};
|
||||
|
||||
template<Dimension D1, Dimension D2>
|
||||
using merge_dimension_t = merge_dimension<D1, D2>::type;
|
||||
|
||||
// dimension_multiply
|
||||
|
||||
template<Dimension D1, Dimension D2>
|
||||
struct dimension_multiply;
|
||||
|
||||
template<Exponent... E1, Exponent... E2>
|
||||
struct dimension_multiply<dimension<E1...>, dimension<E2...>> : std::type_identity<downcasting_traits_t<merge_dimension_t<dimension<E1...>, dimension<E2...>>>> {};
|
||||
|
||||
template<Dimension D1, Dimension D2>
|
||||
using dimension_multiply_t = dimension_multiply<typename D1::base_type, typename D2::base_type>::type;
|
||||
|
||||
// dimension_divide
|
||||
|
||||
template<Dimension D1, Dimension D2>
|
||||
struct dimension_divide;
|
||||
|
||||
template<Exponent... E1, Exponent... E2>
|
||||
struct dimension_divide<dimension<E1...>, dimension<E2...>>
|
||||
: dimension_multiply<dimension<E1...>, dimension<exp_invert_t<E2>...>> {
|
||||
};
|
||||
|
||||
template<Dimension D1, Dimension D2>
|
||||
using dimension_divide_t = dimension_divide<typename D1::base_type, typename D2::base_type>::type;
|
||||
|
||||
} // namespace std::experimental::units
|
216
test/metabench/make_dimension/dimension_concepts_iface.h
Normal file
216
test/metabench/make_dimension/dimension_concepts_iface.h
Normal file
@ -0,0 +1,216 @@
|
||||
// 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 "type_list_concepts_iface.h"
|
||||
#include "downcasting_concepts_all.h"
|
||||
#include "ratio_concepts_iface.h"
|
||||
#include <ratio>
|
||||
|
||||
namespace std::experimental::units {
|
||||
|
||||
struct base_dimension {
|
||||
const char* name;
|
||||
};
|
||||
|
||||
constexpr bool operator==(const base_dimension& lhs, const base_dimension& rhs)
|
||||
{
|
||||
const char* p1 = lhs.name;
|
||||
const char* p2 = rhs.name;
|
||||
for(; (*p1 != '\0') && (*p2 != '\0'); ++p1, (void)++p2) {
|
||||
if(*p1 != *p2) return false;
|
||||
}
|
||||
return *p1 == *p2;
|
||||
}
|
||||
|
||||
constexpr bool operator<(const base_dimension& lhs, const base_dimension& rhs)
|
||||
{
|
||||
const char* p1 = lhs.name;
|
||||
const char* p2 = rhs.name;
|
||||
for(; (*p1 != '\0') && (*p2 != '\0'); ++p1, (void)++p2) {
|
||||
if(*p1 < *p2) return true;
|
||||
if(*p2 < *p1) return false;
|
||||
}
|
||||
return (*p1 == '\0') && (*p2 != '\0');
|
||||
}
|
||||
|
||||
// base_dimension_less
|
||||
|
||||
template<const base_dimension& D1, const base_dimension& D2>
|
||||
struct base_dimension_less : std::bool_constant<D1 < D2> {
|
||||
};
|
||||
|
||||
// exp
|
||||
|
||||
template<const base_dimension& BaseDimension, int Num, int Den = 1>
|
||||
struct exp {
|
||||
static constexpr const base_dimension& dimension = BaseDimension;
|
||||
static constexpr int num = Num;
|
||||
static constexpr int den = Den;
|
||||
};
|
||||
|
||||
// is_exp
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
inline constexpr bool is_exp = false;
|
||||
|
||||
template<const base_dimension& BaseDimension, int Num, int Den>
|
||||
inline constexpr bool is_exp<exp<BaseDimension, Num, Den>> = true;
|
||||
} // namespace detail
|
||||
|
||||
template<typename T>
|
||||
concept bool Exponent = detail::is_exp<T>;
|
||||
|
||||
// exp_dim_id_less
|
||||
|
||||
template<Exponent E1, Exponent E2>
|
||||
struct exp_less : base_dimension_less<E1::dimension, E2::dimension> {
|
||||
};
|
||||
|
||||
// exp_invert
|
||||
|
||||
template<Exponent E>
|
||||
struct exp_invert;
|
||||
|
||||
template<const base_dimension& BaseDimension, int Num, int Den>
|
||||
struct exp_invert<exp<BaseDimension, Num, Den>> {
|
||||
using type = exp<BaseDimension, -Num, Den>;
|
||||
};
|
||||
|
||||
template<Exponent E>
|
||||
using exp_invert_t = exp_invert<E>::type;
|
||||
|
||||
// dimension
|
||||
|
||||
template<Exponent... Es>
|
||||
struct dimension : downcast_base<dimension<Es...>> {};
|
||||
|
||||
// is_dimension
|
||||
namespace detail {
|
||||
|
||||
template<typename T>
|
||||
inline constexpr bool is_dimension = false;
|
||||
|
||||
template<typename... Es>
|
||||
inline constexpr bool is_dimension<dimension<Es...>> = true;
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<typename T>
|
||||
concept bool Dimension =
|
||||
std::is_empty_v<T> &&
|
||||
detail::is_dimension<downcast_from<T>>;
|
||||
|
||||
|
||||
// dim_invert
|
||||
|
||||
template<Dimension E>
|
||||
struct dim_invert;
|
||||
|
||||
template<typename... Es>
|
||||
struct dim_invert<dimension<Es...>> : std::type_identity<downcasting_traits_t<dimension<exp_invert_t<Es>...>>> {};
|
||||
|
||||
template<Dimension D>
|
||||
using dim_invert_t = dim_invert<typename D::base_type>::type;
|
||||
|
||||
|
||||
// todo: force as the only user interface to create dimensions through modules
|
||||
// make_dimension
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<Dimension D>
|
||||
struct dim_consolidate;
|
||||
|
||||
template<Dimension D>
|
||||
using dim_consolidate_t = dim_consolidate<D>::type;
|
||||
|
||||
template<>
|
||||
struct dim_consolidate<dimension<>> {
|
||||
using type = dimension<>;
|
||||
};
|
||||
|
||||
template<typename E>
|
||||
struct dim_consolidate<dimension<E>> {
|
||||
using type = dimension<E>;
|
||||
};
|
||||
|
||||
template<typename E1, typename... ERest>
|
||||
struct dim_consolidate<dimension<E1, ERest...>> {
|
||||
using rest = dim_consolidate_t<dimension<ERest...>>;
|
||||
using type = conditional<std::is_same_v<rest, dimension<>>, dimension<E1>, type_list_push_front<rest, E1>>;
|
||||
};
|
||||
|
||||
template<const base_dimension& D, int Num1, int Den1, int Num2, int Den2, typename... ERest>
|
||||
struct dim_consolidate<dimension<exp<D, Num1, Den1>, exp<D, Num2, Den2>, ERest...>> {
|
||||
// todo: provide custom implementation for ratio_add
|
||||
using r1 = std::ratio<Num1, Den1>;
|
||||
using r2 = std::ratio<Num2, Den2>;
|
||||
using r = std::ratio_add<r1, r2>;
|
||||
using type = conditional<r::num == 0, dim_consolidate_t<dimension<ERest...>>,
|
||||
dim_consolidate_t<dimension<exp<D, r::num, r::den>, ERest...>>>;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<Exponent... Es>
|
||||
struct make_dimension {
|
||||
using type = detail::dim_consolidate_t<type_list_sort<dimension<Es...>, exp_less>>;
|
||||
};
|
||||
|
||||
template<Exponent... Es>
|
||||
using make_dimension_t = make_dimension<Es...>::type;
|
||||
|
||||
template<Dimension D1, Dimension D2>
|
||||
struct merge_dimension {
|
||||
using type = detail::dim_consolidate_t<type_list_merge_sorted<D1, D2, exp_less>>;
|
||||
};
|
||||
|
||||
template<Dimension D1, Dimension D2>
|
||||
using merge_dimension_t = merge_dimension<D1, D2>::type;
|
||||
|
||||
// dimension_multiply
|
||||
|
||||
template<Dimension D1, Dimension D2>
|
||||
struct dimension_multiply;
|
||||
|
||||
template<typename... E1, typename... E2>
|
||||
struct dimension_multiply<dimension<E1...>, dimension<E2...>> : std::type_identity<downcasting_traits_t<merge_dimension_t<dimension<E1...>, dimension<E2...>>>> {};
|
||||
|
||||
template<Dimension D1, Dimension D2>
|
||||
using dimension_multiply_t = dimension_multiply<typename D1::base_type, typename D2::base_type>::type;
|
||||
|
||||
// dimension_divide
|
||||
|
||||
template<Dimension D1, Dimension D2>
|
||||
struct dimension_divide;
|
||||
|
||||
template<typename... E1, typename... E2>
|
||||
struct dimension_divide<dimension<E1...>, dimension<E2...>>
|
||||
: dimension_multiply<dimension<E1...>, dimension<exp_invert_t<E2>...>> {
|
||||
};
|
||||
|
||||
template<Dimension D1, Dimension D2>
|
||||
using dimension_divide_t = dimension_divide<typename D1::base_type, typename D2::base_type>::type;
|
||||
|
||||
} // namespace std::experimental::units
|
187
test/metabench/make_dimension/dimension_no_concepts.h
Normal file
187
test/metabench/make_dimension/dimension_no_concepts.h
Normal file
@ -0,0 +1,187 @@
|
||||
// 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 "type_list_no_concepts.h"
|
||||
#include "downcasting_no_concepts.h"
|
||||
#include "ratio_no_concepts.h"
|
||||
#include <ratio>
|
||||
|
||||
namespace std::experimental::units {
|
||||
|
||||
struct base_dimension {
|
||||
const char* name;
|
||||
};
|
||||
|
||||
constexpr bool operator==(const base_dimension& lhs, const base_dimension& rhs)
|
||||
{
|
||||
const char* p1 = lhs.name;
|
||||
const char* p2 = rhs.name;
|
||||
for(; (*p1 != '\0') && (*p2 != '\0'); ++p1, (void)++p2) {
|
||||
if(*p1 != *p2) return false;
|
||||
}
|
||||
return *p1 == *p2;
|
||||
}
|
||||
|
||||
constexpr bool operator<(const base_dimension& lhs, const base_dimension& rhs)
|
||||
{
|
||||
const char* p1 = lhs.name;
|
||||
const char* p2 = rhs.name;
|
||||
for(; (*p1 != '\0') && (*p2 != '\0'); ++p1, (void)++p2) {
|
||||
if(*p1 < *p2) return true;
|
||||
if(*p2 < *p1) return false;
|
||||
}
|
||||
return (*p1 == '\0') && (*p2 != '\0');
|
||||
}
|
||||
|
||||
// base_dimension_less
|
||||
|
||||
template<const base_dimension& D1, const base_dimension& D2>
|
||||
struct base_dimension_less : std::bool_constant<D1 < D2> {
|
||||
};
|
||||
|
||||
// exp
|
||||
|
||||
template<const base_dimension& BaseDimension, int Num, int Den = 1>
|
||||
struct exp {
|
||||
static constexpr const base_dimension& dimension = BaseDimension;
|
||||
static constexpr int num = Num;
|
||||
static constexpr int den = Den;
|
||||
};
|
||||
|
||||
// exp_dim_id_less
|
||||
|
||||
template<typename E1, typename E2>
|
||||
struct exp_less : base_dimension_less<E1::dimension, E2::dimension> {
|
||||
};
|
||||
|
||||
// exp_invert
|
||||
|
||||
template<typename E>
|
||||
struct exp_invert;
|
||||
|
||||
template<const base_dimension& BaseDimension, int Num, int Den>
|
||||
struct exp_invert<exp<BaseDimension, Num, Den>> {
|
||||
using type = exp<BaseDimension, -Num, Den>;
|
||||
};
|
||||
|
||||
template<typename E>
|
||||
using exp_invert_t = exp_invert<E>::type;
|
||||
|
||||
// dimension
|
||||
|
||||
template<typename... Es>
|
||||
struct dimension : downcast_base<dimension<Es...>> {};
|
||||
|
||||
// dim_invert
|
||||
|
||||
template<typename E>
|
||||
struct dim_invert;
|
||||
|
||||
template<typename... Es>
|
||||
struct dim_invert<dimension<Es...>> : std::type_identity<downcasting_traits_t<dimension<exp_invert_t<Es>...>>> {};
|
||||
|
||||
template<typename D>
|
||||
using dim_invert_t = dim_invert<typename D::base_type>::type;
|
||||
|
||||
|
||||
// todo: force as the only user interface to create dimensions through modules
|
||||
// make_dimension
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename D>
|
||||
struct dim_consolidate;
|
||||
|
||||
template<typename D>
|
||||
using dim_consolidate_t = dim_consolidate<D>::type;
|
||||
|
||||
template<>
|
||||
struct dim_consolidate<dimension<>> {
|
||||
using type = dimension<>;
|
||||
};
|
||||
|
||||
template<typename E>
|
||||
struct dim_consolidate<dimension<E>> {
|
||||
using type = dimension<E>;
|
||||
};
|
||||
|
||||
template<typename E1, typename... ERest>
|
||||
struct dim_consolidate<dimension<E1, ERest...>> {
|
||||
using rest = dim_consolidate_t<dimension<ERest...>>;
|
||||
using type = conditional<std::is_same_v<rest, dimension<>>, dimension<E1>, type_list_push_front<rest, E1>>;
|
||||
};
|
||||
|
||||
template<const base_dimension& D, int Num1, int Den1, int Num2, int Den2, typename... ERest>
|
||||
struct dim_consolidate<dimension<exp<D, Num1, Den1>, exp<D, Num2, Den2>, ERest...>> {
|
||||
// todo: provide custom implementation for ratio_add
|
||||
using r1 = std::ratio<Num1, Den1>;
|
||||
using r2 = std::ratio<Num2, Den2>;
|
||||
using r = std::ratio_add<r1, r2>;
|
||||
using type = conditional<r::num == 0, dim_consolidate_t<dimension<ERest...>>,
|
||||
dim_consolidate_t<dimension<exp<D, r::num, r::den>, ERest...>>>;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<typename... Es>
|
||||
struct make_dimension {
|
||||
using type = detail::dim_consolidate_t<type_list_sort<dimension<Es...>, exp_less>>;
|
||||
};
|
||||
|
||||
template<typename... Es>
|
||||
using make_dimension_t = make_dimension<Es...>::type;
|
||||
|
||||
template<typename D1, typename D2>
|
||||
struct merge_dimension {
|
||||
using type = detail::dim_consolidate_t<type_list_merge_sorted<D1, D2, exp_less>>;
|
||||
};
|
||||
|
||||
template<typename D1, typename D2>
|
||||
using merge_dimension_t = merge_dimension<D1, D2>::type;
|
||||
|
||||
// dimension_multiply
|
||||
|
||||
template<typename D1, typename D2>
|
||||
struct dimension_multiply;
|
||||
|
||||
template<typename... E1, typename... E2>
|
||||
struct dimension_multiply<dimension<E1...>, dimension<E2...>> : std::type_identity<downcasting_traits_t<merge_dimension_t<dimension<E1...>, dimension<E2...>>>> {};
|
||||
|
||||
template<typename D1, typename D2>
|
||||
using dimension_multiply_t = dimension_multiply<typename D1::base_type, typename D2::base_type>::type;
|
||||
|
||||
// dimension_divide
|
||||
|
||||
template<typename D1, typename D2>
|
||||
struct dimension_divide;
|
||||
|
||||
template<typename... E1, typename... E2>
|
||||
struct dimension_divide<dimension<E1...>, dimension<E2...>>
|
||||
: dimension_multiply<dimension<E1...>, dimension<exp_invert_t<E2>...>> {
|
||||
};
|
||||
|
||||
template<typename D1, typename D2>
|
||||
using dimension_divide_t = dimension_divide<typename D1::base_type, typename D2::base_type>::type;
|
||||
|
||||
} // namespace std::experimental::units
|
54
test/metabench/make_dimension/downcasting_concepts_all.h
Normal file
54
test/metabench/make_dimension/downcasting_concepts_all.h
Normal file
@ -0,0 +1,54 @@
|
||||
// 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 "hacks.h"
|
||||
#include <type_traits>
|
||||
|
||||
namespace std::experimental::units {
|
||||
|
||||
template<typename BaseType>
|
||||
struct downcast_base {
|
||||
using base_type = BaseType;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
concept bool Downcastable =
|
||||
requires {
|
||||
typename T::base_type;
|
||||
} &&
|
||||
std::derived_from<T, downcast_base<typename T::base_type>>;
|
||||
|
||||
template<Downcastable T>
|
||||
using downcast_from = T::base_type;
|
||||
|
||||
template<Downcastable T>
|
||||
using downcast_to = std::type_identity<T>;
|
||||
|
||||
template<Downcastable T>
|
||||
struct downcasting_traits : downcast_to<T> {};
|
||||
|
||||
template<Downcastable T>
|
||||
using downcasting_traits_t = downcasting_traits<T>::type;
|
||||
|
||||
} // namespace std::experimental::units
|
47
test/metabench/make_dimension/downcasting_no_concepts.h
Normal file
47
test/metabench/make_dimension/downcasting_no_concepts.h
Normal file
@ -0,0 +1,47 @@
|
||||
// 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 "hacks.h"
|
||||
#include <type_traits>
|
||||
|
||||
namespace std::experimental::units {
|
||||
|
||||
template<typename BaseType>
|
||||
struct downcast_base {
|
||||
using base_type = BaseType;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
using downcast_from = T::base_type;
|
||||
|
||||
template<typename T>
|
||||
using downcast_to = std::type_identity<T>;
|
||||
|
||||
template<typename T>
|
||||
struct downcasting_traits : downcast_to<T> {};
|
||||
|
||||
template<typename T>
|
||||
using downcasting_traits_t = downcasting_traits<T>::type;
|
||||
|
||||
} // namespace std::experimental::units
|
43
test/metabench/make_dimension/hacks.h
Normal file
43
test/metabench/make_dimension/hacks.h
Normal file
@ -0,0 +1,43 @@
|
||||
// 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 <experimental/ranges/concepts>
|
||||
|
||||
#ifdef NDEBUG
|
||||
#define Expects(cond) (void)(cond);
|
||||
#else
|
||||
#include <cassert>
|
||||
#define Expects(cond) assert(cond);
|
||||
#endif
|
||||
|
||||
namespace std {
|
||||
|
||||
// concepts
|
||||
using experimental::ranges::same_as;
|
||||
using experimental::ranges::derived_from;
|
||||
using experimental::ranges::regular;
|
||||
using experimental::ranges::totally_ordered;
|
||||
using experimental::ranges::convertible_to;
|
||||
|
||||
}
|
25
test/metabench/make_dimension/no_concepts.cpp.erb
Normal file
25
test/metabench/make_dimension/no_concepts.cpp.erb
Normal file
@ -0,0 +1,25 @@
|
||||
#include "dimension_no_concepts.h"
|
||||
|
||||
namespace stde = std::experimental;
|
||||
|
||||
<% (1..100).each do |k| %>
|
||||
struct test<%= k %> {
|
||||
<% (1..n).each do |i| %>
|
||||
static constexpr stde::units::base_dimension dim<%= i %>{"dim<%= i %>"};
|
||||
<% end %>
|
||||
|
||||
#if defined(METABENCH)
|
||||
using dim = stde::units::make_dimension_t<<%=
|
||||
xs = ((1)..(n)).map { |j| "stde::units::exp<dim#{j}, 1>" }
|
||||
rng = Random.new(k)
|
||||
xs.shuffle(random: rng).join(', ')
|
||||
%>>;
|
||||
#else
|
||||
using dim = void;
|
||||
#endif
|
||||
};
|
||||
<% end %>
|
||||
|
||||
int main()
|
||||
{
|
||||
}
|
138
test/metabench/make_dimension/ratio_concepts_all.h
Normal file
138
test/metabench/make_dimension/ratio_concepts_all.h
Normal file
@ -0,0 +1,138 @@
|
||||
// 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 "hacks.h"
|
||||
#include <type_traits>
|
||||
#include <numeric>
|
||||
#include <cstdint>
|
||||
|
||||
namespace std::experimental::units {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] constexpr T abs(T v) noexcept { return v < 0 ? -v : v; }
|
||||
|
||||
}
|
||||
|
||||
template<std::intmax_t Num, std::intmax_t Den = 1>
|
||||
struct ratio {
|
||||
static_assert(Den != 0, "zero denominator");
|
||||
static_assert(-INTMAX_MAX <= Num, "numerator too negative");
|
||||
static_assert(-INTMAX_MAX <= Den, "denominator too negative");
|
||||
|
||||
static constexpr std::intmax_t num = Num * (Den < 0 ? -1 : 1) / std::gcd(Num, Den);
|
||||
static constexpr std::intmax_t den = detail::abs(Den) / std::gcd(Num, Den);
|
||||
|
||||
using type = ratio<num, den>;
|
||||
};
|
||||
|
||||
// is_ratio
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename T>
|
||||
inline constexpr bool is_ratio = false;
|
||||
|
||||
template<intmax_t Num, intmax_t Den>
|
||||
inline constexpr bool is_ratio<ratio<Num, Den>> = true;
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<typename T>
|
||||
concept bool Ratio = detail::is_ratio<T>;
|
||||
|
||||
// ratio_multiply
|
||||
|
||||
namespace detail {
|
||||
|
||||
static constexpr std::intmax_t safe_multiply(std::intmax_t lhs, std::intmax_t rhs)
|
||||
{
|
||||
constexpr std::uintmax_t c = std::uintmax_t(1) << (sizeof(std::intmax_t) * 4);
|
||||
|
||||
const std::uintmax_t a0 = detail::abs(lhs) % c;
|
||||
const std::uintmax_t a1 = detail::abs(lhs) / c;
|
||||
const std::uintmax_t b0 = detail::abs(rhs) % c;
|
||||
const std::uintmax_t b1 = detail::abs(rhs) / c;
|
||||
|
||||
Expects(a1 == 0 || b1 == 0); // overflow in multiplication
|
||||
Expects(a0 * b1 + b0 * a1 < (c >> 1)); // overflow in multiplication
|
||||
Expects(b0 * a0 <= INTMAX_MAX); // overflow in multiplication
|
||||
Expects((a0 * b1 + b0 * a1) * c <= INTMAX_MAX - b0 * a0); // overflow in multiplication
|
||||
|
||||
return lhs * rhs;
|
||||
}
|
||||
|
||||
template<Ratio R1, Ratio R2>
|
||||
struct ratio_multiply_impl {
|
||||
private:
|
||||
static constexpr std::intmax_t gcd1 = std::gcd(R1::num, R2::den);
|
||||
static constexpr std::intmax_t gcd2 = std::gcd(R2::num, R1::den);
|
||||
|
||||
public:
|
||||
using type = ratio<safe_multiply(R1::num / gcd1, R2::num / gcd2), safe_multiply(R1::den / gcd2, R2::den / gcd1)>;
|
||||
static constexpr std::intmax_t num = type::num;
|
||||
static constexpr std::intmax_t den = type::den;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<Ratio R1, Ratio R2>
|
||||
using ratio_multiply = detail::ratio_multiply_impl<R1, R2>::type;
|
||||
|
||||
// ratio_divide
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<Ratio R1, Ratio R2>
|
||||
struct ratio_divide_impl {
|
||||
static_assert(R2::num != 0, "division by 0");
|
||||
using type = ratio_multiply<R1, ratio<R2::den, R2::num>>;
|
||||
static constexpr std::intmax_t num = type::num;
|
||||
static constexpr std::intmax_t den = type::den;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<Ratio R1, Ratio R2>
|
||||
using ratio_divide = detail::ratio_divide_impl<R1, R2>::type;
|
||||
|
||||
// common_ratio
|
||||
|
||||
namespace detail {
|
||||
|
||||
// todo: simplified
|
||||
template<Ratio R1, Ratio R2>
|
||||
struct common_ratio_impl {
|
||||
static constexpr std::intmax_t gcd_num = std::gcd(R1::num, R2::num);
|
||||
static constexpr std::intmax_t gcd_den = std::gcd(R1::den, R2::den);
|
||||
using type = ratio<gcd_num, (R1::den / gcd_den) * R2::den>;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<Ratio R1, Ratio R2>
|
||||
using common_ratio = detail::common_ratio_impl<R1, R2>::type;
|
||||
|
||||
} // namespace std::experimental::units
|
138
test/metabench/make_dimension/ratio_concepts_iface.h
Normal file
138
test/metabench/make_dimension/ratio_concepts_iface.h
Normal file
@ -0,0 +1,138 @@
|
||||
// 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 "hacks.h"
|
||||
#include <type_traits>
|
||||
#include <numeric>
|
||||
#include <cstdint>
|
||||
|
||||
namespace std::experimental::units {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] constexpr T abs(T v) noexcept { return v < 0 ? -v : v; }
|
||||
|
||||
}
|
||||
|
||||
template<std::intmax_t Num, std::intmax_t Den = 1>
|
||||
struct ratio {
|
||||
static_assert(Den != 0, "zero denominator");
|
||||
static_assert(-INTMAX_MAX <= Num, "numerator too negative");
|
||||
static_assert(-INTMAX_MAX <= Den, "denominator too negative");
|
||||
|
||||
static constexpr std::intmax_t num = Num * (Den < 0 ? -1 : 1) / std::gcd(Num, Den);
|
||||
static constexpr std::intmax_t den = detail::abs(Den) / std::gcd(Num, Den);
|
||||
|
||||
using type = ratio<num, den>;
|
||||
};
|
||||
|
||||
// is_ratio
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename T>
|
||||
inline constexpr bool is_ratio = false;
|
||||
|
||||
template<intmax_t Num, intmax_t Den>
|
||||
inline constexpr bool is_ratio<ratio<Num, Den>> = true;
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<typename T>
|
||||
concept bool Ratio = detail::is_ratio<T>;
|
||||
|
||||
// ratio_multiply
|
||||
|
||||
namespace detail {
|
||||
|
||||
static constexpr std::intmax_t safe_multiply(std::intmax_t lhs, std::intmax_t rhs)
|
||||
{
|
||||
constexpr std::uintmax_t c = std::uintmax_t(1) << (sizeof(std::intmax_t) * 4);
|
||||
|
||||
const std::uintmax_t a0 = detail::abs(lhs) % c;
|
||||
const std::uintmax_t a1 = detail::abs(lhs) / c;
|
||||
const std::uintmax_t b0 = detail::abs(rhs) % c;
|
||||
const std::uintmax_t b1 = detail::abs(rhs) / c;
|
||||
|
||||
Expects(a1 == 0 || b1 == 0); // overflow in multiplication
|
||||
Expects(a0 * b1 + b0 * a1 < (c >> 1)); // overflow in multiplication
|
||||
Expects(b0 * a0 <= INTMAX_MAX); // overflow in multiplication
|
||||
Expects((a0 * b1 + b0 * a1) * c <= INTMAX_MAX - b0 * a0); // overflow in multiplication
|
||||
|
||||
return lhs * rhs;
|
||||
}
|
||||
|
||||
template<typename R1, typename R2>
|
||||
struct ratio_multiply_impl {
|
||||
private:
|
||||
static constexpr std::intmax_t gcd1 = std::gcd(R1::num, R2::den);
|
||||
static constexpr std::intmax_t gcd2 = std::gcd(R2::num, R1::den);
|
||||
|
||||
public:
|
||||
using type = ratio<safe_multiply(R1::num / gcd1, R2::num / gcd2), safe_multiply(R1::den / gcd2, R2::den / gcd1)>;
|
||||
static constexpr std::intmax_t num = type::num;
|
||||
static constexpr std::intmax_t den = type::den;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<Ratio R1, Ratio R2>
|
||||
using ratio_multiply = detail::ratio_multiply_impl<R1, R2>::type;
|
||||
|
||||
// ratio_divide
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename R1, typename R2>
|
||||
struct ratio_divide_impl {
|
||||
static_assert(R2::num != 0, "division by 0");
|
||||
using type = ratio_multiply<R1, ratio<R2::den, R2::num>>;
|
||||
static constexpr std::intmax_t num = type::num;
|
||||
static constexpr std::intmax_t den = type::den;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<Ratio R1, Ratio R2>
|
||||
using ratio_divide = detail::ratio_divide_impl<R1, R2>::type;
|
||||
|
||||
// common_ratio
|
||||
|
||||
namespace detail {
|
||||
|
||||
// todo: simplified
|
||||
template<typename R1, typename R2>
|
||||
struct common_ratio_impl {
|
||||
static constexpr std::intmax_t gcd_num = std::gcd(R1::num, R2::num);
|
||||
static constexpr std::intmax_t gcd_den = std::gcd(R1::den, R2::den);
|
||||
using type = ratio<gcd_num, (R1::den / gcd_den) * R2::den>;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<Ratio R1, Ratio R2>
|
||||
using common_ratio = detail::common_ratio_impl<R1, R2>::type;
|
||||
|
||||
} // namespace std::experimental::units
|
123
test/metabench/make_dimension/ratio_no_concepts.h
Normal file
123
test/metabench/make_dimension/ratio_no_concepts.h
Normal file
@ -0,0 +1,123 @@
|
||||
// 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 "hacks.h"
|
||||
#include <type_traits>
|
||||
#include <numeric>
|
||||
#include <cstdint>
|
||||
|
||||
namespace std::experimental::units {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] constexpr T abs(T v) noexcept { return v < 0 ? -v : v; }
|
||||
|
||||
}
|
||||
|
||||
template<std::intmax_t Num, std::intmax_t Den = 1>
|
||||
struct ratio {
|
||||
static_assert(Den != 0, "zero denominator");
|
||||
static_assert(-INTMAX_MAX <= Num, "numerator too negative");
|
||||
static_assert(-INTMAX_MAX <= Den, "denominator too negative");
|
||||
|
||||
static constexpr std::intmax_t num = Num * (Den < 0 ? -1 : 1) / std::gcd(Num, Den);
|
||||
static constexpr std::intmax_t den = detail::abs(Den) / std::gcd(Num, Den);
|
||||
|
||||
using type = ratio<num, den>;
|
||||
};
|
||||
|
||||
// ratio_multiply
|
||||
|
||||
namespace detail {
|
||||
|
||||
static constexpr std::intmax_t safe_multiply(std::intmax_t lhs, std::intmax_t rhs)
|
||||
{
|
||||
constexpr std::uintmax_t c = std::uintmax_t(1) << (sizeof(std::intmax_t) * 4);
|
||||
|
||||
const std::uintmax_t a0 = detail::abs(lhs) % c;
|
||||
const std::uintmax_t a1 = detail::abs(lhs) / c;
|
||||
const std::uintmax_t b0 = detail::abs(rhs) % c;
|
||||
const std::uintmax_t b1 = detail::abs(rhs) / c;
|
||||
|
||||
Expects(a1 == 0 || b1 == 0); // overflow in multiplication
|
||||
Expects(a0 * b1 + b0 * a1 < (c >> 1)); // overflow in multiplication
|
||||
Expects(b0 * a0 <= INTMAX_MAX); // overflow in multiplication
|
||||
Expects((a0 * b1 + b0 * a1) * c <= INTMAX_MAX - b0 * a0); // overflow in multiplication
|
||||
|
||||
return lhs * rhs;
|
||||
}
|
||||
|
||||
template<typename R1, typename R2>
|
||||
struct ratio_multiply_impl {
|
||||
private:
|
||||
static constexpr std::intmax_t gcd1 = std::gcd(R1::num, R2::den);
|
||||
static constexpr std::intmax_t gcd2 = std::gcd(R2::num, R1::den);
|
||||
|
||||
public:
|
||||
using type = ratio<safe_multiply(R1::num / gcd1, R2::num / gcd2), safe_multiply(R1::den / gcd2, R2::den / gcd1)>;
|
||||
static constexpr std::intmax_t num = type::num;
|
||||
static constexpr std::intmax_t den = type::den;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<typename R1, typename R2>
|
||||
using ratio_multiply = detail::ratio_multiply_impl<R1, R2>::type;
|
||||
|
||||
// ratio_divide
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename R1, typename R2>
|
||||
struct ratio_divide_impl {
|
||||
static_assert(R2::num != 0, "division by 0");
|
||||
using type = ratio_multiply<R1, ratio<R2::den, R2::num>>;
|
||||
static constexpr std::intmax_t num = type::num;
|
||||
static constexpr std::intmax_t den = type::den;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<typename R1, typename R2>
|
||||
using ratio_divide = detail::ratio_divide_impl<R1, R2>::type;
|
||||
|
||||
// common_ratio
|
||||
|
||||
namespace detail {
|
||||
|
||||
// todo: simplified
|
||||
template<typename R1, typename R2>
|
||||
struct common_ratio_impl {
|
||||
static constexpr std::intmax_t gcd_num = std::gcd(R1::num, R2::num);
|
||||
static constexpr std::intmax_t gcd_den = std::gcd(R1::den, R2::den);
|
||||
using type = ratio<gcd_num, (R1::den / gcd_den) * R2::den>;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<typename R1, typename R2>
|
||||
using common_ratio = detail::common_ratio_impl<R1, R2>::type;
|
||||
|
||||
} // namespace std::experimental::units
|
185
test/metabench/make_dimension/type_list_concepts_all.h
Normal file
185
test/metabench/make_dimension/type_list_concepts_all.h
Normal file
@ -0,0 +1,185 @@
|
||||
// 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 "type_traits.h"
|
||||
|
||||
namespace std::experimental::units {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename T>
|
||||
inline constexpr bool is_type_list = false;
|
||||
|
||||
template<template<typename...> typename T, typename... Types>
|
||||
inline constexpr bool is_type_list<T<Types...>> = true;
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<typename T>
|
||||
concept bool TypeList = detail::is_type_list<T>;
|
||||
|
||||
// push_front
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<TypeList List, typename... Types>
|
||||
struct type_list_push_front_impl;
|
||||
|
||||
template<template<typename...> typename List, typename... OldTypes, typename... NewTypes>
|
||||
struct type_list_push_front_impl<List<OldTypes...>, NewTypes...> {
|
||||
using type = List<NewTypes..., OldTypes...>;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<TypeList List, typename... Types>
|
||||
using type_list_push_front = detail::type_list_push_front_impl<List, Types...>::type;
|
||||
|
||||
// push_back
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<TypeList List, typename... Types>
|
||||
struct type_list_push_back_impl;
|
||||
|
||||
template<template<typename...> typename List, typename... OldTypes, typename... NewTypes>
|
||||
struct type_list_push_back_impl<List<OldTypes...>, NewTypes...> {
|
||||
using type = List<OldTypes..., NewTypes...>;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<TypeList List, typename... Types>
|
||||
using type_list_push_back = detail::type_list_push_back_impl<List, Types...>::type;
|
||||
|
||||
// split
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<template<typename...> typename List, std::size_t Idx, std::size_t N, typename... Types>
|
||||
struct split_impl;
|
||||
|
||||
template<template<typename...> typename List, std::size_t Idx, std::size_t N>
|
||||
struct split_impl<List, Idx, N> {
|
||||
using first_list = List<>;
|
||||
using second_list = List<>;
|
||||
};
|
||||
|
||||
template<template<typename...> typename List, std::size_t Idx, std::size_t N, typename T, typename... Rest>
|
||||
struct split_impl<List, Idx, N, T, Rest...> : split_impl<List, Idx + 1, N, Rest...> {
|
||||
using base = split_impl<List, Idx + 1, N, Rest...>;
|
||||
using first_list = conditional<Idx < N,
|
||||
typename type_list_push_front_impl<typename base::first_list, T>::type,
|
||||
typename base::first_list>;
|
||||
using second_list = conditional<Idx < N,
|
||||
typename base::second_list,
|
||||
typename type_list_push_front_impl<typename base::second_list, T>::type>;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<TypeList List, std::size_t N>
|
||||
struct type_list_split;
|
||||
|
||||
template<template<typename...> typename List, std::size_t N, typename... Types>
|
||||
struct type_list_split<List<Types...>, N> {
|
||||
static_assert(N <= sizeof...(Types), "Invalid index provided");
|
||||
using split = detail::split_impl<List, 0, N, Types...>;
|
||||
using first_list = split::first_list;
|
||||
using second_list = split::second_list;
|
||||
};
|
||||
|
||||
// split_half
|
||||
|
||||
template<TypeList List>
|
||||
struct type_list_split_half;
|
||||
|
||||
template<template<typename...> typename List, typename... Types>
|
||||
struct type_list_split_half<List<Types...>> : type_list_split<List<Types...>, (sizeof...(Types) + 1) / 2> {
|
||||
};
|
||||
|
||||
// merge_sorted
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<TypeList SortedList1, TypeList SortedList2, template<typename, typename> typename Pred>
|
||||
struct type_list_merge_sorted_impl;
|
||||
|
||||
template<template<typename...> typename List, typename... Lhs, template<typename, typename> typename Pred>
|
||||
struct type_list_merge_sorted_impl<List<Lhs...>, List<>, Pred> {
|
||||
using type = List<Lhs...>;
|
||||
};
|
||||
|
||||
template<template<typename...> typename List, typename... Rhs, template<typename, typename> typename Pred>
|
||||
struct type_list_merge_sorted_impl<List<>, List<Rhs...>, Pred> {
|
||||
using type = List<Rhs...>;
|
||||
};
|
||||
|
||||
template<template<typename...> typename List, typename Lhs1, typename... LhsRest, typename Rhs1, typename... RhsRest,
|
||||
template<typename, typename> typename Pred>
|
||||
struct type_list_merge_sorted_impl<List<Lhs1, LhsRest...>, List<Rhs1, RhsRest...>, Pred> {
|
||||
using type = conditional<
|
||||
Pred<Lhs1, Rhs1>::value,
|
||||
typename type_list_push_front_impl<typename type_list_merge_sorted_impl<List<LhsRest...>, List<Rhs1, RhsRest...>, Pred>::type, Lhs1>::type,
|
||||
typename type_list_push_front_impl<typename type_list_merge_sorted_impl<List<Lhs1, LhsRest...>, List<RhsRest...>, Pred>::type, Rhs1>::type>;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<TypeList SortedList1, TypeList SortedList2, template<typename, typename> typename Pred>
|
||||
using type_list_merge_sorted = detail::type_list_merge_sorted_impl<SortedList1, SortedList2, Pred>::type;
|
||||
|
||||
|
||||
// sort
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<TypeList List, template<typename, typename> typename Pred>
|
||||
struct type_list_sort_impl;
|
||||
|
||||
template<template<typename...> typename List, template<typename, typename> typename Pred>
|
||||
struct type_list_sort_impl<List<>, Pred> {
|
||||
using type = List<>;
|
||||
};
|
||||
|
||||
template<template<typename...> typename List, typename T, template<typename, typename> typename Pred>
|
||||
struct type_list_sort_impl<List<T>, Pred> {
|
||||
using type = List<T>;
|
||||
};
|
||||
|
||||
template<template<typename...> typename List, typename... Types, template<typename, typename> typename Pred>
|
||||
struct type_list_sort_impl<List<Types...>, Pred> {
|
||||
using types = List<Types...>;
|
||||
using split = type_list_split_half<List<Types...>>;
|
||||
using left = type_list_sort_impl<typename split::first_list, Pred>::type;
|
||||
using right = type_list_sort_impl<typename split::second_list, Pred>::type;
|
||||
using type = type_list_merge_sorted_impl<left, right, Pred>::type;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<TypeList List, template<typename, typename> typename Pred>
|
||||
using type_list_sort = detail::type_list_sort_impl<List, Pred>::type;
|
||||
|
||||
} // namespace std::experimental::units
|
185
test/metabench/make_dimension/type_list_concepts_iface.h
Normal file
185
test/metabench/make_dimension/type_list_concepts_iface.h
Normal file
@ -0,0 +1,185 @@
|
||||
// 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 "type_traits.h"
|
||||
|
||||
namespace std::experimental::units {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename T>
|
||||
inline constexpr bool is_type_list = false;
|
||||
|
||||
template<template<typename...> typename T, typename... Types>
|
||||
inline constexpr bool is_type_list<T<Types...>> = true;
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<typename T>
|
||||
concept bool TypeList = detail::is_type_list<T>;
|
||||
|
||||
// push_front
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename List, typename... Types>
|
||||
struct type_list_push_front_impl;
|
||||
|
||||
template<template<typename...> typename List, typename... OldTypes, typename... NewTypes>
|
||||
struct type_list_push_front_impl<List<OldTypes...>, NewTypes...> {
|
||||
using type = List<NewTypes..., OldTypes...>;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<TypeList List, typename... Types>
|
||||
using type_list_push_front = detail::type_list_push_front_impl<List, Types...>::type;
|
||||
|
||||
// push_back
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename List, typename... Types>
|
||||
struct type_list_push_back_impl;
|
||||
|
||||
template<template<typename...> typename List, typename... OldTypes, typename... NewTypes>
|
||||
struct type_list_push_back_impl<List<OldTypes...>, NewTypes...> {
|
||||
using type = List<OldTypes..., NewTypes...>;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<TypeList List, typename... Types>
|
||||
using type_list_push_back = detail::type_list_push_back_impl<List, Types...>::type;
|
||||
|
||||
// split
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<template<typename...> typename List, std::size_t Idx, std::size_t N, typename... Types>
|
||||
struct split_impl;
|
||||
|
||||
template<template<typename...> typename List, std::size_t Idx, std::size_t N>
|
||||
struct split_impl<List, Idx, N> {
|
||||
using first_list = List<>;
|
||||
using second_list = List<>;
|
||||
};
|
||||
|
||||
template<template<typename...> typename List, std::size_t Idx, std::size_t N, typename T, typename... Rest>
|
||||
struct split_impl<List, Idx, N, T, Rest...> : split_impl<List, Idx + 1, N, Rest...> {
|
||||
using base = split_impl<List, Idx + 1, N, Rest...>;
|
||||
using first_list = conditional<Idx < N,
|
||||
typename type_list_push_front_impl<typename base::first_list, T>::type,
|
||||
typename base::first_list>;
|
||||
using second_list = conditional<Idx < N,
|
||||
typename base::second_list,
|
||||
typename type_list_push_front_impl<typename base::second_list, T>::type>;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<TypeList List, std::size_t N>
|
||||
struct type_list_split;
|
||||
|
||||
template<template<typename...> typename List, std::size_t N, typename... Types>
|
||||
struct type_list_split<List<Types...>, N> {
|
||||
static_assert(N <= sizeof...(Types), "Invalid index provided");
|
||||
using split = detail::split_impl<List, 0, N, Types...>;
|
||||
using first_list = split::first_list;
|
||||
using second_list = split::second_list;
|
||||
};
|
||||
|
||||
// split_half
|
||||
|
||||
template<TypeList List>
|
||||
struct type_list_split_half;
|
||||
|
||||
template<template<typename...> typename List, typename... Types>
|
||||
struct type_list_split_half<List<Types...>> : type_list_split<List<Types...>, (sizeof...(Types) + 1) / 2> {
|
||||
};
|
||||
|
||||
// merge_sorted
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename SortedList1, typename SortedList2, template<typename, typename> typename Pred>
|
||||
struct type_list_merge_sorted_impl;
|
||||
|
||||
template<template<typename...> typename List, typename... Lhs, template<typename, typename> typename Pred>
|
||||
struct type_list_merge_sorted_impl<List<Lhs...>, List<>, Pred> {
|
||||
using type = List<Lhs...>;
|
||||
};
|
||||
|
||||
template<template<typename...> typename List, typename... Rhs, template<typename, typename> typename Pred>
|
||||
struct type_list_merge_sorted_impl<List<>, List<Rhs...>, Pred> {
|
||||
using type = List<Rhs...>;
|
||||
};
|
||||
|
||||
template<template<typename...> typename List, typename Lhs1, typename... LhsRest, typename Rhs1, typename... RhsRest,
|
||||
template<typename, typename> typename Pred>
|
||||
struct type_list_merge_sorted_impl<List<Lhs1, LhsRest...>, List<Rhs1, RhsRest...>, Pred> {
|
||||
using type = conditional<
|
||||
Pred<Lhs1, Rhs1>::value,
|
||||
typename type_list_push_front_impl<typename type_list_merge_sorted_impl<List<LhsRest...>, List<Rhs1, RhsRest...>, Pred>::type, Lhs1>::type,
|
||||
typename type_list_push_front_impl<typename type_list_merge_sorted_impl<List<Lhs1, LhsRest...>, List<RhsRest...>, Pred>::type, Rhs1>::type>;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<TypeList SortedList1, typename SortedList2, template<typename, typename> typename Pred>
|
||||
using type_list_merge_sorted = detail::type_list_merge_sorted_impl<SortedList1, SortedList2, Pred>::type;
|
||||
|
||||
|
||||
// sort
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename List, template<typename, typename> typename Pred>
|
||||
struct type_list_sort_impl;
|
||||
|
||||
template<template<typename...> typename List, template<typename, typename> typename Pred>
|
||||
struct type_list_sort_impl<List<>, Pred> {
|
||||
using type = List<>;
|
||||
};
|
||||
|
||||
template<template<typename...> typename List, typename T, template<typename, typename> typename Pred>
|
||||
struct type_list_sort_impl<List<T>, Pred> {
|
||||
using type = List<T>;
|
||||
};
|
||||
|
||||
template<template<typename...> typename List, typename... Types, template<typename, typename> typename Pred>
|
||||
struct type_list_sort_impl<List<Types...>, Pred> {
|
||||
using types = List<Types...>;
|
||||
using split = type_list_split_half<List<Types...>>;
|
||||
using left = type_list_sort_impl<typename split::first_list, Pred>::type;
|
||||
using right = type_list_sort_impl<typename split::second_list, Pred>::type;
|
||||
using type = type_list_merge_sorted_impl<left, right, Pred>::type;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<TypeList List, template<typename, typename> typename Pred>
|
||||
using type_list_sort = detail::type_list_sort_impl<List, Pred>::type;
|
||||
|
||||
} // namespace std::experimental::units
|
172
test/metabench/make_dimension/type_list_no_concepts.h
Normal file
172
test/metabench/make_dimension/type_list_no_concepts.h
Normal file
@ -0,0 +1,172 @@
|
||||
// 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 "type_traits.h"
|
||||
|
||||
namespace std::experimental::units {
|
||||
|
||||
// push_front
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename List, typename... Types>
|
||||
struct type_list_push_front_impl;
|
||||
|
||||
template<template<typename...> typename List, typename... OldTypes, typename... NewTypes>
|
||||
struct type_list_push_front_impl<List<OldTypes...>, NewTypes...> {
|
||||
using type = List<NewTypes..., OldTypes...>;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<typename List, typename... Types>
|
||||
using type_list_push_front = detail::type_list_push_front_impl<List, Types...>::type;
|
||||
|
||||
// push_back
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename List, typename... Types>
|
||||
struct type_list_push_back_impl;
|
||||
|
||||
template<template<typename...> typename List, typename... OldTypes, typename... NewTypes>
|
||||
struct type_list_push_back_impl<List<OldTypes...>, NewTypes...> {
|
||||
using type = List<OldTypes..., NewTypes...>;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<typename List, typename... Types>
|
||||
using type_list_push_back = detail::type_list_push_back_impl<List, Types...>::type;
|
||||
|
||||
// split
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<template<typename...> typename List, std::size_t Idx, std::size_t N, typename... Types>
|
||||
struct split_impl;
|
||||
|
||||
template<template<typename...> typename List, std::size_t Idx, std::size_t N>
|
||||
struct split_impl<List, Idx, N> {
|
||||
using first_list = List<>;
|
||||
using second_list = List<>;
|
||||
};
|
||||
|
||||
template<template<typename...> typename List, std::size_t Idx, std::size_t N, typename T, typename... Rest>
|
||||
struct split_impl<List, Idx, N, T, Rest...> : split_impl<List, Idx + 1, N, Rest...> {
|
||||
using base = split_impl<List, Idx + 1, N, Rest...>;
|
||||
using first_list = conditional<Idx < N,
|
||||
typename type_list_push_front_impl<typename base::first_list, T>::type,
|
||||
typename base::first_list>;
|
||||
using second_list = conditional<Idx < N,
|
||||
typename base::second_list,
|
||||
typename type_list_push_front_impl<typename base::second_list, T>::type>;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<typename List, std::size_t N>
|
||||
struct type_list_split;
|
||||
|
||||
template<template<typename...> typename List, std::size_t N, typename... Types>
|
||||
struct type_list_split<List<Types...>, N> {
|
||||
static_assert(N <= sizeof...(Types), "Invalid index provided");
|
||||
using split = detail::split_impl<List, 0, N, Types...>;
|
||||
using first_list = split::first_list;
|
||||
using second_list = split::second_list;
|
||||
};
|
||||
|
||||
// split_half
|
||||
|
||||
template<typename List>
|
||||
struct type_list_split_half;
|
||||
|
||||
template<template<typename...> typename List, typename... Types>
|
||||
struct type_list_split_half<List<Types...>> : type_list_split<List<Types...>, (sizeof...(Types) + 1) / 2> {
|
||||
};
|
||||
|
||||
// merge_sorted
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename SortedList1, typename SortedList2, template<typename, typename> typename Pred>
|
||||
struct type_list_merge_sorted_impl;
|
||||
|
||||
template<template<typename...> typename List, typename... Lhs, template<typename, typename> typename Pred>
|
||||
struct type_list_merge_sorted_impl<List<Lhs...>, List<>, Pred> {
|
||||
using type = List<Lhs...>;
|
||||
};
|
||||
|
||||
template<template<typename...> typename List, typename... Rhs, template<typename, typename> typename Pred>
|
||||
struct type_list_merge_sorted_impl<List<>, List<Rhs...>, Pred> {
|
||||
using type = List<Rhs...>;
|
||||
};
|
||||
|
||||
template<template<typename...> typename List, typename Lhs1, typename... LhsRest, typename Rhs1, typename... RhsRest,
|
||||
template<typename, typename> typename Pred>
|
||||
struct type_list_merge_sorted_impl<List<Lhs1, LhsRest...>, List<Rhs1, RhsRest...>, Pred> {
|
||||
using type = conditional<
|
||||
Pred<Lhs1, Rhs1>::value,
|
||||
typename type_list_push_front_impl<typename type_list_merge_sorted_impl<List<LhsRest...>, List<Rhs1, RhsRest...>, Pred>::type, Lhs1>::type,
|
||||
typename type_list_push_front_impl<typename type_list_merge_sorted_impl<List<Lhs1, LhsRest...>, List<RhsRest...>, Pred>::type, Rhs1>::type>;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<typename SortedList1, typename SortedList2, template<typename, typename> typename Pred>
|
||||
using type_list_merge_sorted = detail::type_list_merge_sorted_impl<SortedList1, SortedList2, Pred>::type;
|
||||
|
||||
|
||||
// sort
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename List, template<typename, typename> typename Pred>
|
||||
struct type_list_sort_impl;
|
||||
|
||||
template<template<typename...> typename List, template<typename, typename> typename Pred>
|
||||
struct type_list_sort_impl<List<>, Pred> {
|
||||
using type = List<>;
|
||||
};
|
||||
|
||||
template<template<typename...> typename List, typename T, template<typename, typename> typename Pred>
|
||||
struct type_list_sort_impl<List<T>, Pred> {
|
||||
using type = List<T>;
|
||||
};
|
||||
|
||||
template<template<typename...> typename List, typename... Types, template<typename, typename> typename Pred>
|
||||
struct type_list_sort_impl<List<Types...>, Pred> {
|
||||
using types = List<Types...>;
|
||||
using split = type_list_split_half<List<Types...>>;
|
||||
using left = type_list_sort_impl<typename split::first_list, Pred>::type;
|
||||
using right = type_list_sort_impl<typename split::second_list, Pred>::type;
|
||||
using type = type_list_merge_sorted_impl<left, right, Pred>::type;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<typename List, template<typename, typename> typename Pred>
|
||||
using type_list_sort = detail::type_list_sort_impl<List, Pred>::type;
|
||||
|
||||
} // namespace std::experimental::units
|
48
test/metabench/make_dimension/type_traits.h
Normal file
48
test/metabench/make_dimension/type_traits.h
Normal file
@ -0,0 +1,48 @@
|
||||
// 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 <type_traits>
|
||||
|
||||
namespace std::experimental::units {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<bool>
|
||||
struct conditional_impl {
|
||||
template<typename T, typename F>
|
||||
using type = F;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct conditional_impl<true> {
|
||||
template<typename T, typename F>
|
||||
using type = T;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<bool B, typename T, typename F>
|
||||
using conditional = detail::conditional_impl<B>::template type<T, F>;
|
||||
|
||||
}
|
Reference in New Issue
Block a user