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