| 1 | #ifndef BOOST_MP11_INTEGER_SEQUENCE_HPP_INCLUDED |
| 2 | #define BOOST_MP11_INTEGER_SEQUENCE_HPP_INCLUDED |
| 3 | |
| 4 | // Copyright 2015, 2017, 2019 Peter Dimov. |
| 5 | // |
| 6 | // Distributed under the Boost Software License, Version 1.0. |
| 7 | // |
| 8 | // See accompanying file LICENSE_1_0.txt or copy at |
| 9 | // http://www.boost.org/LICENSE_1_0.txt |
| 10 | |
| 11 | #include <boost/mp11/version.hpp> |
| 12 | #include <cstddef> |
| 13 | |
| 14 | #if defined(__has_builtin) |
| 15 | # if __has_builtin(__make_integer_seq) |
| 16 | # define BOOST_MP11_HAS_MAKE_INTEGER_SEQ |
| 17 | # endif |
| 18 | #endif |
| 19 | |
| 20 | namespace boost |
| 21 | { |
| 22 | namespace mp11 |
| 23 | { |
| 24 | |
| 25 | // integer_sequence |
| 26 | template<class T, T... I> struct integer_sequence |
| 27 | { |
| 28 | }; |
| 29 | |
| 30 | #if defined(BOOST_MP11_HAS_MAKE_INTEGER_SEQ) |
| 31 | |
| 32 | template<class T, T N> using make_integer_sequence = __make_integer_seq<integer_sequence, T, N>; |
| 33 | |
| 34 | #else |
| 35 | |
| 36 | // detail::make_integer_sequence_impl |
| 37 | namespace detail |
| 38 | { |
| 39 | |
| 40 | // iseq_if_c |
| 41 | template<bool C, class T, class E> struct iseq_if_c_impl; |
| 42 | |
| 43 | template<class T, class E> struct iseq_if_c_impl<true, T, E> |
| 44 | { |
| 45 | using type = T; |
| 46 | }; |
| 47 | |
| 48 | template<class T, class E> struct iseq_if_c_impl<false, T, E> |
| 49 | { |
| 50 | using type = E; |
| 51 | }; |
| 52 | |
| 53 | template<bool C, class T, class E> using iseq_if_c = typename iseq_if_c_impl<C, T, E>::type; |
| 54 | |
| 55 | // iseq_identity |
| 56 | template<class T> struct iseq_identity |
| 57 | { |
| 58 | using type = T; |
| 59 | }; |
| 60 | |
| 61 | template<class S1, class S2> struct append_integer_sequence; |
| 62 | |
| 63 | template<class T, T... I, T... J> struct append_integer_sequence<integer_sequence<T, I...>, integer_sequence<T, J...>> |
| 64 | { |
| 65 | using type = integer_sequence< T, I..., ( J + sizeof...(I) )... >; |
| 66 | }; |
| 67 | |
| 68 | template<class T, T N> struct make_integer_sequence_impl; |
| 69 | |
| 70 | template<class T, T N> struct make_integer_sequence_impl_ |
| 71 | { |
| 72 | private: |
| 73 | |
| 74 | static_assert( N >= 0, "make_integer_sequence<T, N>: N must not be negative" ); |
| 75 | |
| 76 | static T const M = N / 2; |
| 77 | static T const R = N % 2; |
| 78 | |
| 79 | using S1 = typename make_integer_sequence_impl<T, M>::type; |
| 80 | using S2 = typename append_integer_sequence<S1, S1>::type; |
| 81 | using S3 = typename make_integer_sequence_impl<T, R>::type; |
| 82 | using S4 = typename append_integer_sequence<S2, S3>::type; |
| 83 | |
| 84 | public: |
| 85 | |
| 86 | using type = S4; |
| 87 | }; |
| 88 | |
| 89 | template<class T, T N> struct make_integer_sequence_impl: iseq_if_c<N == 0, iseq_identity<integer_sequence<T>>, iseq_if_c<N == 1, iseq_identity<integer_sequence<T, 0>>, make_integer_sequence_impl_<T, N> > > |
| 90 | { |
| 91 | }; |
| 92 | |
| 93 | } // namespace detail |
| 94 | |
| 95 | // make_integer_sequence |
| 96 | template<class T, T N> using make_integer_sequence = typename detail::make_integer_sequence_impl<T, N>::type; |
| 97 | |
| 98 | #endif // defined(BOOST_MP11_HAS_MAKE_INTEGER_SEQ) |
| 99 | |
| 100 | // index_sequence |
| 101 | template<std::size_t... I> using index_sequence = integer_sequence<std::size_t, I...>; |
| 102 | |
| 103 | // make_index_sequence |
| 104 | template<std::size_t N> using make_index_sequence = make_integer_sequence<std::size_t, N>; |
| 105 | |
| 106 | // index_sequence_for |
| 107 | template<class... T> using index_sequence_for = make_integer_sequence<std::size_t, sizeof...(T)>; |
| 108 | |
| 109 | } // namespace mp11 |
| 110 | } // namespace boost |
| 111 | |
| 112 | #endif // #ifndef BOOST_MP11_INTEGER_SEQUENCE_HPP_INCLUDED |
| 113 | |