DUNE-ACFEM (unstable)

accumulate.hh
1#ifndef __DUNE_ACFEM_MPL_ACCUMULATE_HH__
2#define __DUNE_ACFEM_MPL_ACCUMULATE_HH__
3
4#include "transform.hh"
5
6namespace Dune {
7
8 namespace ACFem {
9
30 namespace {
31 template<class Sequence, class Functor>
32 struct AccumulateSequenceHelper;
33
34 template<class T, T I, T... Rest, class F>
35 struct AccumulateSequenceHelper<Sequence<T, I, Rest...>, F>
36 {
37 static const T value =
38 F::template Apply<T, I, AccumulateSequenceHelper<Sequence<T, Rest...>, F>::value>::value;
39 };
40
41 template<class T, class F>
42 struct AccumulateSequenceHelper<Sequence<T>, F>
43 {
44 static constexpr T value = F::template NeutralValue<T>::value;
45 };
46 }
47
72 template<class F, class Seq>
74
81 struct LogicalAndFunctor
82 {
83 template<class T>
84 using NeutralValue = Constant<T, true>;
85
86 template<class T, T I1, T I2>
87 using Apply = BoolConstant<I1 && I2>;
88 };
89
90 struct LogicalOrFunctor
91 {
92 template<class T>
93 using NeutralValue = Constant<T, false>;
94
95 template<class T, T I1, T I2>
96 using Apply = BoolConstant<I1 || I2>;
97 };
98
99 struct BitwiseAndFunctor
100 {
102 template<class T>
103 using NeutralValue = Constant<T, T(~0UL)>;
104
105 template<class T, T I1, T I2>
106 using Apply = Constant<T, I1 & I2>;
107 };
108
109 struct BitwiseOrFunctor
110 {
112 template<class T>
113 using NeutralValue = Constant<T, T(0UL)>;
114
115 template<class T, T I1, T I2>
116 using Apply = Constant<T, I1 | I2>;
117 };
118
119 struct MinFunctor
120 {
121 template<class T>
122 using NeutralValue = Constant<T, std::numeric_limits<T>::max()>;
123
124 template<class T, T I1, T I2>
125 using Apply = Constant<T, std::min(I1, I2)>;
126 };
127
128 struct MaxFunctor
129 {
130 template<class T>
131 using NeutralValue = Constant<T, std::numeric_limits<T>::min()>;
132
133 template<class T, T I1, T I2>
134 using Apply = Constant<T, std::max(I1, I2)>;
135 };
136
137 struct AddFunctor
138 {
139 template<class T>
140 using NeutralValue = Constant<T, T(0)>;
141
142 template<class T, T I1, T I2>
143 using Apply = Constant<T, I1 + I2>;
144 };
145
146 struct ProdFunctor
147 {
148 template<class T>
149 using NeutralValue = Constant<T, T(1)>;
150
151 template<class T, T I1, T I2>
152 using Apply = Constant<T, I1 * I2>;
153 };
154
155 template<class F, F V>
156 struct MultiplyAddFunctor
157 {
158 template<class T>
159 using NeutralValue = Constant<T, T(0)>;
160
161 template<class T, T Front, T Rest>
162 using Apply = Constant<T, Front + V * Rest>;
163 };
164
172 template<class T, T... Ts>
173 constexpr bool logicalAnd(Sequence<T, Ts...>)
174 {
175 return (... && ((bool)Ts));
176 }
177
178 template<class T, T... Ts>
179 constexpr bool logicalOr(Sequence<T, Ts...>)
180 {
181 return (... || ((bool)Ts));
182 }
183
184 template<class T, T... Ts>
185 constexpr T bitwiseAnd(Sequence<T, Ts...>)
186 {
187 return (T(~0UL) & ... & ((bool)Ts));
188 }
189
190 template<class T, T... Ts>
191 constexpr T bitwiseOr(Sequence<T, Ts...>)
192 {
193 return (T(0) | ... | ((bool)Ts));
194 }
195
196 template<class T, T... Ts>
197 constexpr T sum(Sequence<T, Ts...>)
198 {
199 return (T(0) + ... + Ts);
200 }
201
202 template<class T, T... Ts>
203 constexpr T prod(Sequence<T, Ts...>)
204 {
205 return (T(1) * ... * Ts);
206 }
207
208 template<class T, T... Ts>
209 constexpr T max(Sequence<T, Ts...>)
210 {
211 return AccumulateSequence<MaxFunctor, Sequence<T, Ts...> >::value;
212 }
213
214 template<class T, T... Ts>
215 constexpr T min(Sequence<T, Ts...>)
216 {
217 return AccumulateSequence<MinFunctor, Sequence<T, Ts...> >::value;
218 }
219
220 template<class Seq>
221 using LogicalAnd = BoolConstant<logicalAnd(Seq{})>;
222
223 template<class Seq>
224 using LogicalOr = BoolConstant<logicalOr(Seq{})>;
225
226 template<class Seq>
227 using BitwiseAnd = Constant<typename Seq::value_type, bitwiseAnd(Seq{})>;
228
229 template<class Seq>
230 using BitwiseOr = Constant<typename Seq::value_type, bitwiseOr(Seq{})>;
231
232 template<class Seq>
233 using Sum = Constant<typename Seq::value_type, sum(Seq{})>;
234
235 template<class Seq>
236 using Prod = Constant<typename Seq::value_type, prod(Seq{})>;
237
238 template<class Seq>
239 using Min = Constant<typename Seq::value_type, min(Seq{})>;
240
241 template<class Seq>
242 using Max = Constant<typename Seq::value_type, max(Seq{})>;
243
245
247
249
251
253
254 } // ACFem::
255
256} // Dune::
257
258#endif // __DUNE_ACFEM_MPL_ACCUMULATE_HH__
Constant< typename Seq::value_type, AccumulateSequenceHelper< Seq, F >::value > AccumulateSequence
Accumulate the values of the sequence according to the supplied functor.
Definition: accumulate.hh:73
integral_constant< T, V > Constant
Short-cut for any integral constant.
Definition: types.hh:40
Constant< bool, V > BoolConstant
Short-cut for integral constant of type bool.
Definition: types.hh:48
std::integer_sequence< T, V... > Sequence
Sequence of any type of integer values.
Definition: types.hh:56
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Sep 4, 22:38, 2025)