cartesian_product.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*!
  2. @file
  3. Defines `boost::hana::cartesian_product`.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_CARTESIAN_PRODUCT_HPP
  9. #define BOOST_HANA_CARTESIAN_PRODUCT_HPP
  10. #include <boost/hana/fwd/cartesian_product.hpp>
  11. #include <boost/hana/at.hpp>
  12. #include <boost/hana/concept/sequence.hpp>
  13. #include <boost/hana/config.hpp>
  14. #include <boost/hana/core/dispatch.hpp>
  15. #include <boost/hana/core/make.hpp>
  16. #include <boost/hana/detail/array.hpp>
  17. #include <boost/hana/integral_constant.hpp>
  18. #include <boost/hana/length.hpp>
  19. #include <boost/hana/unpack.hpp>
  20. #include <cstddef>
  21. #include <utility>
  22. BOOST_HANA_NAMESPACE_BEGIN
  23. //! @cond
  24. template <typename Xs>
  25. constexpr auto cartesian_product_t::operator()(Xs&& xs) const {
  26. using S = typename hana::tag_of<Xs>::type;
  27. using CartesianProduct = BOOST_HANA_DISPATCH_IF(
  28. cartesian_product_impl<S>,
  29. hana::Sequence<S>::value
  30. );
  31. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  32. static_assert(hana::Sequence<S>::value,
  33. "hana::cartesian_product(xs) requires 'xs' to be a Sequence");
  34. #endif
  35. return CartesianProduct::apply(static_cast<Xs&&>(xs));
  36. }
  37. //! @endcond
  38. namespace detail {
  39. template <std::size_t ...Lengths>
  40. struct cartesian_product_indices {
  41. static constexpr std::size_t total_length() {
  42. std::size_t lengths[sizeof...(Lengths)] = {Lengths...};
  43. std::size_t r = 1;
  44. for (std::size_t len: lengths)
  45. r *= len;
  46. return r;
  47. }
  48. static constexpr std::size_t length = total_length();
  49. static constexpr auto indices_of(std::size_t i) {
  50. constexpr std::size_t lengths[sizeof...(Lengths)] = {Lengths...};
  51. constexpr std::size_t n = sizeof...(Lengths);
  52. detail::array<std::size_t, n> result{};
  53. for (std::size_t j = n; j--;) {
  54. result[j] = i % lengths[j];
  55. i /= lengths[j];
  56. }
  57. return result;
  58. }
  59. template <typename S, std::size_t n, std::size_t ...k, typename ...Xs>
  60. static constexpr auto
  61. product_element(std::index_sequence<k...>, Xs&& ...xs) {
  62. constexpr auto indices = indices_of(n);
  63. return hana::make<S>(hana::at_c<indices[k]>(xs)...);
  64. }
  65. template <typename S, std::size_t ...n, typename ...Xs>
  66. static constexpr auto
  67. create_product(std::index_sequence<n...>, Xs&& ...xs) {
  68. return hana::make<S>(product_element<S, n>(
  69. std::make_index_sequence<sizeof...(Xs)>{}, xs...
  70. )...);
  71. }
  72. };
  73. }
  74. // Credits: implementation adapted from http://github.com/alexk7/hel.
  75. template <typename S, bool condition>
  76. struct cartesian_product_impl<S, when<condition>> : default_ {
  77. template <typename Xs>
  78. static constexpr auto apply(Xs&& xs) {
  79. return hana::unpack(static_cast<Xs&&>(xs), cartesian_product_impl{});
  80. }
  81. template <typename ...Xs>
  82. constexpr auto operator()(Xs&& ...xs) const {
  83. using indices = detail::cartesian_product_indices<
  84. decltype(hana::length(xs))::value...
  85. >;
  86. return indices::template create_product<S>(
  87. std::make_index_sequence<indices::length>{},
  88. static_cast<Xs&&>(xs)...);
  89. }
  90. constexpr auto operator()() const {
  91. return hana::make<S>();
  92. }
  93. };
  94. BOOST_HANA_NAMESPACE_END
  95. #endif // !BOOST_HANA_CARTESIAN_PRODUCT_HPP