/*! @file Defines `boost::hana::cycle`. @copyright Louis Dionne 2013-2016 Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) */ #ifndef BOOST_HANA_CYCLE_HPP #define BOOST_HANA_CYCLE_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include #include BOOST_HANA_NAMESPACE_BEGIN //! @cond template constexpr auto cycle_t::operator()(Xs&& xs, N const& n) const { using M = typename hana::tag_of::type; using Cycle = BOOST_HANA_DISPATCH_IF(cycle_impl, hana::MonadPlus::value ); #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS static_assert(hana::MonadPlus::value, "hana::cycle(xs, n) requires 'xs' to be a MonadPlus"); static_assert(hana::IntegralConstant::value, "hana::cycle(xs, n) requires 'n' to be an IntegralConstant"); #endif static_assert(N::value >= 0, "hana::cycle(xs, n) requires 'n' to be non-negative"); return Cycle::apply(static_cast(xs), n); } //! @endcond namespace detail { template struct cycle_helper; template struct cycle_helper { template static constexpr auto apply(Xs const&) { return hana::empty(); } }; template struct cycle_helper { template static constexpr auto apply(Xs const& xs) { return cycle_helper::apply(hana::concat(xs, xs)); } }; template struct cycle_helper { template static constexpr auto apply(Xs const& xs) { return hana::concat(xs, cycle_helper::apply(xs)); } }; } template struct cycle_impl> : default_ { template static constexpr auto apply(Xs const& xs, N const&) { constexpr std::size_t n = N::value; return detail::cycle_helper::apply(xs); } }; namespace detail { template struct cycle_indices { static constexpr auto compute_value() { detail::array indices{}; // Avoid (incorrect) Clang warning about remainder by zero // in the loop below. std::size_t len = Len; for (std::size_t i = 0; i < N * Len; ++i) indices[i] = i % len; return indices; } static constexpr auto value = compute_value(); }; } template struct cycle_impl::value>> { template static constexpr auto cycle_helper(Xs&& xs, std::index_sequence) { constexpr auto indices = Indices::value; (void)indices; // workaround GCC warning when sizeof...(i) == 0 return hana::make(hana::at_c(xs)...); } template static constexpr auto apply(Xs&& xs, N const&) { constexpr std::size_t n = N::value; constexpr std::size_t len = decltype(hana::length(xs))::value; using Indices = detail::cycle_indices; return cycle_helper(static_cast(xs), std::make_index_sequence{}); } }; BOOST_HANA_NAMESPACE_END #endif // !BOOST_HANA_CYCLE_HPP