DUNE PDELab (2.8)

hybridutilities.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_COMMON_HYBRIDUTILITIES_HH
4#define DUNE_COMMON_HYBRIDUTILITIES_HH
5
6#include <tuple>
7#include <utility>
8
12#include <dune/common/indices.hh>
13#include <dune/common/assertandreturn.hh>
15
16
17
18namespace Dune {
19namespace Hybrid {
20
21namespace Impl {
22
23 // Try if tuple_size is implemented for class
24 template<class T, int i>
25 constexpr auto size(const Dune::FieldVector<T, i>&, const PriorityTag<5>&)
26 -> decltype(std::integral_constant<std::size_t,i>())
27 {
28 return {};
29 }
30
31 // Try if tuple_size is implemented for class
32 template<class T>
33 constexpr auto size(const T&, const PriorityTag<3>&)
34 -> decltype(std::integral_constant<std::size_t,std::tuple_size<T>::value>())
35 {
36 return {};
37 }
38
39 // Try if there's a static constexpr size()
40 template<class T>
41 constexpr auto size(const T&, const PriorityTag<1>&)
42 -> decltype(std::integral_constant<std::size_t,T::size()>())
43 {
44 return {};
45 }
46
47 // As a last resort try if there's a static constexpr size()
48 template<class T>
49 constexpr auto size(const T& t, const PriorityTag<0>&)
50 {
51 return t.size();
52 }
53
54} // namespace Impl
55
56
57
79template<class T>
80constexpr auto size(const T& t)
81{
82 return Impl::size(t, PriorityTag<42>());
83}
84
85
86
87namespace Impl {
88
89 template<class Container, class Index,
90 std::enable_if_t<IsTuple<std::decay_t<Container>>::value, int> = 0>
91 constexpr decltype(auto) elementAt(Container&& c, Index&&, PriorityTag<2>)
92 {
93 return std::get<std::decay_t<Index>::value>(c);
94 }
95
96 template<class T, T... t, class Index>
97 constexpr decltype(auto) elementAt(std::integer_sequence<T, t...> c, Index, PriorityTag<1>)
98 {
99 return Dune::integerSequenceEntry(c, std::integral_constant<std::size_t, Index::value>());
100 }
101
102 template<class Container, class Index>
103 constexpr decltype(auto) elementAt(Container&& c, Index&& i, PriorityTag<0>)
104 {
105 return c[i];
106 }
107
108} // namespace Impl
109
110
111
132template<class Container, class Index>
133constexpr decltype(auto) elementAt(Container&& c, Index&& i)
134{
135 return Impl::elementAt(std::forward<Container>(c), std::forward<Index>(i), PriorityTag<42>());
136}
137
138
139
140namespace Impl {
141
142 template<class Begin, class End,
143 std::enable_if_t<IsIntegralConstant<Begin>::value and IsIntegralConstant<End>::value, int> = 0>
144 constexpr auto integralRange(const Begin& /*begin*/, const End& /*end*/, const PriorityTag<1>&)
145 {
146 static_assert(Begin::value <= End::value, "You cannot create an integralRange where end<begin");
148 }
149
150 // This should be constexpr but gcc-4.9 does not support
151 // the relaxed constexpr requirements. Hence for being
152 // constexpr the function body can only contain a return
153 // statement and no assertion before this.
154 template<class Begin, class End>
155 constexpr auto integralRange(const Begin& begin, const End& end, const PriorityTag<0>&)
156 {
157 return DUNE_ASSERT_AND_RETURN(begin<=end, Dune::IntegralRange<End>(begin, end));
158 }
159
160} // namespace Impl
161
162
163
181template<class Begin, class End>
182constexpr auto integralRange(const Begin& begin, const End& end)
183{
184 return Impl::integralRange(begin, end, PriorityTag<42>());
185}
186
200template<class End>
201constexpr auto integralRange(const End& end)
202{
204}
205
206
207
208namespace Impl {
209
210 template<class T>
211 constexpr void evaluateFoldExpression(std::initializer_list<T>&&)
212 {}
213
214 template<class Range, class F, class Index, Index... i>
215 constexpr void forEachIndex(Range&& range, F&& f, std::integer_sequence<Index, i...>)
216 {
217 evaluateFoldExpression<int>({(f(Hybrid::elementAt(range, std::integral_constant<Index,i>())), 0)...});
218 }
219
220 template<class F, class Index, Index... i>
221 constexpr void forEach(std::integer_sequence<Index, i...> /*range*/, F&& f, PriorityTag<2>)
222 {
223 evaluateFoldExpression<int>({(f(std::integral_constant<Index,i>()), 0)...});
224 }
225
226
227 template<class Range, class F,
228 std::enable_if_t<IsIntegralConstant<decltype(Hybrid::size(std::declval<Range>()))>::value, int> = 0>
229 constexpr void forEach(Range&& range, F&& f, PriorityTag<1>)
230 {
231 auto size = Hybrid::size(range);
232 auto indices = std::make_index_sequence<size>();
233 (forEachIndex)(std::forward<Range>(range), std::forward<F>(f), indices);
234 }
235
236 template<class Range, class F>
237 constexpr void forEach(Range&& range, F&& f, PriorityTag<0>)
238 {
239 for(auto&& e : range)
240 f(e);
241 }
242
243} // namespace Impl
244
245
246
265template<class Range, class F>
266constexpr void forEach(Range&& range, F&& f)
267{
268 Impl::forEach(std::forward<Range>(range), std::forward<F>(f), PriorityTag<42>());
269}
270
271
272
288template<class Range, class T, class F>
289constexpr T accumulate(Range&& range, T value, F&& f)
290{
291 forEach(std::forward<Range>(range), [&](auto&& entry) {
292 value = f(value, entry);
293 });
294 return value;
295}
296
297
298
299namespace Impl {
300
301 struct Id {
302 template<class T>
303 constexpr T operator()(T&& x) const {
304 return std::forward<T>(x);
305 }
306 };
307
308 template<class IfFunc, class ElseFunc>
309 constexpr decltype(auto) ifElse(std::true_type, IfFunc&& ifFunc, ElseFunc&& /*elseFunc*/)
310 {
311 return ifFunc(Id{});
312 }
313
314 template<class IfFunc, class ElseFunc>
315 constexpr decltype(auto) ifElse(std::false_type, IfFunc&& /*ifFunc*/, ElseFunc&& elseFunc)
316 {
317 return elseFunc(Id{});
318 }
319
320 template<class IfFunc, class ElseFunc>
321 decltype(auto) ifElse(const bool& condition, IfFunc&& ifFunc, ElseFunc&& elseFunc)
322 {
323 if (condition)
324 return ifFunc(Id{});
325 else
326 return elseFunc(Id{});
327 }
328
329} // namespace Impl
330
331
332
353template<class Condition, class IfFunc, class ElseFunc>
354decltype(auto) ifElse(const Condition& condition, IfFunc&& ifFunc, ElseFunc&& elseFunc)
355{
356 return Impl::ifElse(condition, std::forward<IfFunc>(ifFunc), std::forward<ElseFunc>(elseFunc));
357}
358
366template<class Condition, class IfFunc>
367void ifElse(const Condition& condition, IfFunc&& ifFunc)
368{
369 ifElse(condition, std::forward<IfFunc>(ifFunc), [](auto&&) {});
370}
371
372
373
374namespace Impl {
375
376 template<class T1, class T2>
377 constexpr auto equals(const T1& /*t1*/, const T2& /*t2*/, PriorityTag<1>) -> decltype(T1::value, T2::value, std::integral_constant<bool,T1::value == T2::value>())
378 { return {}; }
379
380 template<class T1, class T2>
381 constexpr auto equals(const T1& t1, const T2& t2, PriorityTag<0>)
382 {
383 return t1==t2;
384 }
385
386} // namespace Impl
387
388
389
399template<class T1, class T2>
400constexpr auto equals(T1&& t1, T2&& t2)
401{
402 return Impl::equals(std::forward<T1>(t1), std::forward<T2>(t2), PriorityTag<1>());
403}
404
405
406
407namespace Impl {
408
409 template<class Result, class T, class Value, class Branches, class ElseBranch>
410 constexpr Result switchCases(std::integer_sequence<T>, const Value& /*value*/, Branches&& /*branches*/, ElseBranch&& elseBranch)
411 {
412 return elseBranch();
413 }
414
415 template<class Result, class T, T t0, T... tt, class Value, class Branches, class ElseBranch>
416 constexpr Result switchCases(std::integer_sequence<T, t0, tt...>, const Value& value, Branches&& branches, ElseBranch&& elseBranch)
417 {
418 return ifElse(
419 Hybrid::equals(std::integral_constant<T, t0>(), value),
420 [&](auto id) -> decltype(auto) {
421 return id(branches)(std::integral_constant<T, t0>());
422 }, [&](auto id) -> decltype(auto) {
423 return Impl::switchCases<Result>(id(std::integer_sequence<T, tt...>()), value, branches, elseBranch);
424 });
425 }
426
427} // namespace Impl
428
429
430
458template<class Cases, class Value, class Branches, class ElseBranch>
459constexpr decltype(auto) switchCases(const Cases& cases, const Value& value, Branches&& branches, ElseBranch&& elseBranch)
460{
461 return Impl::switchCases<decltype(elseBranch())>(cases, value, std::forward<Branches>(branches), std::forward<ElseBranch>(elseBranch));
462}
463
484template<class Cases, class Value, class Branches>
485constexpr void switchCases(const Cases& cases, const Value& value, Branches&& branches)
486{
487 Impl::switchCases<void>(cases, value, std::forward<Branches>(branches), []() {});
488}
489
490
491} // namespace Hybrid
492} // namespace Dune
493
494
495#endif // #ifndef DUNE_COMMON_HYBRIDUTILITIES_HH
vector space out of a tensor product of fields.
Definition: fvector.hh:95
dynamic integer range for use in range-based for loops
Definition: rangeutilities.hh:173
static integer range for use in range-based for loops
Definition: rangeutilities.hh:223
Traits for type conversions and type information.
Implements a vector constructed from a given type representing a field and a compile-time given size.
constexpr index_constant< 0 > _0
Compile time index with value 0.
Definition: indices.hh:51
constexpr auto integerSequenceEntry(std::integer_sequence< T, t... >, std::integral_constant< std::size_t, index > i)
Get entry of std::integer_sequence.
Definition: typetraits.hh:462
#define DUNE_ASSERT_AND_RETURN(C, X)
Asserts a condition and return on success in constexpr context.
Definition: assertandreturn.hh:20
void ifElse(const Condition &condition, IfFunc &&ifFunc)
A conditional expression.
Definition: hybridutilities.hh:367
constexpr auto size(const T &t)
Size query.
Definition: hybridutilities.hh:80
constexpr auto integralRange(const End &end)
Create an integral range starting from 0.
Definition: hybridutilities.hh:201
constexpr auto equals(T1 &&t1, T2 &&t2)
Equality comparison.
Definition: hybridutilities.hh:400
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition: hybridutilities.hh:266
constexpr void switchCases(const Cases &cases, const Value &value, Branches &&branches)
Switch statement.
Definition: hybridutilities.hh:485
constexpr decltype(auto) elementAt(Container &&c, Index &&i)
Get element at given position from container.
Definition: hybridutilities.hh:133
constexpr T accumulate(Range &&range, T value, F &&f)
Accumulate values.
Definition: hybridutilities.hh:289
Dune namespace.
Definition: alignedallocator.hh:11
Utilities for reduction like operations on ranges.
Check if T is an std::integral_constant<I, i>
Definition: typetraits.hh:384
Utilities for type computations, constraining overloads, ...
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)