]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/hana/include/boost/hana/find_if.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / hana / include / boost / hana / find_if.hpp
1 /*!
2 @file
3 Defines `boost::hana::find_if`.
4
5 @copyright Louis Dionne 2013-2016
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_FIND_IF_HPP
11 #define BOOST_HANA_FIND_IF_HPP
12
13 #include <boost/hana/fwd/find_if.hpp>
14
15 #include <boost/hana/accessors.hpp>
16 #include <boost/hana/at.hpp>
17 #include <boost/hana/bool.hpp>
18 #include <boost/hana/concept/iterable.hpp>
19 #include <boost/hana/concept/searchable.hpp>
20 #include <boost/hana/concept/sequence.hpp>
21 #include <boost/hana/concept/struct.hpp>
22 #include <boost/hana/config.hpp>
23 #include <boost/hana/core/dispatch.hpp>
24 #include <boost/hana/detail/decay.hpp>
25 #include <boost/hana/drop_while.hpp>
26 #include <boost/hana/first.hpp>
27 #include <boost/hana/front.hpp>
28 #include <boost/hana/functional/compose.hpp>
29 #include <boost/hana/is_empty.hpp>
30 #include <boost/hana/length.hpp>
31 #include <boost/hana/not.hpp>
32 #include <boost/hana/optional.hpp>
33 #include <boost/hana/second.hpp>
34 #include <boost/hana/transform.hpp>
35
36 #include <cstddef>
37 #include <utility>
38
39
40 BOOST_HANA_NAMESPACE_BEGIN
41 //! @cond
42 template <typename Xs, typename Pred>
43 constexpr auto find_if_t::operator()(Xs&& xs, Pred&& pred) const {
44 using S = typename hana::tag_of<Xs>::type;
45 using FindIf = BOOST_HANA_DISPATCH_IF(find_if_impl<S>,
46 hana::Searchable<S>::value
47 );
48
49 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
50 static_assert(hana::Searchable<S>::value,
51 "hana::find_if(xs, pred) requires 'xs' to be a Searchable");
52 #endif
53
54 return FindIf::apply(static_cast<Xs&&>(xs), static_cast<Pred&&>(pred));
55 }
56 //! @endcond
57
58 template <typename S, bool condition>
59 struct find_if_impl<S, when<condition>> : default_ {
60 template <typename ...Args>
61 static constexpr auto apply(Args&& ...) = delete;
62 };
63
64 namespace detail {
65 template <typename Xs, typename Pred, std::size_t i, std::size_t N, bool Done>
66 struct advance_until;
67
68 template <typename Xs, typename Pred, std::size_t i, std::size_t N>
69 struct advance_until<Xs, Pred, i, N, false>
70 : advance_until<Xs, Pred, i + 1, N, static_cast<bool>(detail::decay<decltype(
71 std::declval<Pred>()(hana::at_c<i>(std::declval<Xs>()))
72 )>::type::value)>
73 { };
74
75 template <typename Xs, typename Pred, std::size_t N>
76 struct advance_until<Xs, Pred, N, N, false> {
77 template <typename Ys>
78 static constexpr auto apply(Ys&&) {
79 return hana::nothing;
80 }
81 };
82
83 template <typename Xs, typename Pred, std::size_t i, std::size_t N>
84 struct advance_until<Xs, Pred, i, N, true> {
85 template <typename Ys>
86 static constexpr auto apply(Ys&& ys) {
87 return hana::just(hana::at_c<i - 1>(static_cast<Ys&&>(ys)));
88 }
89 };
90 }
91
92 template <typename S>
93 struct find_if_impl<S, when<Sequence<S>::value>> {
94 template <typename Xs, typename Pred>
95 static constexpr auto apply(Xs&& xs, Pred&&) {
96 constexpr std::size_t N = decltype(hana::length(xs))::value;
97 return detail::advance_until<Xs&&, Pred&&, 0, N, false>::apply(
98 static_cast<Xs&&>(xs)
99 );
100 }
101 };
102
103 template <typename It>
104 struct find_if_impl<It, when<hana::Iterable<It>::value && !Sequence<It>::value>> {
105 template <typename Xs, typename Pred>
106 static constexpr auto find_if_helper(Xs&& xs, Pred&& pred, hana::true_) {
107 return hana::just(hana::front(
108 hana::drop_while(static_cast<Xs&&>(xs),
109 hana::compose(hana::not_, static_cast<Pred&&>(pred)))
110 ));
111 }
112
113 template <typename Xs, typename Pred>
114 static constexpr auto find_if_helper(Xs&&, Pred&&, hana::false_) {
115 return hana::nothing;
116 }
117
118 template <typename Xs, typename Pred>
119 static constexpr auto apply(Xs&& xs, Pred&& pred) {
120 constexpr bool found = !decltype(
121 hana::is_empty(hana::drop_while(static_cast<Xs&&>(xs),
122 hana::compose(hana::not_, static_cast<Pred&&>(pred))))
123 )::value;
124 return find_if_impl::find_if_helper(static_cast<Xs&&>(xs),
125 static_cast<Pred&&>(pred),
126 hana::bool_<found>{});
127 }
128 };
129
130 template <typename T, std::size_t N>
131 struct find_if_impl<T[N]> {
132 template <typename Xs>
133 static constexpr auto find_if_helper(Xs&&, hana::false_)
134 { return hana::nothing; }
135
136 template <typename Xs>
137 static constexpr auto find_if_helper(Xs&& xs, hana::true_)
138 { return hana::just(static_cast<Xs&&>(xs)[0]); }
139
140 template <typename Xs, typename Pred>
141 static constexpr auto apply(Xs&& xs, Pred&& pred) {
142 return find_if_helper(static_cast<Xs&&>(xs),
143 hana::bool_c<decltype(
144 static_cast<Pred&&>(pred)(static_cast<Xs&&>(xs)[0])
145 )::value>
146 );
147 }
148 };
149
150 namespace struct_detail {
151 template <typename X>
152 struct get_member {
153 X x;
154 template <typename Member>
155 constexpr decltype(auto) operator()(Member&& member) && {
156 return hana::second(static_cast<Member&&>(member))(
157 static_cast<X&&>(x)
158 );
159 }
160 };
161 }
162
163 template <typename S>
164 struct find_if_impl<S, when<hana::Struct<S>::value>> {
165 template <typename X, typename Pred>
166 static constexpr decltype(auto) apply(X&& x, Pred&& pred) {
167 return hana::transform(
168 hana::find_if(hana::accessors<S>(),
169 hana::compose(static_cast<Pred&&>(pred), hana::first)
170 ),
171 struct_detail::get_member<X>{static_cast<X&&>(x)}
172 );
173 }
174 };
175 BOOST_HANA_NAMESPACE_END
176
177 #endif // !BOOST_HANA_FIND_IF_HPP