3 Defines `boost::hana::cartesian_product`.
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_CARTESIAN_PRODUCT_HPP
11 #define BOOST_HANA_CARTESIAN_PRODUCT_HPP
13 #include <boost/hana/fwd/cartesian_product.hpp>
15 #include <boost/hana/at.hpp>
16 #include <boost/hana/concept/sequence.hpp>
17 #include <boost/hana/config.hpp>
18 #include <boost/hana/core/dispatch.hpp>
19 #include <boost/hana/core/make.hpp>
20 #include <boost/hana/detail/array.hpp>
21 #include <boost/hana/integral_constant.hpp>
22 #include <boost/hana/length.hpp>
23 #include <boost/hana/unpack.hpp>
29 BOOST_HANA_NAMESPACE_BEGIN
31 template <typename Xs>
32 constexpr auto cartesian_product_t::operator()(Xs&& xs) const {
33 using S = typename hana::tag_of<Xs>::type;
34 using CartesianProduct = BOOST_HANA_DISPATCH_IF(
35 cartesian_product_impl<S>,
36 hana::Sequence<S>::value
39 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
40 static_assert(hana::Sequence<S>::value,
41 "hana::cartesian_product(xs) requires 'xs' to be a Sequence");
44 return CartesianProduct::apply(static_cast<Xs&&>(xs));
49 template <std::size_t ...Lengths>
50 struct cartesian_product_indices {
51 static constexpr std::size_t total_length() {
52 std::size_t lengths[] = {Lengths...};
54 for (std::size_t len: lengths)
59 static constexpr std::size_t length = total_length();
61 static constexpr auto indices_of(std::size_t i) {
62 constexpr std::size_t lengths[] = {Lengths...};
63 constexpr std::size_t n = sizeof...(Lengths);
64 detail::array<std::size_t, n> result{};
65 for (std::size_t j = n; j--;) {
66 result[j] = i % lengths[j];
72 template <typename S, std::size_t n, std::size_t ...k, typename ...Xs>
74 product_element(std::index_sequence<k...>, Xs&& ...xs) {
75 constexpr auto indices = indices_of(n);
76 return hana::make<S>(hana::at_c<indices[k]>(xs)...);
79 template <typename S, std::size_t ...n, typename ...Xs>
81 create_product(std::index_sequence<n...>, Xs&& ...xs) {
82 return hana::make<S>(product_element<S, n>(
83 std::make_index_sequence<sizeof...(Xs)>{}, xs...
89 // Credits: implementation adapted from http://github.com/alexk7/hel.
90 template <typename S, bool condition>
91 struct cartesian_product_impl<S, when<condition>> : default_ {
92 template <typename Xs>
93 static constexpr auto apply(Xs&& xs) {
94 return hana::unpack(static_cast<Xs&&>(xs), cartesian_product_impl{});
97 template <typename ...Xs>
98 constexpr auto operator()(Xs&& ...xs) const {
99 using indices = detail::cartesian_product_indices<
100 decltype(hana::length(xs))::value...
102 return indices::template create_product<S>(
103 std::make_index_sequence<indices::length>{},
104 static_cast<Xs&&>(xs)...);
107 constexpr auto operator()() const {
108 return hana::make<S>();
111 BOOST_HANA_NAMESPACE_END
113 #endif // !BOOST_HANA_CARTESIAN_PRODUCT_HPP