Imported Upstream version 1.64.0
[platform/upstream/boost.git] / boost / hana / cycle.hpp
1 /*!
2 @file
3 Defines `boost::hana::cycle`.
4
5 @copyright Louis Dionne 2013-2017
6 Distributed under the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
8  */
9
10 #ifndef BOOST_HANA_CYCLE_HPP
11 #define BOOST_HANA_CYCLE_HPP
12
13 #include <boost/hana/fwd/cycle.hpp>
14
15 #include <boost/hana/at.hpp>
16 #include <boost/hana/concat.hpp>
17 #include <boost/hana/concept/integral_constant.hpp>
18 #include <boost/hana/concept/monad_plus.hpp>
19 #include <boost/hana/concept/sequence.hpp>
20 #include <boost/hana/config.hpp>
21 #include <boost/hana/core/dispatch.hpp>
22 #include <boost/hana/core/make.hpp>
23 #include <boost/hana/detail/array.hpp>
24 #include <boost/hana/empty.hpp>
25 #include <boost/hana/length.hpp>
26
27 #include <cstddef>
28 #include <utility>
29
30
31 BOOST_HANA_NAMESPACE_BEGIN
32     //! @cond
33     template <typename Xs, typename N>
34     constexpr auto cycle_t::operator()(Xs&& xs, N const& n) const {
35         using M = typename hana::tag_of<Xs>::type;
36         using Cycle = BOOST_HANA_DISPATCH_IF(cycle_impl<M>,
37             hana::MonadPlus<M>::value
38         );
39
40     #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
41         static_assert(hana::MonadPlus<M>::value,
42         "hana::cycle(xs, n) requires 'xs' to be a MonadPlus");
43
44         static_assert(hana::IntegralConstant<N>::value,
45         "hana::cycle(xs, n) requires 'n' to be an IntegralConstant");
46     #endif
47
48         static_assert(N::value >= 0,
49         "hana::cycle(xs, n) requires 'n' to be non-negative");
50
51         return Cycle::apply(static_cast<Xs&&>(xs), n);
52     }
53     //! @endcond
54
55     namespace detail {
56         template <typename M, std::size_t n, bool = n % 2 == 0>
57         struct cycle_helper;
58
59         template <typename M>
60         struct cycle_helper<M, 0, true> {
61             template <typename Xs>
62             static constexpr auto apply(Xs const&)
63             { return hana::empty<M>(); }
64         };
65
66         template <typename M, std::size_t n>
67         struct cycle_helper<M, n, true> {
68             template <typename Xs>
69             static constexpr auto apply(Xs const& xs)
70             { return cycle_helper<M, n/2>::apply(hana::concat(xs, xs)); }
71         };
72
73         template <typename M, std::size_t n>
74         struct cycle_helper<M, n, false> {
75             template <typename Xs>
76             static constexpr auto apply(Xs const& xs)
77             { return hana::concat(xs, cycle_helper<M, n-1>::apply(xs)); }
78         };
79     }
80
81     template <typename M, bool condition>
82     struct cycle_impl<M, when<condition>> : default_ {
83         template <typename Xs, typename N>
84         static constexpr auto apply(Xs const& xs, N const&) {
85             constexpr std::size_t n = N::value;
86             return detail::cycle_helper<M, n>::apply(xs);
87         }
88     };
89
90     namespace detail {
91         template <std::size_t N, std::size_t Len>
92         struct cycle_indices {
93             static constexpr auto compute_value() {
94                 detail::array<std::size_t, N * Len> indices{};
95                 // Avoid (incorrect) Clang warning about remainder by zero
96                 // in the loop below.
97                 std::size_t len = Len;
98                 for (std::size_t i = 0; i < N * Len; ++i)
99                     indices[i] = i % len;
100                 return indices;
101             }
102
103             static constexpr auto value = compute_value();
104         };
105     }
106
107     template <typename S>
108     struct cycle_impl<S, when<Sequence<S>::value>> {
109         template <typename Indices, typename Xs, std::size_t ...i>
110         static constexpr auto cycle_helper(Xs&& xs, std::index_sequence<i...>) {
111             constexpr auto indices = Indices::value;
112             (void)indices; // workaround GCC warning when sizeof...(i) == 0
113             return hana::make<S>(hana::at_c<indices[i]>(xs)...);
114         }
115
116         template <typename Xs, typename N>
117         static constexpr auto apply(Xs&& xs, N const&) {
118             constexpr std::size_t n = N::value;
119             constexpr std::size_t len = decltype(hana::length(xs))::value;
120             using Indices = detail::cycle_indices<n, len>;
121             return cycle_helper<Indices>(static_cast<Xs&&>(xs),
122                                          std::make_index_sequence<n * len>{});
123         }
124     };
125 BOOST_HANA_NAMESPACE_END
126
127 #endif // !BOOST_HANA_CYCLE_HPP