3 Defines `boost::hana::detail::unpack_flatten`.
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_DETAIL_UNPACK_FLATTEN_HPP
11 #define BOOST_HANA_DETAIL_UNPACK_FLATTEN_HPP
13 #include <boost/hana/at.hpp>
14 #include <boost/hana/config.hpp>
15 #include <boost/hana/detail/algorithm.hpp>
16 #include <boost/hana/detail/array.hpp>
17 #include <boost/hana/length.hpp>
18 #include <boost/hana/unpack.hpp>
24 BOOST_HANA_NAMESPACE_BEGIN namespace detail {
25 template <std::size_t ...Lengths>
26 struct flatten_indices {
27 // avoid empty arrays by appending 0 to `lengths`
28 static constexpr std::size_t lengths[] = {Lengths..., 0};
29 static constexpr auto flat_length =
30 detail::accumulate(lengths, lengths + sizeof...(Lengths), 0);
33 static constexpr auto compute() {
34 detail::array<std::size_t, flat_length> indices{};
35 for (std::size_t index = 0, i = 0; i < sizeof...(Lengths); ++i)
36 for (std::size_t j = 0; j < lengths[i]; ++j, ++index)
37 indices[index] = (Inner ? i : j);
41 static constexpr auto inner = compute<true>();
42 static constexpr auto outer = compute<false>();
44 template <typename Xs, typename F, std::size_t ...i>
45 static constexpr decltype(auto)
46 apply(Xs&& xs, F&& f, std::index_sequence<i...>) {
47 return static_cast<F&&>(f)(
48 hana::at_c<outer[i]>(hana::at_c<inner[i]>(
55 struct make_flatten_indices {
56 template <typename ...Xs>
57 auto operator()(Xs const& ...xs) const -> detail::flatten_indices<
58 decltype(hana::length(xs))::value...
62 template <typename Xs, typename F>
63 constexpr decltype(auto) unpack_flatten(Xs&& xs, F&& f) {
64 using Indices = decltype(hana::unpack(xs, make_flatten_indices{}));
65 return Indices::apply(static_cast<Xs&&>(xs), static_cast<F&&>(f),
66 std::make_index_sequence<Indices::flat_length>{});
68 } BOOST_HANA_NAMESPACE_END
70 #endif // !BOOST_HANA_DETAIL_UNPACK_FLATTEN_HPP