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 <__cstddef/size_t.h>
14#include <__tuple/tuple_element.h>
15#include <__tuple/tuple_size.h>
16#include <__type_traits/is_integral.h>
17
18#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19# pragma GCC system_header
20#endif
21
22#ifndef _LIBCPP_CXX03_LANG
23
24_LIBCPP_BEGIN_NAMESPACE_STD
25
26# if __has_builtin(__make_integer_seq)
27template <template <class _Tp, _Tp...> class _BaseType, class _Tp, _Tp _SequenceSize>
28using __make_integer_sequence_impl _LIBCPP_NODEBUG = __make_integer_seq<_BaseType, _Tp, _SequenceSize>;
29# else
30template <template <class _Tp, _Tp...> class _BaseType, class _Tp, _Tp _SequenceSize>
31using __make_integer_sequence_impl _LIBCPP_NODEBUG = _BaseType<_Tp, __integer_pack(_SequenceSize)...>;
32# endif
33
34template <class _Tp, _Tp... _Indices>
35struct __integer_sequence {
36 using value_type = _Tp;
37 static_assert(is_integral<_Tp>::value, "std::integer_sequence can only be instantiated with an integral type");
38 [[__nodiscard__]] static _LIBCPP_HIDE_FROM_ABI constexpr size_t size() noexcept { return sizeof...(_Indices); }
39};
40
41template <size_t... _Indices>
42using __index_sequence _LIBCPP_NODEBUG = __integer_sequence<size_t, _Indices...>;
43
44template <size_t _SequenceSize>
45using __make_index_sequence _LIBCPP_NODEBUG = __make_integer_sequence_impl<__integer_sequence, size_t, _SequenceSize>;
46
47template <class... _Args>
48using __index_sequence_for _LIBCPP_NODEBUG = __make_index_sequence<sizeof...(_Args)>;
49
50# if _LIBCPP_STD_VER >= 14
51
52template <class _Tp, _Tp... _Indices>
53struct integer_sequence : __integer_sequence<_Tp, _Indices...> {};
54
55template <size_t... _Ip>
56using index_sequence = integer_sequence<size_t, _Ip...>;
57
58template <class _Tp, _Tp _Ep>
59using make_integer_sequence _LIBCPP_NODEBUG = __make_integer_sequence_impl<integer_sequence, _Tp, _Ep>;
60
61template <size_t _Np>
62using make_index_sequence = make_integer_sequence<size_t, _Np>;
63
64template <class... _Tp>
65using index_sequence_for = make_index_sequence<sizeof...(_Tp)>;
66
67# if _LIBCPP_STD_VER >= 20
68// Executes __func for every element in an index_sequence.
69template <size_t... _Index, class _Function>
70_LIBCPP_HIDE_FROM_ABI constexpr void __for_each_index_sequence(index_sequence<_Index...>, _Function __func) {
71 (__func.template operator()<_Index>(), ...);
72}
73# endif // _LIBCPP_STD_VER >= 20
74
75# if _LIBCPP_STD_VER >= 26
76// [intseq.binding], structured binding support
77template <class _Tp, _Tp... _Indices>
78struct tuple_size<integer_sequence<_Tp, _Indices...>> : integral_constant<size_t, sizeof...(_Indices)> {};
79
80template <size_t _Ip, class _Tp, _Tp... _Indices>
81struct tuple_element<_Ip, integer_sequence<_Tp, _Indices...>> {
82 static_assert(_Ip < sizeof...(_Indices), "Index out of bounds in std::tuple_element<> (std::integer_sequence)");
83 using type _LIBCPP_NODEBUG = _Tp;
84};
85
86template <size_t _Ip, class _Tp, _Tp... _Indices>
87struct tuple_element<_Ip, const integer_sequence<_Tp, _Indices...>> {
88 static_assert(_Ip < sizeof...(_Indices), "Index out of bounds in std::tuple_element<> (const std::integer_sequence)");
89 using type _LIBCPP_NODEBUG = _Tp;
90};
91
92template <size_t _Ip, class _Tp, _Tp... _Indices>
93[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp get(integer_sequence<_Tp, _Indices...>) noexcept {
94 static_assert(_Ip < sizeof...(_Indices), "Index out of bounds in std::get<> (std::integer_sequence)");
95 return _Indices...[_Ip];
96}
97# endif // _LIBCPP_STD_VER >= 26
98
99# endif // _LIBCPP_STD_VER >= 14
100
101_LIBCPP_END_NAMESPACE_STD
102
103#endif // _LIBCPP_CXX03_LANG
104
105#endif // _LIBCPP___UTILITY_INTEGER_SEQUENCE_H
106