| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | //====================================================================================================================== | ||
| 2 | /* | ||
| 3 | KUMI - Compact Tuple Tools | ||
| 4 | Copyright : KUMI Project Contributors | ||
| 5 | SPDX-License-Identifier: BSL-1.0 | ||
| 6 | */ | ||
| 7 | //====================================================================================================================== | ||
| 8 | #pragma once | ||
| 9 | |||
| 10 | namespace kumi | ||
| 11 | { | ||
| 12 | namespace _ | ||
| 13 | { | ||
| 14 | struct reindex_case_t | ||
| 15 | { | ||
| 16 | template<typename T, template<auto> class C, auto Old, auto P> | ||
| 17 | 152 | KUMI_HIDDEN_ABI constexpr auto operator()(T&& t, C<Old>, kumi::projection_map<P>) const | |
| 18 | { | ||
| 19 | 8 | if constexpr (kumi::concepts::projection_map<decltype(P)>) return C<P>{}(KUMI_FWD(t)); | |
| 20 | else | ||
| 21 | { | ||
| 22 | static_assert(requires { get<P>(std::declval<T>()); }, "[KUMI] - Invalid projection for input type"); | ||
| 23 | 144 | return get<P>(KUMI_FWD(t)); | |
| 24 | } | ||
| 25 | } | ||
| 26 | }; | ||
| 27 | |||
| 28 | KUMI_VARIABLE_ABI constexpr reindex_case_t reindex_case{}; | ||
| 29 | |||
| 30 | template<typename T, typename S, auto... E> | ||
| 31 | 53 | KUMI_HIDDEN_ABI constexpr auto reindex_(kumi::_::adl_tag_t, T&& t, S self, kumi::projection_map<E...>) | |
| 32 | { | ||
| 33 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 1 times.
|
106 | return kumi::builder<T>::make(kumi::_::reindex_case(KUMI_FWD(t), self, kumi::projection_map<E>{})...); |
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | template<std::size_t... Idx> struct reorder_t | ||
| 38 | { | ||
| 39 | template<kumi::concepts::product_type T> [[nodiscard]] KUMI_ABI constexpr auto operator()(T&& t) const | ||
| 40 | { | ||
| 41 | static_assert(((Idx < kumi::size_v<T>) && ...), "[KUMI] - Index out of bounds"); | ||
| 42 | 83 | return builder<T>::make(get<Idx>(KUMI_FWD(t))...); | |
| 43 | } | ||
| 44 | }; | ||
| 45 | |||
| 46 | template<kumi::concepts::identifier auto... Name> struct reorder_fields_t | ||
| 47 | { | ||
| 48 | template<kumi::concepts::product_type Tuple> [[nodiscard]] KUMI_ABI constexpr auto operator()(Tuple&& t) const | ||
| 49 | { | ||
| 50 | static_assert((requires { get<Name>(std::declval<Tuple>()); } && ...), | ||
| 51 | "[KUMI] - Identifier not present in input type"); | ||
| 52 | 29 | return builder<Tuple>::make(Name = get<Name>(KUMI_FWD(t))...); | |
| 53 | } | ||
| 54 | }; | ||
| 55 | |||
| 56 | template<kumi::concepts::projection_map auto Projections> struct reindex_t | ||
| 57 | { | ||
| 58 | template<kumi::concepts::product_type T> [[nodiscard]] KUMI_ABI constexpr auto operator()(T&& t) const | ||
| 59 | { | ||
| 60 | using proj_t = std::remove_cvref_t<decltype(Projections)>; | ||
| 61 | if constexpr (kumi::concepts::empty_product_type<T>) return builder<T>::make(); | ||
| 62 | else if constexpr (proj_t::size() == 0) return builder<T>::make(); | ||
| 63 |
12/12✓ Branch 1 taken 4 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
✓ Branch 13 taken 3 times.
✓ Branch 16 taken 3 times.
✓ Branch 19 taken 2 times.
✓ Branch 22 taken 2 times.
✓ Branch 25 taken 1 times.
✓ Branch 28 taken 1 times.
✓ Branch 31 taken 1 times.
✓ Branch 34 taken 1 times.
|
27 | else return reindex_(kumi::_::adl_tag, KUMI_FWD(t), (*this), Projections); |
| 64 | } | ||
| 65 | }; | ||
| 66 | |||
| 67 | //==================================================================================================================== | ||
| 68 | /** | ||
| 69 | @ingroup kumi_generators | ||
| 70 | |||
| 71 | @var reorder | ||
| 72 | @brief Callable object reordering elements of a product type | ||
| 73 | |||
| 74 | This function will issue a compile time error if any Idx is outside [0, size_v<T>[. | ||
| 75 | |||
| 76 | On record types, this function operates on elements as if they were ordered. The considered order is the order | ||
| 77 | of declaration. | ||
| 78 | |||
| 79 | @note Nothing prevents the number of reordered indexes from being lesser or greater than the size of t or | ||
| 80 | from appearing multiple times. | ||
| 81 | |||
| 82 | @note reorder(tuple) works and is equivalent to reorder<>(tuple) | ||
| 83 | |||
| 84 | @qualifier nodiscard | ||
| 85 | @qualifier inline | ||
| 86 | @qualifier constexpr | ||
| 87 | |||
| 88 | @groupheader{Header file} | ||
| 89 | @code | ||
| 90 | #include <kumi/algorithm/reorder.hpp> | ||
| 91 | @endcode | ||
| 92 | |||
| 93 | @groupheader{Call Signature} | ||
| 94 | |||
| 95 | @code | ||
| 96 | template<std::size_t...Idx, product_type T> | ||
| 97 | constexpr auto reorder<Idx...>(T && t); | ||
| 98 | @endcode | ||
| 99 | |||
| 100 | @subgroupheader{Template Parameters} | ||
| 101 | |||
| 102 | - `Idx` Index of elements to reorder | ||
| 103 | |||
| 104 | @subgroupheader{Parameters} | ||
| 105 | |||
| 106 | - `t`: Product Type to reorder | ||
| 107 | |||
| 108 | @subgroupheader{Return value} | ||
| 109 | |||
| 110 | - A product type with the type of `t` with elements equal to get<Idx>(t) for each given index. | ||
| 111 | |||
| 112 | @groupheader{Helper type} | ||
| 113 | |||
| 114 | @snippet include/kumi/algorithm/reorder.hpp reorder_t | ||
| 115 | |||
| 116 | Computes the return type of a call to kumi::reorder | ||
| 117 | |||
| 118 | @groupheader{Examples} | ||
| 119 | |||
| 120 | @tab_begin | ||
| 121 | |||
| 122 | @tab{Tuple} | ||
| 123 | @godbolt{doc/tuple/algo/reorder.cpp} | ||
| 124 | |||
| 125 | @tab{Record} | ||
| 126 | @godbolt{doc/record/algo/reorder.cpp} | ||
| 127 | |||
| 128 | @tab_end | ||
| 129 | **/ | ||
| 130 | //==================================================================================================================== | ||
| 131 | template<std::size_t... I> inline constexpr reorder_t<I...> reorder{}; | ||
| 132 | |||
| 133 | //==================================================================================================================== | ||
| 134 | /** | ||
| 135 | @ingroup kumi_generators | ||
| 136 | |||
| 137 | @var reorder_fields | ||
| 138 | @brief Callable object reordering elements of a Record Type | ||
| 139 | |||
| 140 | This function will issue a compile time error if the identifiers are not in `t` | ||
| 141 | |||
| 142 | On record types, this function operates on elements as if they were ordered. The considered order is the order | ||
| 143 | of declaration. | ||
| 144 | |||
| 145 | @note Nothing prevents the number of reordered names from being lesser or greater than the size of t or | ||
| 146 | from appearing multiple times if it is applied on a named tuple. | ||
| 147 | |||
| 148 | @note reorder(tuple) works and is equivalent to reorder<>(tuple) | ||
| 149 | |||
| 150 | @qualifier nodiscard | ||
| 151 | @qualifier inline | ||
| 152 | @qualifier constexpr | ||
| 153 | |||
| 154 | @groupheader{Header file} | ||
| 155 | @code | ||
| 156 | #include <kumi/algorithm/reorder.hpp> | ||
| 157 | @endcode | ||
| 158 | |||
| 159 | @groupheader{Call Signature} | ||
| 160 | |||
| 161 | @code | ||
| 162 | template<identifier auto...Ids, product_type T> | ||
| 163 | constexpr auto reorder_fields<Ids...>(T && t); | ||
| 164 | @endcode | ||
| 165 | |||
| 166 | @subgroupheader{Template Parameters} | ||
| 167 | |||
| 168 | - `Ids` Identifiers of the elements to reorder | ||
| 169 | |||
| 170 | @subgroupheader{Parameters} | ||
| 171 | |||
| 172 | - `t`: Product Type to reorder | ||
| 173 | |||
| 174 | @subgroupheader{Return value} | ||
| 175 | |||
| 176 | - A product type with the type of `t` with elements equal to get<Ids>(t) for each given identifier. | ||
| 177 | |||
| 178 | @groupheader{Helper type} | ||
| 179 | |||
| 180 | @snippet include/kumi/algorithm/reorder.hpp reorder_fields_t | ||
| 181 | |||
| 182 | Computes the return type of a call to kumi::reorder_fields | ||
| 183 | |||
| 184 | @groupheader{Example} | ||
| 185 | |||
| 186 | @godbolt{doc/record/algo/reorder_fields.cpp} | ||
| 187 | **/ | ||
| 188 | //==================================================================================================================== | ||
| 189 | template<kumi::concepts::identifier auto... Name> inline constexpr reorder_fields_t<Name...> reorder_fields{}; | ||
| 190 | |||
| 191 | //==================================================================================================================== | ||
| 192 | /** | ||
| 193 | @ingroup kumi_generators | ||
| 194 | |||
| 195 | @var reindex | ||
| 196 | @brief Callable object reindex elements of a Product Type | ||
| 197 | |||
| 198 | This function will issue a compile time error if one of the identifiers is not in `t` or | ||
| 199 | if any index is outside [0, size_v<T>[ at any given level of the input type. | ||
| 200 | |||
| 201 | On record types, this function operates on elements as if they were ordered. The considered order is the order | ||
| 202 | of declaration. | ||
| 203 | |||
| 204 | @note Nothing prevents the number of reordered names from being lesser or greater than the size of t or | ||
| 205 | from appearing multiple times if it is applied on a named tuple. | ||
| 206 | |||
| 207 | @qualifier nodiscard | ||
| 208 | @qualifier inline | ||
| 209 | @qualifier constexpr | ||
| 210 | |||
| 211 | @groupheader{Header file} | ||
| 212 | @code | ||
| 213 | #include <kumi/algorithm/reorder.hpp> | ||
| 214 | @endcode | ||
| 215 | |||
| 216 | @groupheader{Call Signature} | ||
| 217 | |||
| 218 | @code | ||
| 219 | template<identifier auto...Ids, product_type T> | ||
| 220 | constexpr auto reindex<Ids...>(T && t); | ||
| 221 | @endcode | ||
| 222 | |||
| 223 | @subgroupheader{Template Parameters} | ||
| 224 | |||
| 225 | - `Ids` Identifiers of the elements to reorder | ||
| 226 | |||
| 227 | @subgroupheader{Parameters} | ||
| 228 | |||
| 229 | - `t`: Product Type to reorder | ||
| 230 | |||
| 231 | @subgroupheader{Return value} | ||
| 232 | |||
| 233 | - A potentially nested tuple corresponding to recursive applications of reorder | ||
| 234 | |||
| 235 | @groupheader{Helper type} | ||
| 236 | |||
| 237 | @snippet include/kumi/algorithm/reorder.hpp reindex_t | ||
| 238 | |||
| 239 | Computes the return type of a call to kumi::reindex | ||
| 240 | |||
| 241 | @groupheader{Examples} | ||
| 242 | |||
| 243 | @tab_begin | ||
| 244 | |||
| 245 | @tab{Tuple} | ||
| 246 | @godbolt{doc/tuple/algo/reindex.cpp} | ||
| 247 | |||
| 248 | @tab{Record} | ||
| 249 | @godbolt{doc/record/algo/reindex.cpp} | ||
| 250 | |||
| 251 | @tab_end | ||
| 252 | **/ | ||
| 253 | //==================================================================================================================== | ||
| 254 | template<kumi::concepts::projection_map auto Projections> inline constexpr reindex_t<Projections> reindex{}; | ||
| 255 | |||
| 256 | namespace result | ||
| 257 | { | ||
| 258 | //! [reorder_t] | ||
| 259 | template<kumi::concepts::product_type T, std::size_t... Idx> | ||
| 260 | using reorder_t = decltype(kumi::reorder<Idx...>(std::declval<T>())); | ||
| 261 | |||
| 262 | template<kumi::concepts::product_type T, std::size_t... Idx> struct reorder | ||
| 263 | { | ||
| 264 | using type = kumi::result::reorder_t<T, Idx...>; | ||
| 265 | }; | ||
| 266 | |||
| 267 | //! [reorder_t] | ||
| 268 | |||
| 269 | //! [reorder_fields_t] | ||
| 270 | template<kumi::concepts::product_type Tuple, kumi::concepts::identifier auto... Name> | ||
| 271 | using reorder_fields_t = decltype(kumi::reorder_fields<Name...>(std::declval<Tuple>())); | ||
| 272 | |||
| 273 | template<kumi::concepts::product_type Tuple, kumi::concepts::identifier auto... Name> struct reorder_fields | ||
| 274 | { | ||
| 275 | using type = kumi::result::reorder_fields_t<Tuple, Name...>; | ||
| 276 | }; | ||
| 277 | |||
| 278 | //! [reorder_fields_t] | ||
| 279 | |||
| 280 | //! [reindex_t] | ||
| 281 | template<kumi::concepts::product_type T, kumi::concepts::projection_map auto Indexes> | ||
| 282 | using reindex_t = decltype(kumi::reindex<Indexes>(std::declval<T>())); | ||
| 283 | |||
| 284 | template<kumi::concepts::product_type T, kumi::concepts::projection_map auto Indexes> struct reindex | ||
| 285 | { | ||
| 286 | using type = kumi::result::reindex_t<T, Indexes>; | ||
| 287 | }; | ||
| 288 | |||
| 289 | //! [reindex_t] | ||
| 290 | } | ||
| 291 | } | ||
| 292 |