3 Defines `boost::hana::intersperse`.
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_INTERSPERSE_HPP
11 #define BOOST_HANA_INTERSPERSE_HPP
13 #include <boost/hana/fwd/intersperse.hpp>
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>
27 BOOST_HANA_NAMESPACE_BEGIN
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
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");
41 return Intersperse::apply(static_cast<Xs&&>(xs), static_cast<Z&&>(z));
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); }
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)); }
57 template <typename Xs, typename Z, std::size_t ...i>
59 intersperse_helper(Xs&& xs, Z&& z, std::index_sequence<i...>) {
61 pick<i>(static_cast<Xs&&>(xs), static_cast<Z&&>(z),
62 hana::bool_c<(i % 2 == 0)>)...
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>{});
74 BOOST_HANA_NAMESPACE_END
76 #endif // !BOOST_HANA_INTERSPERSE_HPP