Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:03

0001 /*!
0002 @file
0003 Defines `boost::hana::group`.
0004 
0005 Copyright Louis Dionne 2013-2022
0006 Distributed under the Boost Software License, Version 1.0.
0007 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
0008  */
0009 
0010 #ifndef BOOST_HANA_GROUP_HPP
0011 #define BOOST_HANA_GROUP_HPP
0012 
0013 #include <boost/hana/fwd/group.hpp>
0014 
0015 #include <boost/hana/at.hpp>
0016 #include <boost/hana/concept/sequence.hpp>
0017 #include <boost/hana/config.hpp>
0018 #include <boost/hana/core/dispatch.hpp>
0019 #include <boost/hana/core/make.hpp>
0020 #include <boost/hana/detail/algorithm.hpp>
0021 #include <boost/hana/detail/array.hpp>
0022 #include <boost/hana/detail/nested_by.hpp> // required by fwd decl
0023 #include <boost/hana/equal.hpp>
0024 #include <boost/hana/length.hpp>
0025 
0026 #include <cstddef>
0027 #include <utility>
0028 
0029 
0030 namespace boost { namespace hana {
0031     //! @cond
0032     template <typename Xs>
0033     constexpr auto group_t::operator()(Xs&& xs) const {
0034         using S = typename hana::tag_of<Xs>::type;
0035         using Group = BOOST_HANA_DISPATCH_IF(group_impl<S>,
0036             hana::Sequence<S>::value
0037         );
0038 
0039     #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
0040         static_assert(hana::Sequence<S>::value,
0041         "hana::group(xs) requires 'xs' to be a Sequence");
0042     #endif
0043 
0044         return Group::apply(static_cast<Xs&&>(xs));
0045     }
0046 
0047     template <typename Xs, typename Predicate>
0048     constexpr auto group_t::operator()(Xs&& xs, Predicate&& pred) const {
0049         using S = typename hana::tag_of<Xs>::type;
0050         using Group = BOOST_HANA_DISPATCH_IF(group_impl<S>,
0051             hana::Sequence<S>::value
0052         );
0053 
0054     #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
0055         static_assert(hana::Sequence<S>::value,
0056         "hana::group(xs, predicate) requires 'xs' to be a Sequence");
0057     #endif
0058 
0059         return Group::apply(static_cast<Xs&&>(xs),
0060                             static_cast<Predicate&&>(pred));
0061     }
0062     //! @endcond
0063 
0064     namespace detail {
0065         template <typename Xs, std::size_t ...i>
0066         constexpr auto get_subsequence_(Xs&& xs, std::index_sequence<i...>) {
0067             using S = typename hana::tag_of<Xs>::type;
0068             return hana::make<S>(hana::at_c<i>(static_cast<Xs&&>(xs))...);
0069         }
0070 
0071         template <std::size_t offset, typename Indices>
0072         struct offset_by;
0073 
0074         template <std::size_t offset, std::size_t ...i>
0075         struct offset_by<offset, std::index_sequence<i...>> {
0076             using type = std::index_sequence<(offset + i)...>;
0077         };
0078 
0079         template <bool ...b>
0080         struct group_indices {
0081             static constexpr bool bs[sizeof...(b)] = {b...};
0082             static constexpr std::size_t n_groups =
0083                     detail::count(bs, bs + sizeof(bs), false) + 1;
0084 
0085             static constexpr auto compute_info() {
0086                 detail::array<std::size_t, n_groups> sizes{}, offsets{};
0087                 for (std::size_t g = 0, i = 0, offset = 0; g < n_groups; ++g) {
0088                     offsets[g] = offset;
0089 
0090                     sizes[g] = 1;
0091                     while (i < sizeof...(b) && bs[i++])
0092                         ++sizes[g];
0093 
0094                     offset += sizes[g];
0095                 }
0096                 return std::make_pair(offsets, sizes);
0097             }
0098 
0099             static constexpr auto info = compute_info();
0100             static constexpr auto group_offsets = info.first;
0101             static constexpr auto group_sizes = info.second;
0102 
0103             template <typename S, typename Xs, std::size_t ...i>
0104             static constexpr auto finish(Xs&& xs, std::index_sequence<i...>) {
0105                 return hana::make<S>(
0106                     detail::get_subsequence_(
0107                         static_cast<Xs&&>(xs),
0108                         typename offset_by<
0109                             group_offsets[i],
0110                             std::make_index_sequence<group_sizes[i]>
0111                         >::type{}
0112                     )...
0113                 );
0114             }
0115         };
0116     } // end namespace detail
0117 
0118     template <typename S, bool condition>
0119     struct group_impl<S, when<condition>> : default_ {
0120         template <typename Xs, typename Pred, std::size_t ...i>
0121         static constexpr auto
0122         group_helper(Xs&& xs, Pred&& pred, std::index_sequence<0, i...>) {
0123             using info = detail::group_indices<static_cast<bool>(decltype(
0124                 pred(hana::at_c<i - 1>(static_cast<Xs&&>(xs)),
0125                      hana::at_c<i>(static_cast<Xs&&>(xs)))
0126             )::value)...>;
0127             return info::template finish<S>(static_cast<Xs&&>(xs),
0128                 std::make_index_sequence<info::n_groups>{}
0129             );
0130         }
0131 
0132         template <typename Xs, typename Pred>
0133         static constexpr auto
0134         group_helper(Xs&& xs, Pred&&, std::index_sequence<0>) {
0135             return hana::make<S>(static_cast<Xs&&>(xs));
0136         }
0137 
0138         template <typename Xs, typename Pred>
0139         static constexpr auto
0140         group_helper(Xs&&, Pred&&, std::index_sequence<>) {
0141             return hana::make<S>();
0142         }
0143 
0144         template <typename Xs, typename Pred>
0145         static constexpr auto apply(Xs&& xs, Pred&& pred) {
0146             constexpr std::size_t len = decltype(hana::length(xs))::value;
0147             return group_helper(static_cast<Xs&&>(xs),
0148                                 static_cast<Pred&&>(pred),
0149                                 std::make_index_sequence<len>{});
0150         }
0151 
0152         template <typename Xs>
0153         static constexpr auto apply(Xs&& xs)
0154         { return group_impl::apply(static_cast<Xs&&>(xs), hana::equal); }
0155     };
0156 }} // end namespace boost::hana
0157 
0158 #endif // !BOOST_HANA_GROUP_HPP