]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/hana/monadic_fold_right.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / hana / monadic_fold_right.hpp
1 /*!
2 @file
3 Defines `boost::hana::monadic_fold_right`.
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_RIGHT_HPP
11 #define BOOST_HANA_MONADIC_FOLD_RIGHT_HPP
12
13 #include <boost/hana/fwd/monadic_fold_right.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_left.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_right_t {
32 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
33 static_assert(hana::Monad<M>::value,
34 "hana::monadic_fold_right<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 MonadicFoldRight = BOOST_HANA_DISPATCH_IF(monadic_fold_right_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_right<M>(xs, state, f) requires 'xs' to be Foldable");
47 #endif
48
49 return MonadicFoldRight::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 MonadicFoldRight = BOOST_HANA_DISPATCH_IF(monadic_fold_right_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_right<M>(xs, f) requires 'xs' to be Foldable");
64 #endif
65 return MonadicFoldRight::template apply<M>(static_cast<Xs&&>(xs),
66 static_cast<F&&>(f));
67 }
68 };
69
70 namespace detail {
71 struct foldrM_helper {
72 template <typename F, typename K, typename X, typename Z>
73 constexpr decltype(auto) operator()(F&& f, K&& k, X&& x, Z&& z) const {
74 return hana::chain(
75 static_cast<F&&>(f)(
76 static_cast<X&&>(x),
77 static_cast<Z&&>(z)
78 ),
79 static_cast<K&&>(k)
80 );
81 }
82 };
83
84 template <typename End, typename M, typename F>
85 struct monadic_foldr1_helper {
86 F f;
87 template <typename X, typename Y>
88 constexpr decltype(auto) operator()(X&& x, Y&& y) const
89 { return f(static_cast<X&&>(x), static_cast<Y&&>(y)); }
90 template <typename X>
91 constexpr decltype(auto) operator()(X&& x, End) const
92 { return hana::lift<M>(static_cast<X&&>(x)); }
93 };
94 }
95
96 template <typename T, bool condition>
97 struct monadic_fold_right_impl<T, when<condition>> : default_ {
98 // with state
99 template <typename M, typename Xs, typename S, typename F>
100 static constexpr decltype(auto) apply(Xs&& xs, S&& s, F&& f) {
101 return hana::fold_left(
102 static_cast<Xs&&>(xs),
103 hana::lift<M>,
104 hana::curry<3>(hana::partial(
105 detail::foldrM_helper{}, static_cast<F&&>(f)
106 ))
107 )(static_cast<S&&>(s));
108 }
109
110 // without state
111 template <typename M, typename Xs, typename F>
112 static constexpr decltype(auto) apply(Xs&& xs, F&& f) {
113 struct end { };
114 using G = detail::monadic_foldr1_helper<end, M, typename detail::decay<F>::type>;
115 decltype(auto) result = hana::monadic_fold_right<M>(
116 static_cast<Xs&&>(xs),
117 end{},
118 G{static_cast<F&&>(f)}
119 );
120
121 static_assert(!std::is_same<
122 std::remove_reference_t<decltype(result)>,
123 decltype(hana::lift<M>(end{}))
124 >{},
125 "hana::monadic_fold_right<M>(xs, f) requires 'xs' to be non-empty");
126 return result;
127 }
128 };
129 BOOST_HANA_NAMESPACE_END
130
131 #endif // !BOOST_HANA_MONADIC_FOLD_RIGHT_HPP