integer_sequence.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #ifndef _LIBCPP___UTILITY_INTEGER_SEQUENCE_H
  9. #define _LIBCPP___UTILITY_INTEGER_SEQUENCE_H
  10. #include <__config>
  11. #include <__type_traits/is_integral.h>
  12. #include <cstddef>
  13. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  14. # pragma GCC system_header
  15. #endif
  16. _LIBCPP_BEGIN_NAMESPACE_STD
  17. template <size_t...> struct __tuple_indices;
  18. template <class _IdxType, _IdxType... _Values>
  19. struct __integer_sequence {
  20. template <template <class _OIdxType, _OIdxType...> class _ToIndexSeq, class _ToIndexType>
  21. using __convert = _ToIndexSeq<_ToIndexType, _Values...>;
  22. template <size_t _Sp>
  23. using __to_tuple_indices = __tuple_indices<(_Values + _Sp)...>;
  24. };
  25. #if !__has_builtin(__make_integer_seq) || defined(_LIBCPP_TESTING_FALLBACK_MAKE_INTEGER_SEQUENCE)
  26. namespace __detail {
  27. template<typename _Tp, size_t ..._Extra> struct __repeat;
  28. template<typename _Tp, _Tp ..._Np, size_t ..._Extra> struct __repeat<__integer_sequence<_Tp, _Np...>, _Extra...> {
  29. typedef _LIBCPP_NODEBUG __integer_sequence<_Tp,
  30. _Np...,
  31. sizeof...(_Np) + _Np...,
  32. 2 * sizeof...(_Np) + _Np...,
  33. 3 * sizeof...(_Np) + _Np...,
  34. 4 * sizeof...(_Np) + _Np...,
  35. 5 * sizeof...(_Np) + _Np...,
  36. 6 * sizeof...(_Np) + _Np...,
  37. 7 * sizeof...(_Np) + _Np...,
  38. _Extra...> type;
  39. };
  40. template<size_t _Np> struct __parity;
  41. template<size_t _Np> struct __make : __parity<_Np % 8>::template __pmake<_Np> {};
  42. template<> struct __make<0> { typedef __integer_sequence<size_t> type; };
  43. template<> struct __make<1> { typedef __integer_sequence<size_t, 0> type; };
  44. template<> struct __make<2> { typedef __integer_sequence<size_t, 0, 1> type; };
  45. template<> struct __make<3> { typedef __integer_sequence<size_t, 0, 1, 2> type; };
  46. template<> struct __make<4> { typedef __integer_sequence<size_t, 0, 1, 2, 3> type; };
  47. template<> struct __make<5> { typedef __integer_sequence<size_t, 0, 1, 2, 3, 4> type; };
  48. template<> struct __make<6> { typedef __integer_sequence<size_t, 0, 1, 2, 3, 4, 5> type; };
  49. template<> struct __make<7> { typedef __integer_sequence<size_t, 0, 1, 2, 3, 4, 5, 6> type; };
  50. template<> struct __parity<0> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type> {}; };
  51. template<> struct __parity<1> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 1> {}; };
  52. template<> struct __parity<2> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 2, _Np - 1> {}; };
  53. template<> struct __parity<3> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 3, _Np - 2, _Np - 1> {}; };
  54. template<> struct __parity<4> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 4, _Np - 3, _Np - 2, _Np - 1> {}; };
  55. 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> {}; };
  56. 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> {}; };
  57. 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> {}; };
  58. } // namespace detail
  59. #endif
  60. #if __has_builtin(__make_integer_seq)
  61. template <size_t _Ep, size_t _Sp>
  62. using __make_indices_imp =
  63. typename __make_integer_seq<__integer_sequence, size_t, _Ep - _Sp>::template
  64. __to_tuple_indices<_Sp>;
  65. #else
  66. template <size_t _Ep, size_t _Sp>
  67. using __make_indices_imp =
  68. typename __detail::__make<_Ep - _Sp>::type::template __to_tuple_indices<_Sp>;
  69. #endif
  70. #if _LIBCPP_STD_VER > 11
  71. template<class _Tp, _Tp... _Ip>
  72. struct _LIBCPP_TEMPLATE_VIS integer_sequence
  73. {
  74. typedef _Tp value_type;
  75. static_assert( is_integral<_Tp>::value,
  76. "std::integer_sequence can only be instantiated with an integral type" );
  77. static
  78. _LIBCPP_INLINE_VISIBILITY
  79. constexpr
  80. size_t
  81. size() noexcept { return sizeof...(_Ip); }
  82. };
  83. template<size_t... _Ip>
  84. using index_sequence = integer_sequence<size_t, _Ip...>;
  85. #if __has_builtin(__make_integer_seq) && !defined(_LIBCPP_TESTING_FALLBACK_MAKE_INTEGER_SEQUENCE)
  86. template <class _Tp, _Tp _Ep>
  87. using __make_integer_sequence _LIBCPP_NODEBUG = __make_integer_seq<integer_sequence, _Tp, _Ep>;
  88. #else
  89. template<typename _Tp, _Tp _Np> using __make_integer_sequence_unchecked _LIBCPP_NODEBUG =
  90. typename __detail::__make<_Np>::type::template __convert<integer_sequence, _Tp>;
  91. template <class _Tp, _Tp _Ep>
  92. struct __make_integer_sequence_checked
  93. {
  94. static_assert(is_integral<_Tp>::value,
  95. "std::make_integer_sequence can only be instantiated with an integral type" );
  96. static_assert(0 <= _Ep, "std::make_integer_sequence must have a non-negative sequence length");
  97. // Workaround GCC bug by preventing bad installations when 0 <= _Ep
  98. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68929
  99. typedef _LIBCPP_NODEBUG __make_integer_sequence_unchecked<_Tp, 0 <= _Ep ? _Ep : 0> type;
  100. };
  101. template <class _Tp, _Tp _Ep>
  102. using __make_integer_sequence _LIBCPP_NODEBUG = typename __make_integer_sequence_checked<_Tp, _Ep>::type;
  103. #endif
  104. template<class _Tp, _Tp _Np>
  105. using make_integer_sequence = __make_integer_sequence<_Tp, _Np>;
  106. template<size_t _Np>
  107. using make_index_sequence = make_integer_sequence<size_t, _Np>;
  108. template<class... _Tp>
  109. using index_sequence_for = make_index_sequence<sizeof...(_Tp)>;
  110. # if _LIBCPP_STD_VER > 17
  111. // Executes __func for every element in an index_sequence.
  112. template <size_t... _Index, class _Function>
  113. _LIBCPP_HIDE_FROM_ABI constexpr void __for_each_index_sequence(index_sequence<_Index...>, _Function __func) {
  114. (__func.template operator()<_Index>(), ...);
  115. }
  116. # endif // _LIBCPP_STD_VER > 17
  117. #endif // _LIBCPP_STD_VER > 11
  118. _LIBCPP_END_NAMESPACE_STD
  119. #endif // _LIBCPP___UTILITY_INTEGER_SEQUENCE_H