cMHN 1.1
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
fold.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_CORE_VALUE_FUNCTIONS_FOLD_H
4#define pRC_CORE_VALUE_FUNCTIONS_FOLD_H
5
8
9namespace pRC
10{
11 template<class F, Bool E = false, Direction D = Direction::Forwards>
12 struct Fold;
13
14 template<class F, Bool E = false, Direction D = Direction::Forwards,
17 class XA, If<IsInvocable<F, XA, XA>> = 0>
18 static inline constexpr Conditional<IsSatisfied<E>,
19 decltype(eval(declval<XA>())), XA>
20 fold(XA &&a)
21 {
22 if constexpr(E)
23 {
24 return eval(forward<XA>(a));
25 }
26 else
27 {
28 return forward<XA>(a);
29 }
30 }
31
32 template<class F, Bool E = false, Direction D = Direction::Forwards,
35 class XA, class XB, If<IsInvocable<F, XA, XB>> = 0>
36 static inline constexpr auto fold(XA &&a, XB &&b)
37 {
38 if constexpr(E)
39 {
40 return eval(F()(forward<XA>(a), forward<XB>(b)));
41 }
42 else
43 {
44 return F()(forward<XA>(a), forward<XB>(b));
45 }
46 }
47
48 template<class F, Bool E = false, Direction D = Direction::Forwards,
49 class XA, class XB, class... Xs,
50 If<IsSatisfied<(sizeof...(Xs) > 0)>> = 0,
55 Xs...>> = 0>
56 static inline constexpr auto fold(XA &&a, XB &&b, Xs &&...args)
57 {
58 if constexpr(D == Direction::Forwards)
59 {
60 auto const c = [&a, &b]()
61 {
62 if constexpr(E)
63 {
64 return eval(F()(forward<XA>(a), forward<XB>(b)));
65 }
66 else
67 {
68 return F()(forward<XA>(a), forward<XB>(b));
69 }
70 };
71
72 return fold<F, E, D>(c(), forward<Xs>(args)...);
73 }
74 if constexpr(D == Direction::Backwards)
75 {
76 auto const c = [&b, &args...]()
77 {
78 return fold<F, E, D>(forward<XB>(b), forward<Xs>(args)...);
79 };
80
81 if constexpr(E)
82 {
83 return eval(F()(forward<XA>(a), c()));
84 }
85 else
86 {
87 return F()(forward<XA>(a), c());
88 }
89 }
90 }
91}
92#endif // pRC_CORE_VALUE_FUNCTIONS_FOLD_H
pRC::Size const D
Definition CalculatePThetaTests.cpp:9
Definition cholesky.hpp:18
static constexpr X eval(X &&a)
Definition eval.hpp:11
bool Bool
Definition type_traits.hpp:18
static constexpr auto makeConstantSequence()
Definition sequence.hpp:402
std::invoke_result_t< F, Args... > ResultOf
Definition type_traits.hpp:140
std::enable_if_t< B{}, int > If
Definition type_traits.hpp:68
Constant< Bool, B > IsSatisfied
Definition type_traits.hpp:71
static constexpr Conditional< IsSatisfied< E >, decltype(eval(declval< XA >())), XA > fold(XA &&a)
Definition fold.hpp:20
Direction
Definition direction.hpp:9
std::is_invocable< F, Args... > IsInvocable
Definition type_traits.hpp:134