Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
cartesian_product.hpp
Go to the documentation of this file.
1// parasoft-suppress AUTOSAR-A2_8_1-a "False positive: also defines arene::base::type_lists::cartesian_product"
2// Copyright 2026, Toyota Motor Corporation
3//
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5
6#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_LIST_CARTESIAN_PRODUCT_HPP_
7#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_LIST_CARTESIAN_PRODUCT_HPP_
8
9// IWYU pragma: private, include "arene/base/type_list.hpp"
10// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
11
12// parasoft-begin-suppress AUTOSAR-A16_2_2-a "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
17#include "arene/base/detail/index_permutation_range.hpp"
18#include "arene/base/stdlib_choice/cstddef.hpp"
19#include "arene/base/stdlib_choice/ignore.hpp"
20#include "arene/base/stdlib_choice/initializer_list.hpp"
21#include "arene/base/stdlib_choice/integer_sequence.hpp"
22#include "arene/base/stdlib_choice/multiplies.hpp"
26// parasoft-end-suppress AUTOSAR-A16_2_2-a
27
28namespace arene {
29namespace base {
30
31namespace type_lists {
32
33namespace cartesian_product_detail {
34
35/// @brief The size of the space of all permutations of indices fitting within the given @c Sizes
36/// @tparam Sizes The sizes within which the indices of a permutation must fit
37/// @note This is essentially just a C++17 fold expression over multiplication but dim 0 is taken to be size 0
38template <std::size_t... Sizes>
39constexpr std::size_t permutation_space_size_v{
40 sizeof...(Sizes) == 0U //
41 ? 0U //
42 : reduce( //
43 std::initializer_list<std::size_t>{Sizes...},
44 std::size_t{1},
45 std::multiplies<>{}
46 )
47};
48
49/// @brief constexpr reverse algorithm
50/// @tparam BidirectionalIterator iterator type
51/// @param first beginning of the range
52/// @param last end of the range
53template <class BidirectionalIterator>
54constexpr auto reverse(BidirectionalIterator first, BidirectionalIterator last) noexcept -> void {
55 while (first != last && first != --last) {
56 arene::base::iter_swap(first, last);
57 ++first;
58 }
59}
60
61/// @brief Make an array of all permutations of indices fitting within the given @c Sizes
62/// @tparam Sizes A pack of sizes which all indices (of the same dimension) must be less than
63/// @return An array of all possible permutations of "one in-bounds index for each size in <c>Sizes</c>"
64template <std::size_t... Sizes>
65constexpr auto all_index_permutations_impl() noexcept
66 -> arene::base::array<arene::base::array<std::size_t, sizeof...(Sizes)>, permutation_space_size_v<Sizes...>> {
67 using permutation = arene::base::array<std::size_t, sizeof...(Sizes)>;
68
69 // parasoft-begin-suppress AUTOSAR-M8_5_2-a "False positive: correct initialization"
70 auto reversed_values = arene::base::array<std::size_t, sizeof...(Sizes)>{Sizes...};
71 // parasoft-end-suppress AUTOSAR-M8_5_2-a
72 reverse(reversed_values.begin(), reversed_values.end());
73
74 arene::base::array<permutation, permutation_space_size_v<Sizes...>> permutations{};
75 auto perm_itr = permutations.begin();
76 for (auto& perm : arene::base::detail::make_index_permutation_range(reversed_values)) {
77 std::ignore = arene::base::copy(perm.crbegin(), perm.crend(), perm_itr->begin());
78 ++perm_itr;
79 }
80 return permutations;
81}
82
83/// @brief As a static variable, an array of all permutations of indices fitting within the given @c Sizes
84/// @tparam Sizes A pack of sizes which all indices (of the same dimension) must be less than
85template <std::size_t... Sizes>
86constexpr arene::base::array<arene::base::array<std::size_t, sizeof...(Sizes)>, permutation_space_size_v<Sizes...>>
87 all_index_permutations_v{all_index_permutations_impl<Sizes...>()};
88
89/// @brief Get the n'th permutation of the types in the type-list pack @c Ln and emit it wrapped in @c InnerTemplate
90/// @tparam InnerTemplate The template to apply to the permuted type-list
91/// @tparam PermIndex The index of this particular permutation
92/// @tparam Ln The pack of purported type lists to permute
93template <template <class...> class InnerTemplate, std::size_t PermIndex, class... Ln>
94class nth_type_permutation {
95 // parasoft-begin-suppress CERT_CPP-DCL56-a "False positive: constexpr variables are always initialized in
96 // constexpr"
97 // parasoft-begin-suppress AUTOSAR-A3_3_2-a "False positive: constexpr variables are always initialized
98 // in constexpr"
99 /// @brief The *index* permutation used by this type permutation
100 static constexpr arene::base::array<std::size_t, sizeof...(Ln)> const& index_permutation{
101 all_index_permutations_v<size_v<Ln>...>[PermIndex]
102 };
103 // parasoft-end-suppress CERT_CPP-DCL56-a
104 // parasoft-end-suppress AUTOSAR-A3_3_2-a
105
106 // parasoft-begin-suppress CERT_C-EXP37-a "False positive: rule does not mention naming all parameters"
107 /// @brief Generate a pack of types from some permutation of the lists in @c Ln and return it as @c InnerTemplate
108 /// @tparam TypeListIndices a pack of indices of the type *lists* in @c Ln (not the permuted indices)
109 /// @return Notionally a value of type @c InnerTemplate with the pack of permuted types; only used for @c decltype
110 template <std::size_t... TypeListIndices>
111 static constexpr auto deduce_type_from_permutation(std::index_sequence<TypeListIndices...>)
112 -> InnerTemplate<at_t<Ln, index_permutation[TypeListIndices]>...>;
113 // parasoft-end-suppress CERT_C-EXP37-a
114
115 public:
116 /// @brief The permuted types, wrapped in @c InnerTemplate
117 using type = decltype(deduce_type_from_permutation(std::index_sequence_for<Ln...>{}));
118};
119
120/// @brief Get the n'th permutation of the types in the type-list pack @c Ln and emit it wrapped in @c InnerTemplate
121/// @tparam InnerTemplate The template to apply to the permuted type-list
122/// @tparam PermIndex The index of this particular permutation
123/// @tparam Ln The pack of purported type lists to permute
124template <template <class...> class InnerTemplate, std::size_t PermIndex, class... Ln>
125using nth_type_permutation_t = typename nth_type_permutation<InnerTemplate, PermIndex, Ln...>::type;
126
127/// @brief Get a type-list of all permutations of the entries of the input type-lists @c Ln
128/// @tparam OuterTemplate The outer type-list template in which to wrap the pack of permuted type-lists
129/// @tparam InnerTemplate The template to apply to each individual permuted type-list within the list of permutations
130/// @tparam PermIndexList An index-list, one for each permutation to include in the emitted type-list
131/// @tparam Ln The pack of purported type lists to permute
132template <
133 template <class...>
134 class OuterTemplate,
135 template <class...>
136 class InnerTemplate,
137 class PermIndexList,
138 class... Ln>
139struct all_type_permutations;
140
141/// @brief Get a type-list of all permutations of the entries of the input type-lists @c Ln
142/// @tparam OuterTemplate The outer type-list template in which to wrap the pack of permuted type-lists
143/// @tparam InnerTemplate The template to apply to each individual permuted type-list within the list of permutations
144/// @tparam PermIndices A pack of indices, one for each permutation to include in the emitted type-list
145/// @tparam Ln The pack of purported type lists to permute
146template <
147 template <class...>
148 class OuterTemplate,
149 template <class...>
150 class InnerTemplate,
151 std::size_t... PermIndices,
152 class... Ln>
153struct all_type_permutations<OuterTemplate, InnerTemplate, std::index_sequence<PermIndices...>, Ln...> {
154 /// @brief A pack of all permutations of the types in <c>Ln</c>, wrapped in @c OuterTemplate
155 using type = OuterTemplate<nth_type_permutation_t<InnerTemplate, PermIndices, Ln...>...>;
156};
157
158/// @brief Get the first type-list template used in the given pack, or fall back to @c arene::base::type_list if none
159/// @tparam Ln The pack of purported type lists to check
160template <class... Ln>
161struct get_first_list_template {
162 /// @brief Fallback for the case where no first list template can be extracted: use @c arene::base::type_list
163 /// @tparam Types A pack of types to hold
164 template <class... Types>
165 using type = arene::base::type_list<Types...>;
166};
167
168/// @brief Get the first type-list template used in the given pack
169/// @tparam FirstInnerTemplate The type-list template to emit
170/// @tparam Ts The types on which @c FirstInnerTemplate is templated; only used for deduction
171/// @tparam RemainingLists Any other type lists which may remain in the pack after picking out the first one
172template <template <class...> class FirstInnerTemplate, class... Ts, class... RemainingLists>
173struct get_first_list_template<FirstInnerTemplate<Ts...>, RemainingLists...> {
174 /// @brief For the case where the first list template can be extracted, return it
175 /// @tparam Types A pack of types to hold
176 template <class... Types>
177 using type = FirstInnerTemplate<Types...>;
178};
179
180} // namespace cartesian_product_detail
181
182/// @brief Produce the cartesian product of multiple type-lists
183///
184/// @tparam Ln A pack of type-lists that holds the types to process.
185/// @return A type list of type lists, where each inner list holds one combination of one type from each @c Ln
186/// @note The type of the resulting type-list, as well as all of the individual permuted type-lists within it, will be
187/// the type of the first type-list within <c>Ln</c>. It will not necessarily be <c>arene::base::type_list</c>.
188/// @note Permutations within the resulting product are ordered lexicographically with respect to the indices of the
189/// types they refer to within the original lists. The first is (0, 0, ... , 0), followed by (0, 0, ... , 1), and so on.
190template <class... Ln>
191using cartesian_product = cartesian_product_detail::all_type_permutations<
192 cartesian_product_detail::get_first_list_template<Ln...>::template type,
193 cartesian_product_detail::get_first_list_template<Ln...>::template type,
195 Ln...>;
196
197/// @brief Produce the cartesian product of multiple type-lists
198///
199/// @tparam Ln A pack of type-lists that holds the types to process.
200/// @return A type list of type lists, where each inner list holds one combination of one type from each @c Ln
201/// @note The type of the resulting type-list, as well as all of the individual permuted type-lists within it, will be
202/// the type of the first type-list within <c>Ln</c>. It will not necessarily be <c>arene::base::type_list</c>.
203/// @note Permutations within the resulting product are ordered lexicographically with respect to the indices of the
204/// types they refer to within the original lists. The first is (0, 0, ... , 0), followed by (0, 0, ... , 1), and so on.
205template <class... Ln>
206using cartesian_product_t = typename cartesian_product<Ln...>::type;
207
208/// @brief The cartesian product of multiple type-lists, with each combination of types bound to @c DesiredInnerTemplate
209///
210/// @tparam DesiredInnerTemplate A template taking only types, to be used as the inner template of the final list
211/// @tparam Ln A pack of type-lists that holds the types to process.
212/// @return A type list of <c>DesiredInnerTemplate</c>; each entry uses one combination of one type from each @c Ln
213/// @note The type of the resulting type-list will be the type of the first type-list within <c>Ln</c>, and the type of
214/// each permuted element within it will be <c>DesiredInnerTemplate</c>. Neither will necessarily be
215/// <c>arene::base::type_list</c>.
216/// @note Permutations within the resulting product are ordered lexicographically with respect to the indices of the
217/// types they refer to within the original lists. The first is (0, 0, ... , 0), followed by (0, 0, ... , 1), and so on.
218template <template <class...> class DesiredInnerTemplate, class... Ln>
219using cartesian_product_as = cartesian_product_detail::all_type_permutations<
220 cartesian_product_detail::get_first_list_template<Ln...>::template type,
221 DesiredInnerTemplate,
223 Ln...>;
224
225/// @brief The cartesian product of multiple type-lists, with each combination of types bound to @c DesiredInnerTemplate
226///
227/// @tparam DesiredInnerTemplate A template taking only types, to be used as the inner template of the final list
228/// @tparam Ln A pack of type-lists that holds the types to process.
229/// @return A type list of <c>DesiredInnerTemplate</c>; each entry uses one combination of one type from each @c Ln
230/// @note The type of the resulting type-list will be the type of the first type-list within <c>Ln</c>, and the type of
231/// each permuted element within it will be <c>DesiredInnerTemplate</c>. Neither will necessarily be
232/// <c>arene::base::type_list</c>.
233/// @note Permutations within the resulting product are ordered lexicographically with respect to the indices of the
234/// types they refer to within the original lists. The first is (0, 0, ... , 0), followed by (0, 0, ... , 1), and so on.
235template <template <class...> class DesiredInnerTemplate, class... Ln>
236using cartesian_product_as_t = typename cartesian_product_as<DesiredInnerTemplate, Ln...>::type;
237
238} // namespace type_lists
239} // namespace base
240} // namespace arene
241
242#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_LIST_CARTESIAN_PRODUCT_HPP_
Definition apply_all.hpp:14
arene::base::type_lists::size_detail::size_impl< L0 > size_t
Get the size of the type list.
Definition size.hpp:101
typename cartesian_product< Ln... >::type cartesian_product_t
Produce the cartesian product of multiple type-lists.
Definition cartesian_product.hpp:206
cartesian_product_detail::all_type_permutations< cartesian_product_detail::get_first_list_template< Ln... >::template type, DesiredInnerTemplate, std::make_index_sequence< cartesian_product_detail::permutation_space_size_v< size_v< Ln >... > >, Ln... > cartesian_product_as
The cartesian product of multiple type-lists, with each combination of types bound to DesiredInnerTem...
Definition cartesian_product.hpp:219
cartesian_product_detail::all_type_permutations< cartesian_product_detail::get_first_list_template< Ln... >::template type, cartesian_product_detail::get_first_list_template< Ln... >::template type, std::make_index_sequence< cartesian_product_detail::permutation_space_size_v< size_v< Ln >... > >, Ln... > cartesian_product
Produce the cartesian product of multiple type-lists.
Definition cartesian_product.hpp:191
typename cartesian_product_as< DesiredInnerTemplate, Ln... >::type cartesian_product_as_t
The cartesian product of multiple type-lists, with each combination of types bound to DesiredInnerTem...
Definition cartesian_product.hpp:236
Definition accumulate.hpp:37
constexpr auto copy(InputIterator first, InputIterator last, OutputIterator output) noexcept(noexcept(*(++output)) &&noexcept(*(++first)) &&noexcept(first !=last) &&std::is_nothrow_assignable<, typename std::iterator_traits< InputIterator >::reference >::value) -> OutputIterator
Copies the elements in the range, defined by [first, last), to another range beginning at output.
Definition copy.hpp:40
Definition accumulate.hpp:36
make_integer_sequence< size_t, Size > make_index_sequence
A helper alias template for make_integer_sequence<size_t, Size>
Definition integer_sequence.hpp:224
integer_sequence< size_t, Ints... > index_sequence
A helper alias template for integer_sequence<size_t, Ints>
Definition integer_sequence.hpp:218
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
static constexpr auto const & ignore
An object to which anything can be assigned, which ignores the assigned value.
Definition ignore.hpp:72
make_index_sequence< sizeof...(Types)> index_sequence_for
A helper alias template convert any type parameter pack into an index sequence of the same length.
Definition integer_sequence.hpp:230