Skip to content

Commit 2f97643

Browse files
committed
Simplify visit code
1 parent cfd8efc commit 2f97643

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

source/tv/visit.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,37 +79,39 @@ consteval auto is_variants_then_visit_function(std::index_sequence<indexes...>)
7979
>;
8080
}
8181

82-
template<bool use_index, std::size_t, std::size_t... indexes>
82+
template<bool use_index, std::size_t variant_index>
8383
constexpr auto visit_implementation(
8484
auto && function,
85-
std::index_sequence<indexes...>,
8685
bounded::constant_t<0>,
87-
auto && ... variants
88-
) -> decltype(auto) requires(sizeof...(indexes) == sizeof...(variants)) {
86+
auto && ... elements
87+
) -> decltype(auto) requires(variant_index == sizeof...(elements)) {
88+
return OPERATORS_FORWARD(function)(OPERATORS_FORWARD(elements)...);
89+
}
90+
91+
template<bool use_index>
92+
constexpr auto get_element(auto && variant, auto const index) -> decltype(auto) {
8993
if constexpr (use_index) {
90-
return OPERATORS_FORWARD(function)(
91-
indexed_value<
92-
decltype(OPERATORS_FORWARD(variants)[bounded::constant<indexes>]),
93-
indexes
94-
>(OPERATORS_FORWARD(variants)[bounded::constant<indexes>])...
95-
);
94+
return indexed_value<
95+
decltype(OPERATORS_FORWARD(variant)[index]),
96+
std::size_t(index)
97+
>(OPERATORS_FORWARD(variant)[index]);
9698
} else {
97-
return OPERATORS_FORWARD(function)(
98-
OPERATORS_FORWARD(variants)[bounded::constant<indexes>]...
99-
);
99+
return OPERATORS_FORWARD(variant)[index];
100100
}
101101
}
102102

103-
template<bool use_index, std::size_t variant_index, std::size_t... indexes>
103+
template<bool use_index, std::size_t variant_index>
104104
constexpr auto visit_implementation(
105105
auto && function,
106-
std::index_sequence<indexes...> initial_indexes,
107106
auto offset,
108107
auto && ... variants
109-
) -> decltype(auto) requires(sizeof...(indexes) < sizeof...(variants)) {
108+
) -> decltype(auto) requires(variant_index < sizeof...(variants)) {
110109
auto const search_index = variants...[variant_index].index().integer();
111110
auto const this_search = search_index - offset;
112111

112+
auto const [...element_indexes] = bounded::index_sequence_struct<variant_index>();
113+
auto && variant = OPERATORS_FORWARD(variants...[variant_index]); \
114+
auto const [...remaining_indexes] = bounded::index_sequence_struct<sizeof...(variants) - variant_index - 1>();
113115
// Cannot use a lambda because there is no return type that would be valid
114116
// there. A deduced return type would be potentially void.
115117
#define VISIT_IMPL(index) \
@@ -119,9 +121,10 @@ constexpr auto visit_implementation(
119121
} else { \
120122
return ::tv::visit_implementation<use_index, variant_index + 1>( \
121123
OPERATORS_FORWARD(function), \
122-
std::index_sequence<indexes..., static_cast<std::size_t>(offset + (index))>{}, \
123124
0_bi, \
124-
OPERATORS_FORWARD(variants)... \
125+
OPERATORS_FORWARD(variants...[element_indexes.value()])..., \
126+
get_element<use_index>(OPERATORS_FORWARD(variant), offset + (index)), \
127+
OPERATORS_FORWARD(variants...[variant_index + 1 + remaining_indexes.value()])... \
125128
); \
126129
} \
127130
} while (false)
@@ -151,7 +154,6 @@ constexpr auto visit_implementation(
151154
} else {
152155
return ::tv::visit_implementation<use_index, variant_index>(
153156
OPERATORS_FORWARD(function),
154-
initial_indexes,
155157
offset + max_index,
156158
OPERATORS_FORWARD(variants)...
157159
);
@@ -168,7 +170,6 @@ export constexpr auto visit_with_index = []<typename... Args>(Args && ... args)
168170
auto [...indexes] = bounded::index_sequence_struct<sizeof...(args) - 1>();
169171
return ::tv::visit_implementation<true, 0>(
170172
OPERATORS_FORWARD(args...[sizeof...(args) - 1]),
171-
std::index_sequence<>(),
172173
0_bi,
173174
OPERATORS_FORWARD(args...[indexes.value()])...
174175
);
@@ -182,7 +183,6 @@ export constexpr auto visit = []<typename... Args>(Args && ... args) static -> d
182183
auto [...indexes] = bounded::index_sequence_struct<sizeof...(args) - 1>();
183184
return ::tv::visit_implementation<false, 0>(
184185
OPERATORS_FORWARD(args...[sizeof...(args) - 1]),
185-
std::index_sequence<>(),
186186
0_bi,
187187
OPERATORS_FORWARD(args...[indexes.value()])...
188188
);

0 commit comments

Comments
 (0)