partition.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*!
  2. @file
  3. Defines `boost::hana::partition`.
  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_PARTITION_HPP
  9. #define BOOST_HANA_PARTITION_HPP
  10. #include <boost/hana/fwd/partition.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/algorithm.hpp>
  17. #include <boost/hana/detail/array.hpp>
  18. #include <boost/hana/detail/decay.hpp>
  19. #include <boost/hana/detail/nested_by.hpp> // required by fwd decl
  20. #include <boost/hana/pair.hpp>
  21. #include <boost/hana/unpack.hpp>
  22. #include <cstddef>
  23. #include <utility>
  24. BOOST_HANA_NAMESPACE_BEGIN
  25. //! @cond
  26. template <typename Xs, typename Pred>
  27. constexpr auto partition_t::operator()(Xs&& xs, Pred&& pred) const {
  28. using S = typename hana::tag_of<Xs>::type;
  29. using Partition = BOOST_HANA_DISPATCH_IF(partition_impl<S>,
  30. hana::Sequence<S>::value
  31. );
  32. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  33. static_assert(hana::Sequence<S>::value,
  34. "hana::partition(xs, pred) requires 'xs' to be a Sequence");
  35. #endif
  36. return Partition::apply(static_cast<Xs&&>(xs), static_cast<Pred&&>(pred));
  37. }
  38. //! @endcond
  39. namespace detail {
  40. template <bool ...B>
  41. struct partition_indices {
  42. static constexpr detail::array<bool, sizeof...(B)> results{{B...}};
  43. static constexpr std::size_t left_size =
  44. detail::count(results.begin(), results.end(), true);
  45. static constexpr std::size_t right_size = sizeof...(B) - left_size;
  46. static constexpr auto compute_left() {
  47. detail::array<std::size_t, left_size> indices{};
  48. std::size_t* left = &indices[0];
  49. for (std::size_t i = 0; i < sizeof...(B); ++i)
  50. if (results[i])
  51. *left++ = i;
  52. return indices;
  53. }
  54. static constexpr auto compute_right() {
  55. detail::array<std::size_t, right_size> indices{};
  56. std::size_t* right = &indices[0];
  57. for (std::size_t i = 0; i < sizeof...(B); ++i)
  58. if (!results[i])
  59. *right++ = i;
  60. return indices;
  61. }
  62. static constexpr auto left_indices = compute_left();
  63. static constexpr auto right_indices = compute_right();
  64. template <typename S, typename Xs, std::size_t ...l, std::size_t ...r>
  65. static constexpr auto apply(Xs&& xs, std::index_sequence<l...>,
  66. std::index_sequence<r...>)
  67. {
  68. return hana::make<hana::pair_tag>(
  69. hana::make<S>(hana::at_c<left_indices[l]>(static_cast<Xs&&>(xs))...),
  70. hana::make<S>(hana::at_c<right_indices[r]>(static_cast<Xs&&>(xs))...)
  71. );
  72. }
  73. };
  74. template <typename Pred>
  75. struct deduce_partition_indices {
  76. template <typename ...Xs>
  77. auto operator()(Xs&& ...xs) const -> detail::partition_indices<
  78. static_cast<bool>(detail::decay<
  79. decltype(std::declval<Pred>()(static_cast<Xs&&>(xs)))
  80. >::type::value)...
  81. > { return {}; }
  82. };
  83. }
  84. template <typename S, bool condition>
  85. struct partition_impl<S, when<condition>> : default_ {
  86. template <typename Xs, typename Pred>
  87. static constexpr auto apply(Xs&& xs, Pred&&) {
  88. using Indices = decltype(hana::unpack(
  89. static_cast<Xs&&>(xs), detail::deduce_partition_indices<Pred&&>{}
  90. ));
  91. return Indices::template apply<S>(
  92. static_cast<Xs&&>(xs),
  93. std::make_index_sequence<Indices::left_size>{},
  94. std::make_index_sequence<Indices::right_size>{}
  95. );
  96. }
  97. };
  98. BOOST_HANA_NAMESPACE_END
  99. #endif // !BOOST_HANA_PARTITION_HPP