integer_sequence.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #ifndef BOOST_MP11_INTEGER_SEQUENCE_HPP_INCLUDED
  2. #define BOOST_MP11_INTEGER_SEQUENCE_HPP_INCLUDED
  3. // Copyright 2015, 2017 Peter Dimov.
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. //
  7. // See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt
  9. #include <cstddef>
  10. namespace boost
  11. {
  12. namespace mp11
  13. {
  14. // integer_sequence
  15. template<class T, T... I> struct integer_sequence
  16. {
  17. };
  18. // detail::make_integer_sequence_impl
  19. namespace detail
  20. {
  21. // iseq_if_c
  22. template<bool C, class T, class E> struct iseq_if_c_impl;
  23. template<class T, class E> struct iseq_if_c_impl<true, T, E>
  24. {
  25. using type = T;
  26. };
  27. template<class T, class E> struct iseq_if_c_impl<false, T, E>
  28. {
  29. using type = E;
  30. };
  31. template<bool C, class T, class E> using iseq_if_c = typename iseq_if_c_impl<C, T, E>::type;
  32. // iseq_identity
  33. template<class T> struct iseq_identity
  34. {
  35. using type = T;
  36. };
  37. template<class S1, class S2> struct append_integer_sequence;
  38. template<class T, T... I, T... J> struct append_integer_sequence<integer_sequence<T, I...>, integer_sequence<T, J...>>
  39. {
  40. using type = integer_sequence< T, I..., ( J + sizeof...(I) )... >;
  41. };
  42. template<class T, T N> struct make_integer_sequence_impl;
  43. template<class T, T N> struct make_integer_sequence_impl_
  44. {
  45. private:
  46. static_assert( N >= 0, "make_integer_sequence<T, N>: N must not be negative" );
  47. static T const M = N / 2;
  48. static T const R = N % 2;
  49. using S1 = typename make_integer_sequence_impl<T, M>::type;
  50. using S2 = typename append_integer_sequence<S1, S1>::type;
  51. using S3 = typename make_integer_sequence_impl<T, R>::type;
  52. using S4 = typename append_integer_sequence<S2, S3>::type;
  53. public:
  54. using type = S4;
  55. };
  56. 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> > >
  57. {
  58. };
  59. } // namespace detail
  60. // make_integer_sequence
  61. template<class T, T N> using make_integer_sequence = typename detail::make_integer_sequence_impl<T, N>::type;
  62. // index_sequence
  63. template<std::size_t... I> using index_sequence = integer_sequence<std::size_t, I...>;
  64. // make_index_sequence
  65. template<std::size_t N> using make_index_sequence = make_integer_sequence<std::size_t, N>;
  66. // index_sequence_for
  67. template<class... T> using index_sequence_for = make_integer_sequence<std::size_t, sizeof...(T)>;
  68. } // namespace mp11
  69. } // namespace boost
  70. #endif // #ifndef BOOST_MP11_INTEGER_SEQUENCE_HPP_INCLUDED