]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/hana/monadic_fold_left.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / hana / monadic_fold_left.hpp
1 /*!
2 @file
3 Defines `boost::hana::monadic_fold_left`.
4
5 @copyright Louis Dionne 2013-2017
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_MONADIC_FOLD_LEFT_HPP
11 #define BOOST_HANA_MONADIC_FOLD_LEFT_HPP
12
13 #include <boost/hana/fwd/monadic_fold_left.hpp>
14
15 #include <boost/hana/chain.hpp>
16 #include <boost/hana/concept/foldable.hpp>
17 #include <boost/hana/concept/monad.hpp>
18 #include <boost/hana/config.hpp>
19 #include <boost/hana/core/dispatch.hpp>
20 #include <boost/hana/detail/decay.hpp>
21 #include <boost/hana/fold_right.hpp>
22 #include <boost/hana/functional/curry.hpp>
23 #include <boost/hana/functional/partial.hpp>
24 #include <boost/hana/lift.hpp>
25
26 #include <type_traits>
27
28
29 BOOST_HANA_NAMESPACE_BEGIN
30 template <typename M>
31 struct monadic_fold_left_t {
32 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
33 static_assert(hana::Monad<M>::value,
34 "hana::monadic_fold_left<M> requires 'M' to be a Monad");
35 #endif
36
37 template <typename Xs, typename State, typename F>
38 constexpr decltype(auto) operator()(Xs&& xs, State&& state, F&& f) const {
39 using S = typename hana::tag_of<Xs>::type;
40 using MonadicFoldLeft = BOOST_HANA_DISPATCH_IF(monadic_fold_left_impl<S>,
41 hana::Foldable<S>::value
42 );
43
44 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
45 static_assert(hana::Foldable<S>::value,
46 "hana::monadic_fold_left<M>(xs, state, f) requires 'xs' to be Foldable");
47 #endif
48
49 return MonadicFoldLeft::template apply<M>(static_cast<Xs&&>(xs),
50 static_cast<State&&>(state),
51 static_cast<F&&>(f));
52 }
53
54 template <typename Xs, typename F>
55 constexpr decltype(auto) operator()(Xs&& xs, F&& f) const {
56 using S = typename hana::tag_of<Xs>::type;
57 using MonadicFoldLeft = BOOST_HANA_DISPATCH_IF(monadic_fold_left_impl<S>,
58 hana::Foldable<S>::value
59 );
60
61 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
62 static_assert(hana::Foldable<S>::value,
63 "hana::monadic_fold_left<M>(xs, f) requires 'xs' to be Foldable");
64 #endif
65
66 return MonadicFoldLeft::template apply<M>(static_cast<Xs&&>(xs),
67 static_cast<F&&>(f));
68 }
69 };
70
71 namespace detail {
72 struct foldlM_helper {
73 template <typename F, typename X, typename K, typename Z>
74 constexpr decltype(auto) operator()(F&& f, X&& x, K&& k, Z&& z) const {
75 return hana::chain(
76 static_cast<F&&>(f)(
77 static_cast<Z&&>(z),
78 static_cast<X&&>(x)
79 ),
80 static_cast<K&&>(k)
81 );
82 }
83 };
84
85 template <typename End, typename M, typename F>
86 struct monadic_foldl1_helper {
87 F f;
88 template <typename X, typename Y>
89 constexpr decltype(auto) operator()(X&& x, Y&& y) const
90 { return f(static_cast<X&&>(x), static_cast<Y&&>(y)); }
91 template <typename Y>
92 constexpr decltype(auto) operator()(End, Y&& y) const
93 { return hana::lift<M>(static_cast<Y&&>(y)); }
94 };
95 }
96
97 template <typename T, bool condition>
98 struct monadic_fold_left_impl<T, when<condition>> : default_ {
99 // with state
100 template <typename M, typename Xs, typename S, typename F>
101 static constexpr decltype(auto) apply(Xs&& xs, S&& s, F&& f) {
102 return hana::fold_right(
103 static_cast<Xs&&>(xs),
104 hana::lift<M>,
105 hana::curry<3>(hana::partial(
106 detail::foldlM_helper{}, static_cast<F&&>(f)
107 ))
108 )(static_cast<S&&>(s));
109 }
110
111 // without state
112 template <typename M, typename Xs, typename F>
113 static constexpr decltype(auto) apply(Xs&& xs, F&& f) {
114 struct end { };
115 using G = detail::monadic_foldl1_helper<end, M, typename detail::decay<F>::type>;
116 decltype(auto) result = hana::monadic_fold_left<M>(
117 static_cast<Xs&&>(xs),
118 end{},
119 G{static_cast<F&&>(f)}
120 );
121
122 static_assert(!std::is_same<
123 std::remove_reference_t<decltype(result)>,
124 decltype(hana::lift<M>(end{}))
125 >{},
126 "hana::monadic_fold_left<M>(xs, f) requires 'xs' to be non-empty");
127 return result;
128 }
129 };
130 BOOST_HANA_NAMESPACE_END
131
132 #endif // !BOOST_HANA_MONADIC_FOLD_LEFT_HPP