1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef _LIBCPP___UTILITY_INTEGER_SEQUENCE_H
10 #define _LIBCPP___UTILITY_INTEGER_SEQUENCE_H
11
12 #include <__config>
13 #include <__type_traits/is_integral.h>
14 #include <cstddef>
15
16 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17 # pragma GCC system_header
18 #endif
19
20 _LIBCPP_BEGIN_NAMESPACE_STD
21
22 template <size_t...>
23 struct __tuple_indices;
24
25 template <class _IdxType, _IdxType... _Values>
26 struct __integer_sequence {
27 template <template <class _OIdxType, _OIdxType...> class _ToIndexSeq, class _ToIndexType>
28 using __convert = _ToIndexSeq<_ToIndexType, _Values...>;
29
30 template <size_t _Sp>
31 using __to_tuple_indices = __tuple_indices<(_Values + _Sp)...>;
32 };
33
34 #if __has_builtin(__make_integer_seq)
35 template <size_t _Ep, size_t _Sp>
36 using __make_indices_imp =
37 typename __make_integer_seq<__integer_sequence, size_t, _Ep - _Sp>::template __to_tuple_indices<_Sp>;
38 #elif __has_builtin(__integer_pack)
39 template <size_t _Ep, size_t _Sp>
40 using __make_indices_imp =
41 typename __integer_sequence<size_t, __integer_pack(_Ep - _Sp)...>::template __to_tuple_indices<_Sp>;
42 #else
43 # error "No known way to get an integer pack from the compiler"
44 #endif
45
46 #if _LIBCPP_STD_VER >= 14
47
48 template <class _Tp, _Tp... _Ip>
49 struct _LIBCPP_TEMPLATE_VIS integer_sequence {
50 typedef _Tp value_type;
51 static_assert(is_integral<_Tp>::value, "std::integer_sequence can only be instantiated with an integral type");
sizeinteger_sequence52 static _LIBCPP_HIDE_FROM_ABI constexpr size_t size() noexcept { return sizeof...(_Ip); }
53 };
54
55 template <size_t... _Ip>
56 using index_sequence = integer_sequence<size_t, _Ip...>;
57
58 # if __has_builtin(__make_integer_seq)
59
60 template <class _Tp, _Tp _Ep>
61 using make_integer_sequence _LIBCPP_NODEBUG = __make_integer_seq<integer_sequence, _Tp, _Ep>;
62
63 # elif __has_builtin(__integer_pack)
64
65 template <class _Tp, _Tp _SequenceSize>
66 using make_integer_sequence _LIBCPP_NODEBUG = integer_sequence<_Tp, __integer_pack(_SequenceSize)...>;
67
68 # else
69 # error "No known way to get an integer pack from the compiler"
70 # endif
71
72 template <size_t _Np>
73 using make_index_sequence = make_integer_sequence<size_t, _Np>;
74
75 template <class... _Tp>
76 using index_sequence_for = make_index_sequence<sizeof...(_Tp)>;
77
78 # if _LIBCPP_STD_VER >= 20
79 // Executes __func for every element in an index_sequence.
80 template <size_t... _Index, class _Function>
__for_each_index_sequence(index_sequence<_Index...>,_Function __func)81 _LIBCPP_HIDE_FROM_ABI constexpr void __for_each_index_sequence(index_sequence<_Index...>, _Function __func) {
82 (__func.template operator()<_Index>(), ...);
83 }
84 # endif // _LIBCPP_STD_VER >= 20
85
86 #endif // _LIBCPP_STD_VER >= 14
87
88 _LIBCPP_END_NAMESPACE_STD
89
90 #endif // _LIBCPP___UTILITY_INTEGER_SEQUENCE_H
91