]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*! |
2 | @file | |
3 | Defines `boost::hana::detail::unpack_flatten`. | |
4 | ||
b32b8144 | 5 | @copyright Louis Dionne 2013-2017 |
7c673cae FG |
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_DETAIL_UNPACK_FLATTEN_HPP | |
11 | #define BOOST_HANA_DETAIL_UNPACK_FLATTEN_HPP | |
12 | ||
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> | |
19 | ||
20 | #include <cstddef> | |
21 | #include <utility> | |
22 | ||
23 | ||
1e59de90 | 24 | namespace boost { namespace hana { namespace detail { |
7c673cae FG |
25 | template <std::size_t ...Lengths> |
26 | struct flatten_indices { | |
27 | // avoid empty arrays by appending 0 to `lengths` | |
b32b8144 | 28 | static constexpr std::size_t lengths[sizeof...(Lengths) + 1] = {Lengths..., 0}; |
7c673cae FG |
29 | static constexpr auto flat_length = |
30 | detail::accumulate(lengths, lengths + sizeof...(Lengths), 0); | |
31 | ||
32 | template <bool Inner> | |
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); | |
38 | return indices; | |
39 | } | |
40 | ||
41 | static constexpr auto inner = compute<true>(); | |
42 | static constexpr auto outer = compute<false>(); | |
43 | ||
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]>( | |
49 | static_cast<Xs&&>(xs) | |
50 | ))... | |
51 | ); | |
52 | } | |
53 | }; | |
54 | ||
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... | |
59 | >; | |
60 | }; | |
61 | ||
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>{}); | |
67 | } | |
1e59de90 | 68 | } }} // end namespace boost::hana |
7c673cae FG |
69 | |
70 | #endif // !BOOST_HANA_DETAIL_UNPACK_FLATTEN_HPP |