group.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*!
  2. @file
  3. Defines `boost::hana::group`.
  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_GROUP_HPP
  9. #define BOOST_HANA_GROUP_HPP
  10. #include <boost/hana/fwd/group.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/nested_by.hpp> // required by fwd decl
  19. #include <boost/hana/equal.hpp>
  20. #include <boost/hana/length.hpp>
  21. #include <cstddef>
  22. #include <utility>
  23. BOOST_HANA_NAMESPACE_BEGIN
  24. //! @cond
  25. template <typename Xs>
  26. constexpr auto group_t::operator()(Xs&& xs) const {
  27. using S = typename hana::tag_of<Xs>::type;
  28. using Group = BOOST_HANA_DISPATCH_IF(group_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::group(xs) requires 'xs' to be a Sequence");
  34. #endif
  35. return Group::apply(static_cast<Xs&&>(xs));
  36. }
  37. template <typename Xs, typename Predicate>
  38. constexpr auto group_t::operator()(Xs&& xs, Predicate&& pred) const {
  39. using S = typename hana::tag_of<Xs>::type;
  40. using Group = BOOST_HANA_DISPATCH_IF(group_impl<S>,
  41. hana::Sequence<S>::value
  42. );
  43. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  44. static_assert(hana::Sequence<S>::value,
  45. "hana::group(xs, predicate) requires 'xs' to be a Sequence");
  46. #endif
  47. return Group::apply(static_cast<Xs&&>(xs),
  48. static_cast<Predicate&&>(pred));
  49. }
  50. //! @endcond
  51. namespace detail {
  52. template <typename Xs, std::size_t ...i>
  53. constexpr auto get_subsequence_(Xs&& xs, std::index_sequence<i...>) {
  54. using S = typename hana::tag_of<Xs>::type;
  55. return hana::make<S>(hana::at_c<i>(static_cast<Xs&&>(xs))...);
  56. }
  57. template <std::size_t offset, typename Indices>
  58. struct offset_by;
  59. template <std::size_t offset, std::size_t ...i>
  60. struct offset_by<offset, std::index_sequence<i...>> {
  61. using type = std::index_sequence<(offset + i)...>;
  62. };
  63. template <bool ...b>
  64. struct group_indices {
  65. static constexpr bool bs[sizeof...(b)] = {b...};
  66. static constexpr std::size_t n_groups =
  67. detail::count(bs, bs + sizeof(bs), false) + 1;
  68. static constexpr auto compute_info() {
  69. detail::array<std::size_t, n_groups> sizes{}, offsets{};
  70. for (std::size_t g = 0, i = 0, offset = 0; g < n_groups; ++g) {
  71. offsets[g] = offset;
  72. sizes[g] = 1;
  73. while (i < sizeof...(b) && bs[i++])
  74. ++sizes[g];
  75. offset += sizes[g];
  76. }
  77. return std::make_pair(offsets, sizes);
  78. }
  79. static constexpr auto info = compute_info();
  80. static constexpr auto group_offsets = info.first;
  81. static constexpr auto group_sizes = info.second;
  82. template <typename S, typename Xs, std::size_t ...i>
  83. static constexpr auto finish(Xs&& xs, std::index_sequence<i...>) {
  84. return hana::make<S>(
  85. detail::get_subsequence_(
  86. static_cast<Xs&&>(xs),
  87. typename offset_by<
  88. group_offsets[i],
  89. std::make_index_sequence<group_sizes[i]>
  90. >::type{}
  91. )...
  92. );
  93. }
  94. };
  95. } // end namespace detail
  96. template <typename S, bool condition>
  97. struct group_impl<S, when<condition>> : default_ {
  98. template <typename Xs, typename Pred, std::size_t ...i>
  99. static constexpr auto
  100. group_helper(Xs&& xs, Pred&& pred, std::index_sequence<0, i...>) {
  101. using info = detail::group_indices<static_cast<bool>(decltype(
  102. pred(hana::at_c<i - 1>(static_cast<Xs&&>(xs)),
  103. hana::at_c<i>(static_cast<Xs&&>(xs)))
  104. )::value)...>;
  105. return info::template finish<S>(static_cast<Xs&&>(xs),
  106. std::make_index_sequence<info::n_groups>{}
  107. );
  108. }
  109. template <typename Xs, typename Pred>
  110. static constexpr auto
  111. group_helper(Xs&& xs, Pred&&, std::index_sequence<0>) {
  112. return hana::make<S>(static_cast<Xs&&>(xs));
  113. }
  114. template <typename Xs, typename Pred>
  115. static constexpr auto
  116. group_helper(Xs&&, Pred&&, std::index_sequence<>) {
  117. return hana::make<S>();
  118. }
  119. template <typename Xs, typename Pred>
  120. static constexpr auto apply(Xs&& xs, Pred&& pred) {
  121. constexpr std::size_t len = decltype(hana::length(xs))::value;
  122. return group_helper(static_cast<Xs&&>(xs),
  123. static_cast<Pred&&>(pred),
  124. std::make_index_sequence<len>{});
  125. }
  126. template <typename Xs>
  127. static constexpr auto apply(Xs&& xs)
  128. { return group_impl::apply(static_cast<Xs&&>(xs), hana::equal); }
  129. };
  130. BOOST_HANA_NAMESPACE_END
  131. #endif // !BOOST_HANA_GROUP_HPP