3 Defines `boost::hana::cycle`.
5 @copyright Louis Dionne 2013-2016
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)
10 #ifndef BOOST_HANA_CYCLE_HPP
11 #define BOOST_HANA_CYCLE_HPP
13 #include <boost/hana/fwd/cycle.hpp>
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>
31 BOOST_HANA_NAMESPACE_BEGIN
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
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");
44 static_assert(hana::IntegralConstant<N>::value,
45 "hana::cycle(xs, n) requires 'n' to be an IntegralConstant");
48 static_assert(N::value >= 0,
49 "hana::cycle(xs, n) requires 'n' to be non-negative");
51 return Cycle::apply(static_cast<Xs&&>(xs), n);
56 template <typename M, std::size_t n, bool = n % 2 == 0>
60 struct cycle_helper<M, 0, true> {
61 template <typename Xs>
62 static constexpr auto apply(Xs const&)
63 { return hana::empty<M>(); }
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)); }
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)); }
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);
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
97 std::size_t len = Len;
98 for (std::size_t i = 0; i < N * Len; ++i)
103 static constexpr auto value = compute_value();
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)...);
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>{});
125 BOOST_HANA_NAMESPACE_END
127 #endif // !BOOST_HANA_CYCLE_HPP