integer_sequence.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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...>
  18. struct __tuple_indices;
  19. template <class _IdxType, _IdxType... _Values>
  20. struct __integer_sequence {
  21. template <template <class _OIdxType, _OIdxType...> class _ToIndexSeq, class _ToIndexType>
  22. using __convert = _ToIndexSeq<_ToIndexType, _Values...>;
  23. template <size_t _Sp>
  24. using __to_tuple_indices = __tuple_indices<(_Values + _Sp)...>;
  25. };
  26. #if __has_builtin(__make_integer_seq)
  27. template <size_t _Ep, size_t _Sp>
  28. using __make_indices_imp =
  29. typename __make_integer_seq<__integer_sequence, size_t, _Ep - _Sp>::template __to_tuple_indices<_Sp>;
  30. #elif __has_builtin(__integer_pack)
  31. template <size_t _Ep, size_t _Sp>
  32. using __make_indices_imp =
  33. typename __integer_sequence<size_t, __integer_pack(_Ep - _Sp)...>::template __to_tuple_indices<_Sp>;
  34. #else
  35. # error "No known way to get an integer pack from the compiler"
  36. #endif
  37. #if _LIBCPP_STD_VER >= 14
  38. template <class _Tp, _Tp... _Ip>
  39. struct _LIBCPP_TEMPLATE_VIS integer_sequence {
  40. typedef _Tp value_type;
  41. static_assert(is_integral<_Tp>::value, "std::integer_sequence can only be instantiated with an integral type");
  42. static _LIBCPP_HIDE_FROM_ABI constexpr size_t size() noexcept { return sizeof...(_Ip); }
  43. };
  44. template <size_t... _Ip>
  45. using index_sequence = integer_sequence<size_t, _Ip...>;
  46. # if __has_builtin(__make_integer_seq)
  47. template <class _Tp, _Tp _Ep>
  48. using make_integer_sequence _LIBCPP_NODEBUG = __make_integer_seq<integer_sequence, _Tp, _Ep>;
  49. # elif __has_builtin(__integer_pack)
  50. template <class _Tp, _Tp _SequenceSize>
  51. using make_integer_sequence _LIBCPP_NODEBUG = integer_sequence<_Tp, __integer_pack(_SequenceSize)...>;
  52. # else
  53. # error "No known way to get an integer pack from the compiler"
  54. # endif
  55. template <size_t _Np>
  56. using make_index_sequence = make_integer_sequence<size_t, _Np>;
  57. template <class... _Tp>
  58. using index_sequence_for = make_index_sequence<sizeof...(_Tp)>;
  59. # if _LIBCPP_STD_VER >= 20
  60. // Executes __func for every element in an index_sequence.
  61. template <size_t... _Index, class _Function>
  62. _LIBCPP_HIDE_FROM_ABI constexpr void __for_each_index_sequence(index_sequence<_Index...>, _Function __func) {
  63. (__func.template operator()<_Index>(), ...);
  64. }
  65. # endif // _LIBCPP_STD_VER >= 20
  66. #endif // _LIBCPP_STD_VER >= 14
  67. _LIBCPP_END_NAMESPACE_STD
  68. #endif // _LIBCPP___UTILITY_INTEGER_SEQUENCE_H