pRC
multi-purpose Tensor Train library for C++
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
34 template<class F, Bool E = false, Direction D = Direction::Forwards,
37 class XA, If<IsInvocable<F, XA, XA>> = 0>
38 static inline constexpr Conditional<IsSatisfied<E>,
39 decltype(eval(declval<XA>())), XA>
40 fold(XA &&a)
41 {
42 if constexpr(E)
43 {
44 return eval(forward<XA>(a));
45 }
46 else
47 {
48 return forward<XA>(a);
49 }
50 }
51
74 template<class F, Bool E = false, Direction D = Direction::Forwards,
77 class XA, class XB, If<IsInvocable<F, XA, XB>> = 0>
78 static inline constexpr auto fold(XA &&a, XB &&b)
79 {
80 if constexpr(E)
81 {
82 return eval(F()(forward<XA>(a), forward<XB>(b)));
83 }
84 else
85 {
86 return F()(forward<XA>(a), forward<XB>(b));
87 }
88 }
89
120 template<class F, Bool E = false, Direction D = Direction::Forwards,
121 class XA, class XB, class... Xs,
122 If<IsSatisfied<(sizeof...(Xs) > 0)>> = 0,
127 Xs...>> = 0>
128 static inline constexpr auto fold(XA &&a, XB &&b, Xs &&...args)
129 {
130 if constexpr(D == Direction::Forwards)
131 {
132 auto const c = [&a, &b]()
133 {
134 if constexpr(E)
135 {
136 return eval(F()(forward<XA>(a), forward<XB>(b)));
137 }
138 else
139 {
140 return F()(forward<XA>(a), forward<XB>(b));
141 }
142 };
143
144 return fold<F, E, D>(c(), forward<Xs>(args)...);
145 }
146 if constexpr(D == Direction::Backwards)
147 {
148 auto const c = [&b, &args...]()
149 {
151 };
152
153 if constexpr(E)
154 {
155 return eval(F()(forward<XA>(a), c()));
156 }
157 else
158 {
159 return F()(forward<XA>(a), c());
160 }
161 }
162 }
163}
164#endif // pRC_CORE_VALUE_FUNCTIONS_FOLD_H
Definition cholesky.hpp:18
static constexpr X eval(X &&a)
Definition eval.hpp:11
bool Bool
Definition type_traits.hpp:18
std::invoke_result_t< F, Args... > ResultOf
Definition type_traits.hpp:140
std::enable_if_t< B{}, int > If
Definition type_traits.hpp:68
std::is_invocable< F, Args... > IsInvocable
Definition type_traits.hpp:134
static constexpr Conditional< IsSatisfied< E >, decltype(eval(declval< XA >())), XA > fold(XA &&a)
Repeatedly applies an operation to generate one pRC object out of many.
Definition fold.hpp:40
Constant< Bool, B > IsSatisfied
Definition type_traits.hpp:71
static constexpr Conditional< IsSatisfied< C >, RemoveConstReference< X >, X > copy(X &&a)
Definition copy.hpp:13
Direction
Definition direction.hpp:9
std::conditional_t< B{}, T, F > Conditional
Definition type_traits.hpp:131