@@ -199,30 +199,71 @@ template <index_t N>
199199using make_index_sequence =
200200 typename __make_integer_seq<impl::__integer_sequence, index_t , N>::seq_type;
201201
202- // merge sequence
203- template <typename Seq, typename ... Seqs>
204- struct sequence_merge
202+ // merge sequence - optimized to avoid recursive instantiation
203+ namespace detail {
204+
205+ // Helper to concatenate multiple sequences in one step using fold expression
206+ template <typename ... Seqs>
207+ struct sequence_merge_impl ;
208+
209+ // Base case: single sequence
210+ template <index_t ... Is>
211+ struct sequence_merge_impl <Sequence<Is...>>
205212{
206- using type = typename sequence_merge<Seq, typename sequence_merge<Seqs ...>::type>::type ;
213+ using type = Sequence<Is ...>;
207214};
208215
216+ // Two sequences: direct concatenation
209217template <index_t ... Xs, index_t ... Ys>
210- struct sequence_merge <Sequence<Xs...>, Sequence<Ys...>>
218+ struct sequence_merge_impl <Sequence<Xs...>, Sequence<Ys...>>
211219{
212220 using type = Sequence<Xs..., Ys...>;
213221};
214222
215- template <typename Seq>
216- struct sequence_merge <Seq>
223+ // Three sequences: direct concatenation (avoids one level of recursion)
224+ template <index_t ... Xs, index_t ... Ys, index_t ... Zs>
225+ struct sequence_merge_impl <Sequence<Xs...>, Sequence<Ys...>, Sequence<Zs...>>
217226{
218- using type = Seq;
227+ using type = Sequence<Xs..., Ys..., Zs...>;
228+ };
229+
230+ // Four sequences: direct concatenation
231+ template <index_t ... As, index_t ... Bs, index_t ... Cs, index_t ... Ds>
232+ struct sequence_merge_impl <Sequence<As...>, Sequence<Bs...>, Sequence<Cs...>, Sequence<Ds...>>
233+ {
234+ using type = Sequence<As..., Bs..., Cs..., Ds...>;
235+ };
236+
237+ // General case: binary tree reduction (O(log N) depth instead of O(N))
238+ template <typename S1, typename S2, typename S3, typename S4, typename ... Rest>
239+ struct sequence_merge_impl <S1, S2, S3, S4, Rest...>
240+ {
241+ // Merge pairs first, then recurse
242+ using left = typename sequence_merge_impl<S1, S2>::type;
243+ using right = typename sequence_merge_impl<S3, S4, Rest...>::type;
244+ using type = typename sequence_merge_impl<left, right>::type;
245+ };
246+
247+ } // namespace detail
248+
249+ template <typename ... Seqs>
250+ struct sequence_merge
251+ {
252+ using type = typename detail::sequence_merge_impl<Seqs...>::type;
253+ };
254+
255+ template <>
256+ struct sequence_merge <>
257+ {
258+ using type = Sequence<>;
219259};
220260
221261// generate sequence - optimized using __make_integer_seq to avoid recursive instantiation
222262namespace detail {
223263
224264// Helper that applies functor F to indices and produces a Sequence
225- // __make_integer_seq<sequence_gen_helper, index_t, N> produces sequence_gen_helper<index_t, 0, 1, ..., N-1>
265+ // __make_integer_seq<sequence_gen_helper, index_t, N> produces sequence_gen_helper<index_t, 0, 1,
266+ // ..., N-1>
226267template <typename T, T... Is>
227268struct sequence_gen_helper
228269{
@@ -236,8 +277,8 @@ struct sequence_gen_helper
236277template <index_t NSize, typename F>
237278struct sequence_gen
238279{
239- using type = typename __make_integer_seq<detail::sequence_gen_helper, index_t , NSize>::
240- template apply<F>;
280+ using type =
281+ typename __make_integer_seq<detail::sequence_gen_helper, index_t , NSize>:: template apply<F>;
241282};
242283
243284template <typename F>
0 commit comments