mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-04 20:54:28 +02:00
test: metabench tests removed from repo
This commit is contained in:
@@ -1,43 +0,0 @@
|
|||||||
# 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.
|
|
||||||
|
|
||||||
cmake_minimum_required(VERSION 3.12)
|
|
||||||
|
|
||||||
function(add_metabench_test target name erb_path range)
|
|
||||||
metabench_add_dataset(${target} "${erb_path}" "${range}" NAME "${name}")
|
|
||||||
target_compile_features(${target} PUBLIC cxx_std_20)
|
|
||||||
target_link_libraries(${target} PUBLIC range-v3::range-v3>)
|
|
||||||
target_compile_options(${target} PUBLIC -fconcepts)
|
|
||||||
endfunction()
|
|
||||||
|
|
||||||
include(metabench)
|
|
||||||
if(NOT METABENCH_DIR)
|
|
||||||
return()
|
|
||||||
endif()
|
|
||||||
|
|
||||||
enable_testing()
|
|
||||||
|
|
||||||
add_custom_target(metabench)
|
|
||||||
|
|
||||||
add_subdirectory(list)
|
|
||||||
add_subdirectory(make_dimension)
|
|
||||||
add_subdirectory(ratio)
|
|
@@ -1,65 +0,0 @@
|
|||||||
# 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.
|
|
||||||
|
|
||||||
cmake_minimum_required(VERSION 3.2)
|
|
||||||
|
|
||||||
add_metabench_test(
|
|
||||||
metabench.data.list.type_list.concepts_all "all concepts" type_list_concepts_all.cpp.erb "[3, 6, 9, 12, 15]"
|
|
||||||
)
|
|
||||||
add_metabench_test(
|
|
||||||
metabench.data.list.type_list.concepts_iface "concepts in interface" type_list_concepts_iface.cpp.erb
|
|
||||||
"[3, 6, 9, 12, 15]"
|
|
||||||
)
|
|
||||||
add_metabench_test(
|
|
||||||
metabench.data.list.type_list.concepts_no "no concepts" type_list_concepts_no.cpp.erb "[3, 6, 9, 12, 15]"
|
|
||||||
)
|
|
||||||
metabench_add_chart(
|
|
||||||
metabench.chart.list.concepts
|
|
||||||
TITLE "Sorting a list of size N"
|
|
||||||
SUBTITLE "(lower is better)"
|
|
||||||
DATASETS metabench.data.list.type_list.concepts_all metabench.data.list.type_list.concepts_iface
|
|
||||||
metabench.data.list.type_list.concepts_no
|
|
||||||
)
|
|
||||||
|
|
||||||
add_metabench_test(
|
|
||||||
metabench.data.list.type_list.conditional_std "std::conditional" type_list_conditional_std.cpp.erb
|
|
||||||
"[3, 6, 9, 12, 15]"
|
|
||||||
)
|
|
||||||
add_metabench_test(
|
|
||||||
metabench.data.list.type_list.conditional_alias "alias conditional" type_list_conditional_alias.cpp.erb
|
|
||||||
"[3, 6, 9, 12, 15]"
|
|
||||||
)
|
|
||||||
add_metabench_test(
|
|
||||||
metabench.data.list.type_list.conditional_alias_hard "alias conditional hard"
|
|
||||||
type_list_conditional_alias_hard.cpp.erb "[3, 6, 9, 12, 15]"
|
|
||||||
)
|
|
||||||
metabench_add_chart(
|
|
||||||
metabench.chart.list.conditional
|
|
||||||
TITLE "Sorting a list of size N"
|
|
||||||
SUBTITLE "(lower is better)"
|
|
||||||
DATASETS metabench.data.list.type_list.conditional_std metabench.data.list.type_list.conditional_alias
|
|
||||||
metabench.data.list.type_list.conditional_alias_hard
|
|
||||||
)
|
|
||||||
|
|
||||||
add_custom_target(metabench.chart.list DEPENDS metabench.chart.list.concepts metabench.chart.list.conditional)
|
|
||||||
|
|
||||||
add_dependencies(metabench metabench.chart.list)
|
|
@@ -1,34 +0,0 @@
|
|||||||
#include "type_list_concepts_all.h"
|
|
||||||
|
|
||||||
template<int UniqueValue>
|
|
||||||
using dim_id = std::integral_constant<int, UniqueValue>;
|
|
||||||
|
|
||||||
template<typename D1, typename D2>
|
|
||||||
using dim_id_less = std::bool_constant<D1::value < D2::value>;
|
|
||||||
|
|
||||||
template<typename... Es>
|
|
||||||
struct dimension;
|
|
||||||
|
|
||||||
<% (1..10).each do |k| %>
|
|
||||||
struct test<%= k %> {
|
|
||||||
|
|
||||||
<% (1..n).each do |i| %>
|
|
||||||
using <%= "dim#{i}" %> = dimension<<%=
|
|
||||||
xs = ((1)..(i)).map { |j| "dim_id<#{k*n+i+j}>" }
|
|
||||||
rng = Random.new(i)
|
|
||||||
xs.shuffle(random: rng).join(', ')
|
|
||||||
%>>;
|
|
||||||
#if defined(METABENCH)
|
|
||||||
using <%= "result#{i}" %> = units::type_list_sort_t<<%= "dim#{i}" %>, dim_id_less>;
|
|
||||||
#else
|
|
||||||
using <%= "result#{i}" %> = void;
|
|
||||||
#endif
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
}
|
|
@@ -1,184 +0,0 @@
|
|||||||
// 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 units {
|
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
template<bool>
|
|
||||||
struct conditional {
|
|
||||||
template<typename T, typename F>
|
|
||||||
using type = F;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<>
|
|
||||||
struct conditional<true> {
|
|
||||||
template<typename T, typename F>
|
|
||||||
using type = T;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<bool B, typename T, typename F>
|
|
||||||
using conditional_t = conditional<B>::template type<T, F>;
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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 TypeList = detail::is_type_list<T>;
|
|
||||||
|
|
||||||
// push_front
|
|
||||||
|
|
||||||
template<TypeList List, typename... Types>
|
|
||||||
struct type_list_push_front;
|
|
||||||
|
|
||||||
template<template<typename...> typename List, typename... OldTypes, typename... NewTypes>
|
|
||||||
struct type_list_push_front<List<OldTypes...>, NewTypes...> {
|
|
||||||
using type = List<NewTypes..., OldTypes...>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<TypeList List, typename... Types>
|
|
||||||
using type_list_push_front_t = type_list_push_front<List, Types...>::type;
|
|
||||||
|
|
||||||
// push_back
|
|
||||||
|
|
||||||
template<TypeList List, typename... Types>
|
|
||||||
struct type_list_push_back;
|
|
||||||
|
|
||||||
template<template<typename...> typename List, typename... OldTypes, typename... NewTypes>
|
|
||||||
struct type_list_push_back<List<OldTypes...>, NewTypes...> {
|
|
||||||
using type = List<OldTypes..., NewTypes...>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<TypeList List, typename... Types>
|
|
||||||
using type_list_push_back_t = type_list_push_back<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 base_first = base::first_list;
|
|
||||||
using base_second = base::second_list;
|
|
||||||
using first_list = detail::conditional_t < Idx<N, type_list_push_front_t<base_first, T>, base_first>;
|
|
||||||
using second_list = detail::conditional_t < Idx<N, base_second, type_list_push_front_t<base_second, T>>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // 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
|
|
||||||
|
|
||||||
template<TypeList SortedList1, TypeList SortedList2, template<typename, typename> typename Pred>
|
|
||||||
struct type_list_merge_sorted;
|
|
||||||
|
|
||||||
template<TypeList SortedList1, TypeList SortedList2, template<typename, typename> typename Pred>
|
|
||||||
using type_list_merge_sorted_t = type_list_merge_sorted<SortedList1, SortedList2, Pred>::type;
|
|
||||||
|
|
||||||
template<template<typename...> typename List, typename... Lhs, template<typename, typename> typename Pred>
|
|
||||||
struct type_list_merge_sorted<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<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<List<Lhs1, LhsRest...>, List<Rhs1, RhsRest...>, Pred> {
|
|
||||||
using type = detail::conditional_t<
|
|
||||||
Pred<Lhs1, Rhs1>::value,
|
|
||||||
type_list_push_front_t<type_list_merge_sorted_t<List<LhsRest...>, List<Rhs1, RhsRest...>, Pred>, Lhs1>,
|
|
||||||
type_list_push_front_t<type_list_merge_sorted_t<List<Lhs1, LhsRest...>, List<RhsRest...>, Pred>, Rhs1>>;
|
|
||||||
};
|
|
||||||
|
|
||||||
// sort
|
|
||||||
|
|
||||||
template<TypeList List, template<typename, typename> typename Pred>
|
|
||||||
struct type_list_sort;
|
|
||||||
|
|
||||||
template<template<typename...> typename List, template<typename, typename> typename Pred>
|
|
||||||
struct type_list_sort<List<>, Pred> {
|
|
||||||
using type = List<>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<template<typename...> typename List, typename T, template<typename, typename> typename Pred>
|
|
||||||
struct type_list_sort<List<T>, Pred> {
|
|
||||||
using type = List<T>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<template<typename...> typename List, typename... Types, template<typename, typename> typename Pred>
|
|
||||||
struct type_list_sort<List<Types...>, Pred> {
|
|
||||||
using types = List<Types...>;
|
|
||||||
using split = type_list_split_half<List<Types...>>;
|
|
||||||
using left = type_list_sort<typename split::first_list, Pred>::type;
|
|
||||||
using right = type_list_sort<typename split::second_list, Pred>::type;
|
|
||||||
using type = type_list_merge_sorted_t<left, right, Pred>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<TypeList List, template<typename, typename> typename Pred>
|
|
||||||
using type_list_sort_t = type_list_sort<List, Pred>::type;
|
|
||||||
|
|
||||||
} // namespace units
|
|
@@ -1,35 +0,0 @@
|
|||||||
#include "type_list_concepts_iface.h"
|
|
||||||
|
|
||||||
template<int UniqueValue>
|
|
||||||
using dim_id = std::integral_constant<int, UniqueValue>;
|
|
||||||
|
|
||||||
template<typename D1, typename D2>
|
|
||||||
using dim_id_less = std::bool_constant<D1::value < D2::value>;
|
|
||||||
|
|
||||||
|
|
||||||
template<typename... Es>
|
|
||||||
struct dimension;
|
|
||||||
|
|
||||||
<% (1..10).each do |k| %>
|
|
||||||
struct test<%= k %> {
|
|
||||||
|
|
||||||
<% (1..n).each do |i| %>
|
|
||||||
using <%= "dim#{i}" %> = dimension<<%=
|
|
||||||
xs = ((1)..(i)).map { |j| "dim_id<#{k*n+i+j}>" }
|
|
||||||
rng = Random.new(i)
|
|
||||||
xs.shuffle(random: rng).join(', ')
|
|
||||||
%>>;
|
|
||||||
#if defined(METABENCH)
|
|
||||||
using <%= "result#{i}" %> = units::type_list_sort<<%= "dim#{i}" %>, dim_id_less>;
|
|
||||||
#else
|
|
||||||
using <%= "result#{i}" %> = void;
|
|
||||||
#endif
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
}
|
|
@@ -1,205 +0,0 @@
|
|||||||
// 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 units {
|
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
template<bool>
|
|
||||||
struct conditional {
|
|
||||||
template<typename T, typename F>
|
|
||||||
using type = F;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<>
|
|
||||||
struct conditional<true> {
|
|
||||||
template<typename T, typename F>
|
|
||||||
using type = T;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<bool B, typename T, typename F>
|
|
||||||
using conditional = detail::conditional<B>::template type<T, F>;
|
|
||||||
|
|
||||||
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 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...>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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...>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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 =
|
|
||||||
units::conditional <
|
|
||||||
Idx<N, typename type_list_push_front_impl<typename base::first_list, T>::type, typename base::first_list>;
|
|
||||||
using second_list =
|
|
||||||
units::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 = units::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>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<TypeList List, template<typename, typename> typename Pred>
|
|
||||||
using type_list_sort = detail::type_list_sort_impl<List, Pred>::type;
|
|
||||||
|
|
||||||
} // namespace units
|
|
@@ -1,35 +0,0 @@
|
|||||||
#include "type_list_concepts_no.h"
|
|
||||||
|
|
||||||
template<int UniqueValue>
|
|
||||||
using dim_id = std::integral_constant<int, UniqueValue>;
|
|
||||||
|
|
||||||
template<typename D1, typename D2>
|
|
||||||
using dim_id_less = std::bool_constant<D1::value < D2::value>;
|
|
||||||
|
|
||||||
|
|
||||||
template<typename... Es>
|
|
||||||
struct dimension;
|
|
||||||
|
|
||||||
<% (1..10).each do |k| %>
|
|
||||||
struct test<%= k %> {
|
|
||||||
|
|
||||||
<% (1..n).each do |i| %>
|
|
||||||
using <%= "dim#{i}" %> = dimension<<%=
|
|
||||||
xs = ((1)..(i)).map { |j| "dim_id<#{k*n+i+j}>" }
|
|
||||||
rng = Random.new(i)
|
|
||||||
xs.shuffle(random: rng).join(', ')
|
|
||||||
%>>;
|
|
||||||
#if defined(METABENCH)
|
|
||||||
using <%= "result#{i}" %> = units::type_list_sort<<%= "dim#{i}" %>, dim_id_less>;
|
|
||||||
#else
|
|
||||||
using <%= "result#{i}" %> = void;
|
|
||||||
#endif
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
}
|
|
@@ -1,192 +0,0 @@
|
|||||||
// 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 units {
|
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
template<bool>
|
|
||||||
struct conditional {
|
|
||||||
template<typename T, typename F>
|
|
||||||
using type = F;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<>
|
|
||||||
struct conditional<true> {
|
|
||||||
template<typename T, typename F>
|
|
||||||
using type = T;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<bool B, typename T, typename F>
|
|
||||||
using conditional = detail::conditional<B>::template type<T, F>;
|
|
||||||
|
|
||||||
// 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...>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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...>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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 =
|
|
||||||
units::conditional <
|
|
||||||
Idx<N, typename type_list_push_front_impl<typename base::first_list, T>::type, typename base::first_list>;
|
|
||||||
using second_list =
|
|
||||||
units::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 = units::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>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<typename List, template<typename, typename> typename Pred>
|
|
||||||
using type_list_sort = detail::type_list_sort_impl<List, Pred>::type;
|
|
||||||
|
|
||||||
} // namespace units
|
|
@@ -1,35 +0,0 @@
|
|||||||
#include "type_list_conditional_alias.h"
|
|
||||||
|
|
||||||
template<int UniqueValue>
|
|
||||||
using dim_id = std::integral_constant<int, UniqueValue>;
|
|
||||||
|
|
||||||
template<typename D1, typename D2>
|
|
||||||
using dim_id_less = std::bool_constant<D1::value < D2::value>;
|
|
||||||
|
|
||||||
|
|
||||||
template<typename... Es>
|
|
||||||
struct dimension;
|
|
||||||
|
|
||||||
<% (1..10).each do |k| %>
|
|
||||||
struct test<%= k %> {
|
|
||||||
|
|
||||||
<% (1..n).each do |i| %>
|
|
||||||
using <%= "dim#{i}" %> = dimension<<%=
|
|
||||||
xs = ((1)..(i)).map { |j| "dim_id<#{k*n+i+j}>" }
|
|
||||||
rng = Random.new(i)
|
|
||||||
xs.shuffle(random: rng).join(', ')
|
|
||||||
%>>;
|
|
||||||
#if defined(METABENCH)
|
|
||||||
using <%= "result#{i}" %> = units::type_list_sort<<%= "dim#{i}" %>, dim_id_less>;
|
|
||||||
#else
|
|
||||||
using <%= "result#{i}" %> = void;
|
|
||||||
#endif
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
}
|
|
@@ -1,205 +0,0 @@
|
|||||||
// 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 units {
|
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
template<bool>
|
|
||||||
struct conditional {
|
|
||||||
template<typename T, typename F>
|
|
||||||
using type = F;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<>
|
|
||||||
struct conditional<true> {
|
|
||||||
template<typename T, typename F>
|
|
||||||
using type = T;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<bool B, typename T, typename F>
|
|
||||||
using conditional = detail::conditional<B>::template type<T, F>;
|
|
||||||
|
|
||||||
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 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...>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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...>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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 =
|
|
||||||
units::conditional <
|
|
||||||
Idx<N, typename type_list_push_front_impl<typename base::first_list, T>::type, typename base::first_list>;
|
|
||||||
using second_list =
|
|
||||||
units::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 = units::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>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<TypeList List, template<typename, typename> typename Pred>
|
|
||||||
using type_list_sort = detail::type_list_sort_impl<List, Pred>::type;
|
|
||||||
|
|
||||||
} // namespace units
|
|
@@ -1,35 +0,0 @@
|
|||||||
#include "type_list_conditional_alias_hard.h"
|
|
||||||
|
|
||||||
template<int UniqueValue>
|
|
||||||
using dim_id = std::integral_constant<int, UniqueValue>;
|
|
||||||
|
|
||||||
template<typename D1, typename D2>
|
|
||||||
using dim_id_less = std::bool_constant<D1::value < D2::value>;
|
|
||||||
|
|
||||||
|
|
||||||
template<typename... Es>
|
|
||||||
struct dimension;
|
|
||||||
|
|
||||||
<% (1..10).each do |k| %>
|
|
||||||
struct test<%= k %> {
|
|
||||||
|
|
||||||
<% (1..n).each do |i| %>
|
|
||||||
using <%= "dim#{i}" %> = dimension<<%=
|
|
||||||
xs = ((1)..(i)).map { |j| "dim_id<#{k*n+i+j}>" }
|
|
||||||
rng = Random.new(i)
|
|
||||||
xs.shuffle(random: rng).join(', ')
|
|
||||||
%>>;
|
|
||||||
#if defined(METABENCH)
|
|
||||||
using <%= "result#{i}" %> = units::type_list_sort<<%= "dim#{i}" %>, dim_id_less>;
|
|
||||||
#else
|
|
||||||
using <%= "result#{i}" %> = void;
|
|
||||||
#endif
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
}
|
|
@@ -1,197 +0,0 @@
|
|||||||
// 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 units {
|
|
||||||
|
|
||||||
template<bool>
|
|
||||||
struct conditional {
|
|
||||||
template<typename T, typename F>
|
|
||||||
using type = F;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<>
|
|
||||||
struct conditional<true> {
|
|
||||||
template<typename T, typename F>
|
|
||||||
using type = T;
|
|
||||||
};
|
|
||||||
|
|
||||||
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 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...>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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...>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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>::template type<typename type_list_push_front_impl<typename base::first_list, T>::type,
|
|
||||||
typename base::first_list>;
|
|
||||||
using second_list =
|
|
||||||
conditional < Idx<N>::template type<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 = units::conditional<Pred<Lhs1, Rhs1>::value>::template type<
|
|
||||||
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>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<TypeList List, template<typename, typename> typename Pred>
|
|
||||||
using type_list_sort = detail::type_list_sort_impl<List, Pred>::type;
|
|
||||||
|
|
||||||
} // namespace units
|
|
@@ -1,35 +0,0 @@
|
|||||||
#include "type_list_conditional_std.h"
|
|
||||||
|
|
||||||
template<int UniqueValue>
|
|
||||||
using dim_id = std::integral_constant<int, UniqueValue>;
|
|
||||||
|
|
||||||
template<typename D1, typename D2>
|
|
||||||
using dim_id_less = std::bool_constant<D1::value < D2::value>;
|
|
||||||
|
|
||||||
|
|
||||||
template<typename... Es>
|
|
||||||
struct dimension;
|
|
||||||
|
|
||||||
<% (1..10).each do |k| %>
|
|
||||||
struct test<%= k %> {
|
|
||||||
|
|
||||||
<% (1..n).each do |i| %>
|
|
||||||
using <%= "dim#{i}" %> = dimension<<%=
|
|
||||||
xs = ((1)..(i)).map { |j| "dim_id<#{k*n+i+j}>" }
|
|
||||||
rng = Random.new(i)
|
|
||||||
xs.shuffle(random: rng).join(', ')
|
|
||||||
%>>;
|
|
||||||
#if defined(METABENCH)
|
|
||||||
using <%= "result#{i}" %> = units::type_list_sort<<%= "dim#{i}" %>, dim_id_less>;
|
|
||||||
#else
|
|
||||||
using <%= "result#{i}" %> = void;
|
|
||||||
#endif
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
}
|
|
@@ -1,186 +0,0 @@
|
|||||||
// 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 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 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...>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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...>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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 =
|
|
||||||
std::conditional_t <
|
|
||||||
Idx<N, typename type_list_push_front_impl<typename base::first_list, T>::type, typename base::first_list>;
|
|
||||||
using second_list =
|
|
||||||
std::conditional_t <
|
|
||||||
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 = std::conditional_t<
|
|
||||||
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>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<TypeList List, template<typename, typename> typename Pred>
|
|
||||||
using type_list_sort = detail::type_list_sort_impl<List, Pred>::type;
|
|
||||||
|
|
||||||
} // namespace units
|
|
@@ -1,40 +0,0 @@
|
|||||||
# 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.
|
|
||||||
|
|
||||||
cmake_minimum_required(VERSION 3.2)
|
|
||||||
|
|
||||||
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)
|
|
@@ -1,23 +0,0 @@
|
|||||||
#include "dimension_concepts_all.h"
|
|
||||||
|
|
||||||
<% (1..100).each do |k| %>
|
|
||||||
struct test<%= k %> {
|
|
||||||
<% (1..n).each do |i| %>
|
|
||||||
static constexpr units::base_dimension dim<%= i %>{"dim<%= i %>"};
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
#if defined(METABENCH)
|
|
||||||
using dim = units::make_dimension_t<<%=
|
|
||||||
xs = ((1)..(n)).map { |j| "units::exponent<dim#{j}, 1>" }
|
|
||||||
rng = Random.new(k)
|
|
||||||
xs.shuffle(random: rng).join(', ')
|
|
||||||
%>>;
|
|
||||||
#else
|
|
||||||
using dim = void;
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
}
|
|
@@ -1,23 +0,0 @@
|
|||||||
#include "dimension_concepts_iface.h"
|
|
||||||
|
|
||||||
<% (1..100).each do |k| %>
|
|
||||||
struct test<%= k %> {
|
|
||||||
<% (1..n).each do |i| %>
|
|
||||||
static constexpr units::base_dimension dim<%= i %>{"dim<%= i %>"};
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
#if defined(METABENCH)
|
|
||||||
using dim = units::make_dimension_t<<%=
|
|
||||||
xs = ((1)..(n)).map { |j| "units::exponent<dim#{j}, 1>" }
|
|
||||||
rng = Random.new(k)
|
|
||||||
xs.shuffle(random: rng).join(', ')
|
|
||||||
%>>;
|
|
||||||
#else
|
|
||||||
using dim = void;
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
}
|
|
@@ -1,211 +0,0 @@
|
|||||||
// 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 "downcasting_concepts_all.h"
|
|
||||||
#include "ratio_concepts_all.h"
|
|
||||||
#include "type_list_concepts_all.h"
|
|
||||||
#include <ratio>
|
|
||||||
|
|
||||||
namespace 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> {};
|
|
||||||
|
|
||||||
// exponent
|
|
||||||
|
|
||||||
template<const base_dimension& BaseDimension, std::intmax_t Num, std::intmax_t Den = 1>
|
|
||||||
struct exponent {
|
|
||||||
static constexpr const base_dimension& dimension = BaseDimension;
|
|
||||||
static constexpr std::intmax_t num = Num;
|
|
||||||
static constexpr std::intmax_t den = Den;
|
|
||||||
};
|
|
||||||
|
|
||||||
// is_exponent
|
|
||||||
namespace detail {
|
|
||||||
template<typename T>
|
|
||||||
inline constexpr bool is_exponent = false;
|
|
||||||
|
|
||||||
template<const base_dimension& BaseDimension, std::intmax_t Num, std::intmax_t Den>
|
|
||||||
inline constexpr bool is_exponent<exponent<BaseDimension, Num, Den>> = true;
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
concept Exponent = detail::is_exponent<T>;
|
|
||||||
|
|
||||||
// exp_dim_id_less
|
|
||||||
|
|
||||||
template<Exponent E1, Exponent E2>
|
|
||||||
struct exponent_less : base_dimension_less<E1::dimension, E2::dimension> {};
|
|
||||||
|
|
||||||
// exponent_invert
|
|
||||||
|
|
||||||
template<Exponent E>
|
|
||||||
struct exponent_invert;
|
|
||||||
|
|
||||||
template<const base_dimension& BaseDimension, std::intmax_t Num, std::intmax_t Den>
|
|
||||||
struct exponent_invert<exponent<BaseDimension, Num, Den>> {
|
|
||||||
using type = exponent<BaseDimension, -Num, Den>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<Exponent E>
|
|
||||||
using exponent_invert_t = exponent_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 Dimension = std::is_empty_v<T> && detail::is_dimension<downcast_base_t<T>>;
|
|
||||||
|
|
||||||
|
|
||||||
// dim_invert
|
|
||||||
|
|
||||||
template<Dimension E>
|
|
||||||
struct dim_invert;
|
|
||||||
|
|
||||||
template<Exponent... Es>
|
|
||||||
struct dim_invert<dimension<Es...>> : std::type_identity<downcast_traits_t<dimension<exponent_invert_t<Es>...>>> {};
|
|
||||||
|
|
||||||
template<Dimension D>
|
|
||||||
using dim_invert_t = dim_invert<typename D::downcast_base_type>::type;
|
|
||||||
|
|
||||||
|
|
||||||
// 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<is_same_v<rest, dimension<>>, dimension<E1>, type_list_push_front<rest, E1>>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<const base_dimension& D, std::intmax_t Num1, std::intmax_t Den1, std::intmax_t Num2, std::intmax_t Den2,
|
|
||||||
Exponent... ERest>
|
|
||||||
struct dim_consolidate<dimension<exponent<D, Num1, Den1>, exponent<D, Num2, Den2>, ERest...>> {
|
|
||||||
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<exponent<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...>, exponent_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, exponent_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<downcast_traits_t<merge_dimension_t<dimension<E1...>, dimension<E2...>>>> {};
|
|
||||||
|
|
||||||
template<Dimension D1, Dimension D2>
|
|
||||||
using dimension_multiply_t = dimension_multiply<typename D1::downcast_base_type, typename D2::downcast_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<exponent_invert_t<E2>...>> {};
|
|
||||||
|
|
||||||
template<Dimension D1, Dimension D2>
|
|
||||||
using dimension_divide_t = dimension_divide<typename D1::downcast_base_type, typename D2::downcast_base_type>::type;
|
|
||||||
|
|
||||||
} // namespace units
|
|
@@ -1,211 +0,0 @@
|
|||||||
// 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 "downcasting_concepts_all.h"
|
|
||||||
#include "ratio_concepts_iface.h"
|
|
||||||
#include "type_list_concepts_iface.h"
|
|
||||||
#include <ratio>
|
|
||||||
|
|
||||||
namespace 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> {};
|
|
||||||
|
|
||||||
// exponent
|
|
||||||
|
|
||||||
template<const base_dimension& BaseDimension, std::intmax_t Num, std::intmax_t Den = 1>
|
|
||||||
struct exponent {
|
|
||||||
static constexpr const base_dimension& dimension = BaseDimension;
|
|
||||||
static constexpr int num = Num;
|
|
||||||
static constexpr int den = Den;
|
|
||||||
};
|
|
||||||
|
|
||||||
// is_exponent
|
|
||||||
namespace detail {
|
|
||||||
template<typename T>
|
|
||||||
inline constexpr bool is_exponent = false;
|
|
||||||
|
|
||||||
template<const base_dimension& BaseDimension, std::intmax_t Num, std::intmax_t Den>
|
|
||||||
inline constexpr bool is_exponent<exponent<BaseDimension, Num, Den>> = true;
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
concept Exponent = detail::is_exponent<T>;
|
|
||||||
|
|
||||||
// exp_dim_id_less
|
|
||||||
|
|
||||||
template<Exponent E1, Exponent E2>
|
|
||||||
struct exponent_less : base_dimension_less<E1::dimension, E2::dimension> {};
|
|
||||||
|
|
||||||
// exponent_invert
|
|
||||||
|
|
||||||
template<Exponent E>
|
|
||||||
struct exponent_invert;
|
|
||||||
|
|
||||||
template<const base_dimension& BaseDimension, std::intmax_t Num, std::intmax_t Den>
|
|
||||||
struct exponent_invert<exponent<BaseDimension, Num, Den>> {
|
|
||||||
using type = exponent<BaseDimension, -Num, Den>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<Exponent E>
|
|
||||||
using exponent_invert_t = exponent_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 Dimension = std::is_empty_v<T> && detail::is_dimension<downcast_base_t<T>>;
|
|
||||||
|
|
||||||
|
|
||||||
// dim_invert
|
|
||||||
|
|
||||||
template<Dimension E>
|
|
||||||
struct dim_invert;
|
|
||||||
|
|
||||||
template<typename... Es>
|
|
||||||
struct dim_invert<dimension<Es...>> : std::type_identity<downcast_traits_t<dimension<exponent_invert_t<Es>...>>> {};
|
|
||||||
|
|
||||||
template<Dimension D>
|
|
||||||
using dim_invert_t = dim_invert<typename D::downcast_base_type>::type;
|
|
||||||
|
|
||||||
|
|
||||||
// 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<is_same_v<rest, dimension<>>, dimension<E1>, type_list_push_front<rest, E1>>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<const base_dimension& D, std::intmax_t Num1, std::intmax_t Den1, std::intmax_t Num2, std::intmax_t Den2,
|
|
||||||
typename... ERest>
|
|
||||||
struct dim_consolidate<dimension<exponent<D, Num1, Den1>, exponent<D, Num2, Den2>, ERest...>> {
|
|
||||||
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<exponent<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...>, exponent_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, exponent_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<downcast_traits_t<merge_dimension_t<dimension<E1...>, dimension<E2...>>>> {};
|
|
||||||
|
|
||||||
template<Dimension D1, Dimension D2>
|
|
||||||
using dimension_multiply_t = dimension_multiply<typename D1::downcast_base_type, typename D2::downcast_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<exponent_invert_t<E2>...>> {};
|
|
||||||
|
|
||||||
template<Dimension D1, Dimension D2>
|
|
||||||
using dimension_divide_t = dimension_divide<typename D1::downcast_base_type, typename D2::downcast_base_type>::type;
|
|
||||||
|
|
||||||
} // namespace units
|
|
@@ -1,184 +0,0 @@
|
|||||||
// 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 "downcasting_no_concepts.h"
|
|
||||||
#include "ratio_no_concepts.h"
|
|
||||||
#include "type_list_no_concepts.h"
|
|
||||||
#include <ratio>
|
|
||||||
|
|
||||||
namespace 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> {};
|
|
||||||
|
|
||||||
// exponent
|
|
||||||
|
|
||||||
template<const base_dimension& BaseDimension, std::intmax_t Num, std::intmax_t Den = 1>
|
|
||||||
struct exponent {
|
|
||||||
static constexpr const base_dimension& dimension = BaseDimension;
|
|
||||||
static constexpr std::intmax_t num = Num;
|
|
||||||
static constexpr std::intmax_t den = Den;
|
|
||||||
};
|
|
||||||
|
|
||||||
// exp_dim_id_less
|
|
||||||
|
|
||||||
template<typename E1, typename E2>
|
|
||||||
struct exponent_less : base_dimension_less<E1::dimension, E2::dimension> {};
|
|
||||||
|
|
||||||
// exponent_invert
|
|
||||||
|
|
||||||
template<typename E>
|
|
||||||
struct exponent_invert;
|
|
||||||
|
|
||||||
template<const base_dimension& BaseDimension, std::intmax_t Num, std::intmax_t Den>
|
|
||||||
struct exponent_invert<exponent<BaseDimension, Num, Den>> {
|
|
||||||
using type = exponent<BaseDimension, -Num, Den>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename E>
|
|
||||||
using exponent_invert_t = exponent_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<downcast_traits_t<dimension<exponent_invert_t<Es>...>>> {};
|
|
||||||
|
|
||||||
template<typename D>
|
|
||||||
using dim_invert_t = dim_invert<typename D::downcast_base_type>::type;
|
|
||||||
|
|
||||||
|
|
||||||
// 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<is_same_v<rest, dimension<>>, dimension<E1>, type_list_push_front<rest, E1>>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<const base_dimension& D, std::intmax_t Num1, std::intmax_t Den1, std::intmax_t Num2, std::intmax_t Den2,
|
|
||||||
typename... ERest>
|
|
||||||
struct dim_consolidate<dimension<exponent<D, Num1, Den1>, exponent<D, Num2, Den2>, ERest...>> {
|
|
||||||
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<exponent<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...>, exponent_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, exponent_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<downcast_traits_t<merge_dimension_t<dimension<E1...>, dimension<E2...>>>> {};
|
|
||||||
|
|
||||||
template<typename D1, typename D2>
|
|
||||||
using dimension_multiply_t = dimension_multiply<typename D1::downcast_base_type, typename D2::downcast_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<exponent_invert_t<E2>...>> {};
|
|
||||||
|
|
||||||
template<typename D1, typename D2>
|
|
||||||
using dimension_divide_t = dimension_divide<typename D1::downcast_base_type, typename D2::downcast_base_type>::type;
|
|
||||||
|
|
||||||
} // namespace units
|
|
@@ -1,48 +0,0 @@
|
|||||||
// 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 units {
|
|
||||||
|
|
||||||
template<typename BaseType>
|
|
||||||
struct downcast_base {
|
|
||||||
using downcast_base_type = BaseType;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
concept Downcastable =
|
|
||||||
requires { typename T::downcast_base_type; } && std::derived_from<T, downcast_base<typename T::downcast_base_type>>;
|
|
||||||
|
|
||||||
template<Downcastable T>
|
|
||||||
using downcast_base_t = T::downcast_base_type;
|
|
||||||
|
|
||||||
template<Downcastable T>
|
|
||||||
struct downcast_traits : std::type_identity<T> {};
|
|
||||||
|
|
||||||
template<Downcastable T>
|
|
||||||
using downcast_traits_t = downcast_traits<T>::type;
|
|
||||||
|
|
||||||
} // namespace units
|
|
@@ -1,44 +0,0 @@
|
|||||||
// 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 units {
|
|
||||||
|
|
||||||
template<typename BaseType>
|
|
||||||
struct downcast_base {
|
|
||||||
using downcast_base_type = BaseType;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
using downcast_base_t = T::downcast_base_type;
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
struct downcast_traits : std::type_identity<T> {};
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
using downcast_traits_t = downcast_traits<T>::type;
|
|
||||||
|
|
||||||
} // namespace units
|
|
@@ -1,36 +0,0 @@
|
|||||||
// 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 <concepts/concepts.hpp>
|
|
||||||
|
|
||||||
namespace std {
|
|
||||||
|
|
||||||
// concepts
|
|
||||||
using concepts::convertible_to;
|
|
||||||
using concepts::derived_from;
|
|
||||||
using concepts::regular;
|
|
||||||
using concepts::same_as;
|
|
||||||
using concepts::totally_ordered;
|
|
||||||
|
|
||||||
} // namespace std
|
|
@@ -1,23 +0,0 @@
|
|||||||
#include "dimension_no_concepts.h"
|
|
||||||
|
|
||||||
<% (1..100).each do |k| %>
|
|
||||||
struct test<%= k %> {
|
|
||||||
<% (1..n).each do |i| %>
|
|
||||||
static constexpr units::base_dimension dim<%= i %>{"dim<%= i %>"};
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
#if defined(METABENCH)
|
|
||||||
using dim = units::make_dimension_t<<%=
|
|
||||||
xs = ((1)..(n)).map { |j| "units::exponent<dim#{j}, 1>" }
|
|
||||||
rng = Random.new(k)
|
|
||||||
xs.shuffle(random: rng).join(', ')
|
|
||||||
%>>;
|
|
||||||
#else
|
|
||||||
using dim = void;
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
}
|
|
@@ -1,140 +0,0 @@
|
|||||||
// 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 <cstdint>
|
|
||||||
#include <numeric>
|
|
||||||
#include <type_traits>
|
|
||||||
|
|
||||||
namespace units {
|
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
[[nodiscard]] constexpr T abs(T v) noexcept
|
|
||||||
{
|
|
||||||
return v < 0 ? -v : v;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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 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;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<Ratio R1, Ratio R2>
|
|
||||||
using ratio_divide = detail::ratio_divide_impl<R1, R2>::type;
|
|
||||||
|
|
||||||
// common_ratio
|
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
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>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<Ratio R1, Ratio R2>
|
|
||||||
using common_ratio = detail::common_ratio_impl<R1, R2>::type;
|
|
||||||
|
|
||||||
} // namespace units
|
|
@@ -1,140 +0,0 @@
|
|||||||
// 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 <cstdint>
|
|
||||||
#include <numeric>
|
|
||||||
#include <type_traits>
|
|
||||||
|
|
||||||
namespace units {
|
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
[[nodiscard]] constexpr T abs(T v) noexcept
|
|
||||||
{
|
|
||||||
return v < 0 ? -v : v;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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 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;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<Ratio R1, Ratio R2>
|
|
||||||
using ratio_divide = detail::ratio_divide_impl<R1, R2>::type;
|
|
||||||
|
|
||||||
// common_ratio
|
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
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>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<Ratio R1, Ratio R2>
|
|
||||||
using common_ratio = detail::common_ratio_impl<R1, R2>::type;
|
|
||||||
|
|
||||||
} // namespace units
|
|
@@ -1,125 +0,0 @@
|
|||||||
// 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 <cstdint>
|
|
||||||
#include <numeric>
|
|
||||||
#include <type_traits>
|
|
||||||
|
|
||||||
namespace units {
|
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
[[nodiscard]] constexpr T abs(T v) noexcept
|
|
||||||
{
|
|
||||||
return v < 0 ? -v : v;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<typename R1, typename R2>
|
|
||||||
using ratio_divide = detail::ratio_divide_impl<R1, R2>::type;
|
|
||||||
|
|
||||||
// common_ratio
|
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
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>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<typename R1, typename R2>
|
|
||||||
using common_ratio = detail::common_ratio_impl<R1, R2>::type;
|
|
||||||
|
|
||||||
} // namespace units
|
|
@@ -1,186 +0,0 @@
|
|||||||
// 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 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 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...>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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...>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<TypeList List, template<typename, typename> typename Pred>
|
|
||||||
using type_list_sort = detail::type_list_sort_impl<List, Pred>::type;
|
|
||||||
|
|
||||||
} // namespace units
|
|
@@ -1,186 +0,0 @@
|
|||||||
// 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 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 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...>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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...>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<TypeList List, template<typename, typename> typename Pred>
|
|
||||||
using type_list_sort = detail::type_list_sort_impl<List, Pred>::type;
|
|
||||||
|
|
||||||
} // namespace units
|
|
@@ -1,173 +0,0 @@
|
|||||||
// 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 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...>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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...>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<typename List, template<typename, typename> typename Pred>
|
|
||||||
using type_list_sort = detail::type_list_sort_impl<List, Pred>::type;
|
|
||||||
|
|
||||||
} // namespace units
|
|
@@ -1,48 +0,0 @@
|
|||||||
// 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 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;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<bool B, typename T, typename F>
|
|
||||||
using conditional = TYPENAME detail::conditional_impl<B>::template type<T, F>;
|
|
||||||
|
|
||||||
} // namespace units
|
|
@@ -1,86 +0,0 @@
|
|||||||
# 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.
|
|
||||||
|
|
||||||
cmake_minimum_required(VERSION 3.2)
|
|
||||||
|
|
||||||
add_metabench_test(
|
|
||||||
metabench.data.ratio.create.std_ratio "std::ratio" create_std_ratio.cpp.erb "[1000, 2500, 5000, 7500, 10000]"
|
|
||||||
)
|
|
||||||
add_metabench_test(
|
|
||||||
metabench.data.ratio.create.ratio_type_constexpr "ratio with constexpr" create_ratio_type_constexpr.cpp.erb
|
|
||||||
"[1000, 2500, 5000, 7500, 10000]"
|
|
||||||
)
|
|
||||||
metabench_add_chart(
|
|
||||||
metabench.chart.ratio.create
|
|
||||||
TITLE "Creation of 2*N ratios"
|
|
||||||
SUBTITLE "(lower is better)"
|
|
||||||
DATASETS metabench.data.ratio.create.std_ratio metabench.data.ratio.create.ratio_type_constexpr
|
|
||||||
)
|
|
||||||
|
|
||||||
add_metabench_test(
|
|
||||||
metabench.data.ratio.multiply_divide.std_ratio "std::ratio" multiply_divide_std_ratio.cpp.erb
|
|
||||||
"[10, 50, 100, 250, 500, 750, 1000, 1500, 2000, 3000, 4000, 5000]"
|
|
||||||
)
|
|
||||||
add_metabench_test(
|
|
||||||
metabench.data.ratio.multiply_divide.ratio_type_constexpr "ratio constexpr"
|
|
||||||
multiply_divide_ratio_type_constexpr.cpp.erb "[10, 50, 100, 250, 500, 750, 1000, 1500, 2000, 3000, 4000, 5000]"
|
|
||||||
)
|
|
||||||
metabench_add_chart(
|
|
||||||
metabench.chart.ratio.multiply_divide
|
|
||||||
TITLE "N ratio multiply + divide operations"
|
|
||||||
SUBTITLE "(lower is better)"
|
|
||||||
DATASETS metabench.data.ratio.multiply_divide.std_ratio metabench.data.ratio.multiply_divide.ratio_type_constexpr
|
|
||||||
)
|
|
||||||
|
|
||||||
add_metabench_test(
|
|
||||||
metabench.data.ratio.common_ratio.std_ratio "std::ratio" common_ratio_std_ratio.cpp.erb
|
|
||||||
"[10, 50, 100, 250, 500, 750, 1000, 1500, 2000, 3000, 4000, 5000]"
|
|
||||||
)
|
|
||||||
add_metabench_test(
|
|
||||||
metabench.data.ratio.common_ratio.ratio_type_constexpr "ratio constexpr" common_ratio_ratio_type_constexpr.cpp.erb
|
|
||||||
"[10, 50, 100, 250, 500, 750, 1000, 1500, 2000, 3000, 4000, 5000]"
|
|
||||||
)
|
|
||||||
metabench_add_chart(
|
|
||||||
metabench.chart.ratio.common_ratio
|
|
||||||
TITLE "N common_ratio operations"
|
|
||||||
SUBTITLE "(lower is better)"
|
|
||||||
DATASETS metabench.data.ratio.common_ratio.std_ratio metabench.data.ratio.common_ratio.ratio_type_constexpr
|
|
||||||
)
|
|
||||||
|
|
||||||
add_metabench_test(metabench.data.ratio.all.std_ratio "std::ratio" all_std_ratio.cpp.erb "[10, 50, 100, 500, 1000]")
|
|
||||||
add_metabench_test(
|
|
||||||
metabench.data.ratio.all.ratio_type_constexpr "ratio with constexpr" all_ratio_type_constexpr.cpp.erb
|
|
||||||
"[10, 50, 100, 500, 1000]"
|
|
||||||
)
|
|
||||||
metabench_add_chart(
|
|
||||||
metabench.chart.ratio.all
|
|
||||||
TITLE "N x all ratio operations"
|
|
||||||
SUBTITLE "(lower is better)"
|
|
||||||
DATASETS metabench.data.ratio.all.std_ratio metabench.data.ratio.all.ratio_type_constexpr
|
|
||||||
)
|
|
||||||
|
|
||||||
add_custom_target(
|
|
||||||
metabench.chart.ratio DEPENDS metabench.chart.ratio.create metabench.chart.ratio.multiply_divide
|
|
||||||
metabench.chart.ratio.common_ratio metabench.chart.ratio.all
|
|
||||||
)
|
|
||||||
|
|
||||||
add_dependencies(metabench metabench.chart.ratio)
|
|
@@ -1,26 +0,0 @@
|
|||||||
#include "ratio_type_constexpr.h"
|
|
||||||
|
|
||||||
<% (1..n).each do |i| %>
|
|
||||||
struct test<%= i %> {
|
|
||||||
#if defined(METABENCH)
|
|
||||||
using r1 = units::ratio<<%= 2 * i - 1 %>, <%= 2 * n %>>;
|
|
||||||
using r2 = units::ratio<<%= 2 * i %>, <%= 2 * n %>>;
|
|
||||||
|
|
||||||
using r3 = units::ratio_multiply<r1, r2>;
|
|
||||||
using r4 = units::ratio_divide<r1, r2>;
|
|
||||||
|
|
||||||
using r5 = units::common_ratio_t<r1, r2>;
|
|
||||||
#else
|
|
||||||
using r1 = void;
|
|
||||||
using r2 = void;
|
|
||||||
using r3 = void;
|
|
||||||
using r4 = void;
|
|
||||||
using r5 = void;
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
}
|
|
@@ -1,26 +0,0 @@
|
|||||||
#include "std_ratio.h"
|
|
||||||
|
|
||||||
<% (1..n).each do |i| %>
|
|
||||||
struct test<%= i %> {
|
|
||||||
#if defined(METABENCH)
|
|
||||||
using r1 = std::ratio<<%= 2 * i - 1 %>, <%= 2 * n %>>;
|
|
||||||
using r2 = std::ratio<<%= 2 * i %>, <%= 2 * n %>>;
|
|
||||||
|
|
||||||
using r3 = std::ratio_multiply<r1, r2>;
|
|
||||||
using r4 = std::ratio_divide<r1, r2>;
|
|
||||||
|
|
||||||
using r5 = units::common_ratio_t<r1, r2>;
|
|
||||||
#else
|
|
||||||
using r1 = void;
|
|
||||||
using r2 = void;
|
|
||||||
using r3 = void;
|
|
||||||
using r4 = void;
|
|
||||||
using r5 = void;
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
}
|
|
@@ -1,19 +0,0 @@
|
|||||||
#include "ratio_type_constexpr.h"
|
|
||||||
|
|
||||||
<% (1..n).each do |i| %>
|
|
||||||
struct test<%= i %> {
|
|
||||||
using r1 = units::ratio<<%= 2 * i - 1 %>, <%= 2 * n %>>;
|
|
||||||
using r2 = units::ratio<<%= 2 * i %>, <%= 2 * n %>>;
|
|
||||||
|
|
||||||
#if defined(METABENCH)
|
|
||||||
using r3 = units::common_ratio_t<r1, r2>;
|
|
||||||
#else
|
|
||||||
using r3 = void;
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
}
|
|
@@ -1,19 +0,0 @@
|
|||||||
#include "std_ratio.h"
|
|
||||||
|
|
||||||
<% (1..n).each do |i| %>
|
|
||||||
struct test<%= i %> {
|
|
||||||
using r1 = std::ratio<<%= 2 * i - 1 %>, <%= 2 * n %>>;
|
|
||||||
using r2 = std::ratio<<%= 2 * i %>, <%= 2 * n %>>;
|
|
||||||
|
|
||||||
#if defined(METABENCH)
|
|
||||||
using r3 = units::common_ratio_t<r1, r2>;
|
|
||||||
#else
|
|
||||||
using r3 = void;
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
}
|
|
@@ -1,18 +0,0 @@
|
|||||||
#include "ratio_type_constexpr.h"
|
|
||||||
|
|
||||||
<% (1..n).each do |i| %>
|
|
||||||
struct test<%= i %> {
|
|
||||||
#if defined(METABENCH)
|
|
||||||
using r1 = units::ratio<<%= 2 * i - 1 %>, <%= 2 * n %>>;
|
|
||||||
using r2 = units::ratio<<%= 2 * i %>, <%= 2 * n %>>;
|
|
||||||
#else
|
|
||||||
using r1 = void;
|
|
||||||
using r2 = void;
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
}
|
|
@@ -1,18 +0,0 @@
|
|||||||
#include <ratio>
|
|
||||||
|
|
||||||
<% (1..n).each do |i| %>
|
|
||||||
struct test<%= i %> {
|
|
||||||
#if defined(METABENCH)
|
|
||||||
using r1 = std::ratio<<%= 2 * i - 1 %>, <%= 2 * n %>>;
|
|
||||||
using r2 = std::ratio<<%= 2 * i %>, <%= 2 * n %>>;
|
|
||||||
#else
|
|
||||||
using r1 = void;
|
|
||||||
using r2 = void;
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
}
|
|
@@ -1,21 +0,0 @@
|
|||||||
#include "ratio_type_constexpr.h"
|
|
||||||
|
|
||||||
<% (1..n).each do |i| %>
|
|
||||||
struct test<%= i %> {
|
|
||||||
using r1 = units::ratio<<%= 2 * i - 1 %>, <%= 2 * n %>>;
|
|
||||||
using r2 = units::ratio<<%= 2 * i %>, <%= 2 * n %>>;
|
|
||||||
|
|
||||||
#if defined(METABENCH)
|
|
||||||
using r3 = units::ratio_multiply<r1, r2>;
|
|
||||||
using r4 = units::ratio_divide<r1, r2>;
|
|
||||||
#else
|
|
||||||
using r3 = void;
|
|
||||||
using r4 = void;
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
}
|
|
@@ -1,21 +0,0 @@
|
|||||||
#include <ratio>
|
|
||||||
|
|
||||||
<% (1..n).each do |i| %>
|
|
||||||
struct test<%= i %> {
|
|
||||||
using r1 = std::ratio<<%= 2 * i - 1 %>, <%= 2 * n %>>;
|
|
||||||
using r2 = std::ratio<<%= 2 * i %>, <%= 2 * n %>>;
|
|
||||||
|
|
||||||
#if defined(METABENCH)
|
|
||||||
using r3 = std::ratio_multiply<r1, r2>;
|
|
||||||
using r4 = std::ratio_divide<r1, r2>;
|
|
||||||
#else
|
|
||||||
using r3 = void;
|
|
||||||
using r4 = void;
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
}
|
|
@@ -1,20 +0,0 @@
|
|||||||
#include "ratio_type_constexpr.h"
|
|
||||||
|
|
||||||
#if defined(METABENCH)
|
|
||||||
<% (1..n).each do |i| %>
|
|
||||||
struct test<%= i %> {
|
|
||||||
using r1 = units::ratio<<%= i %>, <%= n + 1 - i %>>;
|
|
||||||
using r2 = units::ratio<<%= n + 1 - i %>, <%= i %>>;
|
|
||||||
|
|
||||||
using r3 = units::ratio_multiply<r1, r2>;
|
|
||||||
using r4 = units::ratio_divide<r1, r2>;
|
|
||||||
|
|
||||||
using r5 = units::common_ratio_t<r1, r2>;
|
|
||||||
};
|
|
||||||
<% end %>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
}
|
|
@@ -1,128 +0,0 @@
|
|||||||
// 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 <cstdint>
|
|
||||||
#include <exception>
|
|
||||||
#include <numeric>
|
|
||||||
#include <type_traits>
|
|
||||||
|
|
||||||
#define Expects(cond) \
|
|
||||||
if (!(cond)) ::std::terminate();
|
|
||||||
|
|
||||||
namespace units {
|
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
[[nodiscard]] constexpr T abs(T v) noexcept
|
|
||||||
{
|
|
||||||
return v < 0 ? -v : v;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
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 {
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<typename R1, typename R2>
|
|
||||||
using ratio_multiply = typename detail::ratio_multiply<R1, R2>::type;
|
|
||||||
|
|
||||||
// ratio_divide
|
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
template<typename R1, typename R2>
|
|
||||||
struct ratio_divide {
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<typename R1, typename R2>
|
|
||||||
using ratio_divide = typename detail::ratio_divide<R1, R2>::type;
|
|
||||||
|
|
||||||
// common_ratio
|
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
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>;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template<typename R1, typename R2>
|
|
||||||
using common_ratio_t = typename detail::common_ratio_impl<R1, R2>::type;
|
|
||||||
|
|
||||||
} // namespace units
|
|
@@ -1,63 +0,0 @@
|
|||||||
// 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 units {
|
|
||||||
|
|
||||||
// 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> {};
|
|
||||||
|
|
||||||
// common_ratio
|
|
||||||
|
|
||||||
template<typename R1, typename R2>
|
|
||||||
struct common_ratio {
|
|
||||||
using gcd_num = static_gcd<R1::num, R2::num>;
|
|
||||||
using gcd_den = static_gcd<R1::den, R2::den>;
|
|
||||||
using type = std::ratio<gcd_num::value, (R1::den / gcd_den::value) * R2::den>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename R1, typename R2>
|
|
||||||
using common_ratio_t = typename common_ratio<R1, R2>::type;
|
|
||||||
|
|
||||||
} // namespace units
|
|
Reference in New Issue
Block a user