xref: /freebsd-src/contrib/llvm-project/libcxx/include/__utility/integer_sequence.h (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
1fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
2fe6060f1SDimitry Andric //
3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fe6060f1SDimitry Andric //
7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8fe6060f1SDimitry Andric 
9fe6060f1SDimitry Andric #ifndef _LIBCPP___UTILITY_INTEGER_SEQUENCE_H
10fe6060f1SDimitry Andric #define _LIBCPP___UTILITY_INTEGER_SEQUENCE_H
11fe6060f1SDimitry Andric 
12fe6060f1SDimitry Andric #include <__config>
13bdd1243dSDimitry Andric #include <__type_traits/is_integral.h>
14bdd1243dSDimitry Andric #include <cstddef>
15fe6060f1SDimitry Andric 
16fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17fe6060f1SDimitry Andric #  pragma GCC system_header
18fe6060f1SDimitry Andric #endif
19fe6060f1SDimitry Andric 
20fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
21fe6060f1SDimitry Andric 
22*cb14a3feSDimitry Andric template <size_t...>
23*cb14a3feSDimitry Andric struct __tuple_indices;
24bdd1243dSDimitry Andric 
25bdd1243dSDimitry Andric template <class _IdxType, _IdxType... _Values>
26bdd1243dSDimitry Andric struct __integer_sequence {
27bdd1243dSDimitry Andric   template <template <class _OIdxType, _OIdxType...> class _ToIndexSeq, class _ToIndexType>
28bdd1243dSDimitry Andric   using __convert = _ToIndexSeq<_ToIndexType, _Values...>;
29bdd1243dSDimitry Andric 
30bdd1243dSDimitry Andric   template <size_t _Sp>
31bdd1243dSDimitry Andric   using __to_tuple_indices = __tuple_indices<(_Values + _Sp)...>;
32bdd1243dSDimitry Andric };
33bdd1243dSDimitry Andric 
34bdd1243dSDimitry Andric #if !__has_builtin(__make_integer_seq) || defined(_LIBCPP_TESTING_FALLBACK_MAKE_INTEGER_SEQUENCE)
35bdd1243dSDimitry Andric 
36bdd1243dSDimitry Andric namespace __detail {
37bdd1243dSDimitry Andric 
38*cb14a3feSDimitry Andric template <typename _Tp, size_t... _Extra>
39*cb14a3feSDimitry Andric struct __repeat;
40*cb14a3feSDimitry Andric template <typename _Tp, _Tp... _Np, size_t... _Extra>
41*cb14a3feSDimitry Andric struct __repeat<__integer_sequence<_Tp, _Np...>, _Extra...> {
42*cb14a3feSDimitry Andric   typedef _LIBCPP_NODEBUG __integer_sequence<
43*cb14a3feSDimitry Andric       _Tp,
44bdd1243dSDimitry Andric       _Np...,
45bdd1243dSDimitry Andric       sizeof...(_Np) + _Np...,
46bdd1243dSDimitry Andric       2 * sizeof...(_Np) + _Np...,
47bdd1243dSDimitry Andric       3 * sizeof...(_Np) + _Np...,
48bdd1243dSDimitry Andric       4 * sizeof...(_Np) + _Np...,
49bdd1243dSDimitry Andric       5 * sizeof...(_Np) + _Np...,
50bdd1243dSDimitry Andric       6 * sizeof...(_Np) + _Np...,
51bdd1243dSDimitry Andric       7 * sizeof...(_Np) + _Np...,
52*cb14a3feSDimitry Andric       _Extra...>
53*cb14a3feSDimitry Andric       type;
54bdd1243dSDimitry Andric };
55bdd1243dSDimitry Andric 
56*cb14a3feSDimitry Andric template <size_t _Np>
57*cb14a3feSDimitry Andric struct __parity;
58*cb14a3feSDimitry Andric template <size_t _Np>
59*cb14a3feSDimitry Andric struct __make : __parity<_Np % 8>::template __pmake<_Np> {};
60bdd1243dSDimitry Andric 
615f757f3fSDimitry Andric // clang-format off
62bdd1243dSDimitry Andric template<> struct __make<0> { typedef __integer_sequence<size_t> type; };
63bdd1243dSDimitry Andric template<> struct __make<1> { typedef __integer_sequence<size_t, 0> type; };
64bdd1243dSDimitry Andric template<> struct __make<2> { typedef __integer_sequence<size_t, 0, 1> type; };
65bdd1243dSDimitry Andric template<> struct __make<3> { typedef __integer_sequence<size_t, 0, 1, 2> type; };
66bdd1243dSDimitry Andric template<> struct __make<4> { typedef __integer_sequence<size_t, 0, 1, 2, 3> type; };
67bdd1243dSDimitry Andric template<> struct __make<5> { typedef __integer_sequence<size_t, 0, 1, 2, 3, 4> type; };
68bdd1243dSDimitry Andric template<> struct __make<6> { typedef __integer_sequence<size_t, 0, 1, 2, 3, 4, 5> type; };
69bdd1243dSDimitry Andric template<> struct __make<7> { typedef __integer_sequence<size_t, 0, 1, 2, 3, 4, 5, 6> type; };
70bdd1243dSDimitry Andric 
71bdd1243dSDimitry Andric template<> struct __parity<0> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type> {}; };
72bdd1243dSDimitry Andric template<> struct __parity<1> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 1> {}; };
73bdd1243dSDimitry Andric template<> struct __parity<2> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 2, _Np - 1> {}; };
74bdd1243dSDimitry Andric template<> struct __parity<3> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 3, _Np - 2, _Np - 1> {}; };
75bdd1243dSDimitry Andric template<> struct __parity<4> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 4, _Np - 3, _Np - 2, _Np - 1> {}; };
76bdd1243dSDimitry Andric template<> struct __parity<5> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 5, _Np - 4, _Np - 3, _Np - 2, _Np - 1> {}; };
77bdd1243dSDimitry Andric template<> struct __parity<6> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 6, _Np - 5, _Np - 4, _Np - 3, _Np - 2, _Np - 1> {}; };
78bdd1243dSDimitry Andric template<> struct __parity<7> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 7, _Np - 6, _Np - 5, _Np - 4, _Np - 3, _Np - 2, _Np - 1> {}; };
795f757f3fSDimitry Andric // clang-format on
80bdd1243dSDimitry Andric 
81*cb14a3feSDimitry Andric } // namespace __detail
82bdd1243dSDimitry Andric 
83bdd1243dSDimitry Andric #endif
84bdd1243dSDimitry Andric 
85bdd1243dSDimitry Andric #if __has_builtin(__make_integer_seq)
86bdd1243dSDimitry Andric template <size_t _Ep, size_t _Sp>
87bdd1243dSDimitry Andric using __make_indices_imp =
88*cb14a3feSDimitry Andric     typename __make_integer_seq<__integer_sequence, size_t, _Ep - _Sp>::template __to_tuple_indices<_Sp>;
89bdd1243dSDimitry Andric #else
90bdd1243dSDimitry Andric template <size_t _Ep, size_t _Sp>
91*cb14a3feSDimitry Andric using __make_indices_imp = typename __detail::__make<_Ep - _Sp>::type::template __to_tuple_indices<_Sp>;
92bdd1243dSDimitry Andric 
93bdd1243dSDimitry Andric #endif
94bdd1243dSDimitry Andric 
9506c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 14
96fe6060f1SDimitry Andric 
97fe6060f1SDimitry Andric template <class _Tp, _Tp... _Ip>
98*cb14a3feSDimitry Andric struct _LIBCPP_TEMPLATE_VIS integer_sequence {
99fe6060f1SDimitry Andric   typedef _Tp value_type;
100*cb14a3feSDimitry Andric   static_assert(is_integral<_Tp>::value, "std::integer_sequence can only be instantiated with an integral type");
101*cb14a3feSDimitry Andric   static _LIBCPP_HIDE_FROM_ABI constexpr size_t size() noexcept { return sizeof...(_Ip); }
102fe6060f1SDimitry Andric };
103fe6060f1SDimitry Andric 
104fe6060f1SDimitry Andric template <size_t... _Ip>
105fe6060f1SDimitry Andric using index_sequence = integer_sequence<size_t, _Ip...>;
106fe6060f1SDimitry Andric 
107fe6060f1SDimitry Andric #  if __has_builtin(__make_integer_seq) && !defined(_LIBCPP_TESTING_FALLBACK_MAKE_INTEGER_SEQUENCE)
108fe6060f1SDimitry Andric 
109fe6060f1SDimitry Andric template <class _Tp, _Tp _Ep>
110349cc55cSDimitry Andric using __make_integer_sequence _LIBCPP_NODEBUG = __make_integer_seq<integer_sequence, _Tp, _Ep>;
111fe6060f1SDimitry Andric 
112fe6060f1SDimitry Andric #  else
113fe6060f1SDimitry Andric 
114*cb14a3feSDimitry Andric template <typename _Tp, _Tp _Np>
115*cb14a3feSDimitry Andric using __make_integer_sequence_unchecked _LIBCPP_NODEBUG =
116fe6060f1SDimitry Andric     typename __detail::__make<_Np>::type::template __convert<integer_sequence, _Tp>;
117fe6060f1SDimitry Andric 
118fe6060f1SDimitry Andric template <class _Tp, _Tp _Ep>
119*cb14a3feSDimitry Andric struct __make_integer_sequence_checked {
120*cb14a3feSDimitry Andric   static_assert(is_integral<_Tp>::value, "std::make_integer_sequence can only be instantiated with an integral type");
121fe6060f1SDimitry Andric   static_assert(0 <= _Ep, "std::make_integer_sequence must have a non-negative sequence length");
122fe6060f1SDimitry Andric   // Workaround GCC bug by preventing bad installations when 0 <= _Ep
123fe6060f1SDimitry Andric   // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68929
124349cc55cSDimitry Andric   typedef _LIBCPP_NODEBUG __make_integer_sequence_unchecked<_Tp, 0 <= _Ep ? _Ep : 0> type;
125fe6060f1SDimitry Andric };
126fe6060f1SDimitry Andric 
127fe6060f1SDimitry Andric template <class _Tp, _Tp _Ep>
128349cc55cSDimitry Andric using __make_integer_sequence _LIBCPP_NODEBUG = typename __make_integer_sequence_checked<_Tp, _Ep>::type;
129fe6060f1SDimitry Andric 
130fe6060f1SDimitry Andric #  endif
131fe6060f1SDimitry Andric 
132fe6060f1SDimitry Andric template <class _Tp, _Tp _Np>
133fe6060f1SDimitry Andric using make_integer_sequence = __make_integer_sequence<_Tp, _Np>;
134fe6060f1SDimitry Andric 
135fe6060f1SDimitry Andric template <size_t _Np>
136fe6060f1SDimitry Andric using make_index_sequence = make_integer_sequence<size_t, _Np>;
137fe6060f1SDimitry Andric 
138fe6060f1SDimitry Andric template <class... _Tp>
139fe6060f1SDimitry Andric using index_sequence_for = make_index_sequence<sizeof...(_Tp)>;
140fe6060f1SDimitry Andric 
14106c3fb27SDimitry Andric #  if _LIBCPP_STD_VER >= 20
142bdd1243dSDimitry Andric // Executes __func for every element in an index_sequence.
143bdd1243dSDimitry Andric template <size_t... _Index, class _Function>
144bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr void __for_each_index_sequence(index_sequence<_Index...>, _Function __func) {
145bdd1243dSDimitry Andric   (__func.template operator()<_Index>(), ...);
146bdd1243dSDimitry Andric }
14706c3fb27SDimitry Andric #  endif // _LIBCPP_STD_VER >= 20
148bdd1243dSDimitry Andric 
14906c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 14
150fe6060f1SDimitry Andric 
151fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
152fe6060f1SDimitry Andric 
153fe6060f1SDimitry Andric #endif // _LIBCPP___UTILITY_INTEGER_SEQUENCE_H
154