/*! @file Defines `boost::hana::detail::unpack_flatten`. @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_DETAIL_UNPACK_FLATTEN_HPP #define BOOST_HANA_DETAIL_UNPACK_FLATTEN_HPP #include #include #include #include #include #include #include #include BOOST_HANA_NAMESPACE_BEGIN namespace detail { template struct flatten_indices { // avoid empty arrays by appending 0 to `lengths` static constexpr std::size_t lengths[] = {Lengths..., 0}; static constexpr auto flat_length = detail::accumulate(lengths, lengths + sizeof...(Lengths), 0); template static constexpr auto compute() { detail::array indices{}; for (std::size_t index = 0, i = 0; i < sizeof...(Lengths); ++i) for (std::size_t j = 0; j < lengths[i]; ++j, ++index) indices[index] = (Inner ? i : j); return indices; } static constexpr auto inner = compute(); static constexpr auto outer = compute(); template static constexpr decltype(auto) apply(Xs&& xs, F&& f, std::index_sequence) { return static_cast(f)( hana::at_c(hana::at_c( static_cast(xs) ))... ); } }; struct make_flatten_indices { template auto operator()(Xs const& ...xs) const -> detail::flatten_indices< decltype(hana::length(xs))::value... >; }; template constexpr decltype(auto) unpack_flatten(Xs&& xs, F&& f) { using Indices = decltype(hana::unpack(xs, make_flatten_indices{})); return Indices::apply(static_cast(xs), static_cast(f), std::make_index_sequence{}); } } BOOST_HANA_NAMESPACE_END #endif // !BOOST_HANA_DETAIL_UNPACK_FLATTEN_HPP