| 1 | /*! |
| 2 | @file |
| 3 | Defines `boost::hana::intersperse`. |
| 4 | |
| 5 | Copyright Louis Dionne 2013-2022 |
| 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_INTERSPERSE_HPP |
| 11 | #define BOOST_HANA_INTERSPERSE_HPP |
| 12 | |
| 13 | #include <boost/hana/fwd/intersperse.hpp> |
| 14 | |
| 15 | #include <boost/hana/at.hpp> |
| 16 | #include <boost/hana/bool.hpp> |
| 17 | #include <boost/hana/concept/sequence.hpp> |
| 18 | #include <boost/hana/config.hpp> |
| 19 | #include <boost/hana/core/dispatch.hpp> |
| 20 | #include <boost/hana/core/make.hpp> |
| 21 | #include <boost/hana/length.hpp> |
| 22 | |
| 23 | #include <cstddef> |
| 24 | #include <utility> |
| 25 | |
| 26 | |
| 27 | namespace boost { namespace hana { |
| 28 | //! @cond |
| 29 | template <typename Xs, typename Z> |
| 30 | constexpr auto intersperse_t::operator()(Xs&& xs, Z&& z) const { |
| 31 | using S = typename hana::tag_of<Xs>::type; |
| 32 | using Intersperse = BOOST_HANA_DISPATCH_IF(intersperse_impl<S>, |
| 33 | hana::Sequence<S>::value |
| 34 | ); |
| 35 | |
| 36 | #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS |
| 37 | static_assert(hana::Sequence<S>::value, |
| 38 | "hana::intersperse(xs, z) requires 'xs' to be a Sequence" ); |
| 39 | #endif |
| 40 | |
| 41 | return Intersperse::apply(static_cast<Xs&&>(xs), static_cast<Z&&>(z)); |
| 42 | } |
| 43 | //! @endcond |
| 44 | |
| 45 | template <typename S, bool condition> |
| 46 | struct intersperse_impl<S, when<condition>> : default_ { |
| 47 | template <std::size_t i, typename Xs, typename Z> |
| 48 | static constexpr decltype(auto) |
| 49 | pick(Xs&&, Z&& z, hana::false_ /* odd index */) |
| 50 | { return static_cast<Z&&>(z); } |
| 51 | |
| 52 | template <std::size_t i, typename Xs, typename Z> |
| 53 | static constexpr decltype(auto) |
| 54 | pick(Xs&& xs, Z&&, hana::true_ /* even index */) |
| 55 | { return hana::at_c<(i + 1) / 2>(static_cast<Xs&&>(xs)); } |
| 56 | |
| 57 | template <typename Xs, typename Z, std::size_t ...i> |
| 58 | static constexpr auto |
| 59 | intersperse_helper(Xs&& xs, Z&& z, std::index_sequence<i...>) { |
| 60 | return hana::make<S>( |
| 61 | pick<i>(static_cast<Xs&&>(xs), static_cast<Z&&>(z), |
| 62 | hana::bool_c<(i % 2 == 0)>)... |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | template <typename Xs, typename Z> |
| 67 | static constexpr auto apply(Xs&& xs, Z&& z) { |
| 68 | constexpr std::size_t size = decltype(hana::length(xs))::value; |
| 69 | constexpr std::size_t new_size = size == 0 ? 0 : (size * 2) - 1; |
| 70 | return intersperse_helper(static_cast<Xs&&>(xs), static_cast<Z&&>(z), |
| 71 | std::make_index_sequence<new_size>{}); |
| 72 | } |
| 73 | }; |
| 74 | }} // end namespace boost::hana |
| 75 | |
| 76 | #endif // !BOOST_HANA_INTERSPERSE_HPP |
| 77 | |