3#ifndef DUNE_COMMON_HYBRIDUTILITIES_HH
4#define DUNE_COMMON_HYBRIDUTILITIES_HH
11#include <dune/common/indices.hh>
21 template<
class T,
int i>
23 ->
decltype(std::integral_constant<std::size_t,i>())
29 template<
class T, T... t,
class Index>
30 constexpr auto size(std::integer_sequence<T, t...>, PriorityTag<4>)
32 using sizeAsType = std::tuple_size<
decltype(std::make_tuple(t...))>;
33 return std::integral_constant<std::size_t, sizeAsType::value>();
38 constexpr auto size(
const T*,
const PriorityTag<3>&)
39 ->
decltype(std::integral_constant<std::size_t,std::tuple_size<T>::value>())
46 constexpr auto size(
const T*,
const PriorityTag<1>&)
47 ->
decltype(std::integral_constant<std::size_t,T::size()>())
54 constexpr auto size(
const T* t,
const PriorityTag<0>&)
85constexpr auto size(
const T& t)
94 template<
class Container,
class Index,
95 std::enable_if_t<IsTuple<std::decay_t<Container>>::value,
int> = 0>
98 return std::get<std::decay_t<Index>::value>(c);
101 template<
class T, T... t,
class Index>
102 constexpr decltype(
auto)
elementAt(std::integer_sequence<T, t...> c, Index&&, PriorityTag<1>)
104 return std::get<std::decay_t<Index>::value>(std::make_tuple(std::integral_constant<T, t>()...));
107 template<
class Container,
class Index>
108 constexpr decltype(
auto)
elementAt(Container&& c, Index&& i, PriorityTag<0>)
137template<
class Container,
class Index>
138constexpr decltype(
auto)
elementAt(Container&& c, Index&& i)
147 template<
class Begin,
class End>
148 class StaticIntegralRange
152 template<std::
size_t i>
155 return std::integral_constant<typename Begin::value_type, Begin::value+i>();
158 static constexpr auto size()
160 return std::integral_constant<
typename Begin::value_type, End::value - Begin::value>();
165 class DynamicIntegralRange
168 constexpr DynamicIntegralRange(
const T& begin,
const T& end):
173 constexpr auto size()
const
175 return end_ - begin_;
178 constexpr T operator[](
const T&i)
const
186 template<
class Begin,
class End,
187 std::enable_if_t<IsIntegralConstant<Begin>::value and IsIntegralConstant<End>::value,
int> = 0>
188 constexpr auto integralRange(
const Begin& begin,
const End& end,
const PriorityTag<1>&)
190 static_assert(Begin::value <= End::value,
"You cannot create an integralRange where end<begin");
191 return Impl::StaticIntegralRange<Begin,End>();
198 template<
class Begin,
class End>
199 auto integralRange(
const Begin& begin,
const End& end,
const PriorityTag<0>&)
201 assert(begin <= end);
202 return Impl::DynamicIntegralRange<End>(begin, end);
226template<
class Begin,
class End>
256 void evaluateFoldExpression(std::initializer_list<T>&&)
259 template<
class Range,
class F,
class Index, Index... i>
260 constexpr void forEachIndex(Range&& range, F&& f, std::integer_sequence<Index, i...>)
262 evaluateFoldExpression<int>({(f(
Hybrid::elementAt(range, std::integral_constant<Index,i>())), 0)...});
265 template<
class F,
class Index, Index... i>
266 constexpr void forEach(std::integer_sequence<Index, i...> range, F&& f, PriorityTag<2>)
268 evaluateFoldExpression<int>({(f(std::integral_constant<Index,i>()), 0)...});
272 template<
class Range,
class F,
273 std::enable_if_t<IsIntegralConstant<decltype(Hybrid::size(std::declval<Range>()))>::value,
int> = 0>
274 constexpr void forEach(Range&& range, F&& f, PriorityTag<1>)
277 auto indices = std::make_index_sequence<size>();
278 forEachIndex(std::forward<Range>(range), std::forward<F>(f), indices);
281 template<
class Range,
class F>
282 constexpr void forEach(Range&& range, F&& f, PriorityTag<0>)
284 for(std::size_t i=0; i<range.size(); ++i)
313template<
class Range,
class F>
336template<
class Range,
class T,
class F>
339 forEach(std::forward<Range>(range), [&](
auto&& entry) {
340 value = f(value, entry);
349 template<
class IfFunc,
class ElseFunc>
350 constexpr decltype(
auto)
ifElse(std::true_type, IfFunc&& ifFunc, ElseFunc&& elseFunc)
352 return ifFunc([](
auto&& x) ->
decltype(
auto) {
return std::forward<decltype(x)>(x);});
355 template<
class IfFunc,
class ElseFunc>
356 constexpr decltype(
auto)
ifElse(std::false_type, IfFunc&& ifFunc, ElseFunc&& elseFunc)
358 return elseFunc([](
auto&& x) ->
decltype(
auto) {
return std::forward<decltype(x)>(x);});
361 template<
class IfFunc,
class ElseFunc>
362 decltype(
auto)
ifElse(
const bool& condition, IfFunc&& ifFunc, ElseFunc&& elseFunc)
365 return ifFunc([](
auto&& x) ->
decltype(
auto) {
return std::forward<decltype(x)>(x);});
367 return elseFunc([](
auto&& x) ->
decltype(
auto) {
return std::forward<decltype(x)>(x);});
394template<
class Condition,
class IfFunc,
class ElseFunc>
395decltype(
auto)
ifElse(
const Condition& condition, IfFunc&& ifFunc, ElseFunc&& elseFunc)
397 return Impl::ifElse(condition, std::forward<IfFunc>(ifFunc), std::forward<ElseFunc>(elseFunc));
407template<
class Condition,
class IfFunc>
408void ifElse(
const Condition& condition, IfFunc&& ifFunc)
410 ifElse(condition, std::forward<IfFunc>(ifFunc), [](
auto&& i) {});
417 template<
class T1,
class T2>
418 constexpr auto equals(
const T1& t1,
const T2& t2,
PriorityTag<1>) ->
decltype(T1::value, T2::value, std::integral_constant<bool,T1::value == T2::value>())
421 template<
class T1,
class T2>
422 constexpr auto equals(
const T1& t1,
const T2& t2, PriorityTag<0>)
440template<
class T1,
class T2>
450 template<
class Result,
class T,
class Value,
class Branches,
class ElseBranch>
451 constexpr Result
switchCases(std::integer_sequence<T>,
const Value& value, Branches&& branches, ElseBranch&& elseBranch)
456 template<
class Result,
class T, T t0, T... tt,
class Value,
class Branches,
class ElseBranch>
457 constexpr Result
switchCases(std::integer_sequence<T, t0, tt...>,
const Value& value, Branches&& branches, ElseBranch&& elseBranch)
461 [&](
auto id) ->
decltype(
auto) {
462 return id(branches)(std::integral_constant<T, t0>());
463 }, [&](
auto id) ->
decltype(
auto) {
464 return Impl::switchCases<Result>(
id(std::integer_sequence<T, tt...>()), value, branches, elseBranch);
499template<
class Cases,
class Value,
class Branches,
class ElseBranch>
500constexpr decltype(
auto)
switchCases(
const Cases& cases,
const Value& value, Branches&& branches, ElseBranch&& elseBranch)
502 return Impl::switchCases<decltype(elseBranch())>(cases, value, std::forward<Branches>(branches), std::forward<ElseBranch>(elseBranch));
525template<
class Cases,
class Value,
class Branches>
526constexpr void switchCases(
const Cases& cases,
const Value& value, Branches&& branches)
528 return Impl::switchCases<void>(cases, value, std::forward<Branches>(branches), []() {});
vector space out of a tensor product of fields.
Definition: fvector.hh:93
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:49
std::integral_constant< std::size_t, i > index_constant
An index constant with value i.
Definition: indices.hh:26
void ifElse(const Condition &condition, IfFunc &&ifFunc)
A conditional expression.
Definition: hybridutilities.hh:408
constexpr auto size(const T &t)
Size query.
Definition: hybridutilities.hh:85
constexpr auto integralRange(const End &end)
Create an integral range starting from 0.
Definition: hybridutilities.hh:246
constexpr auto equals(T1 &&t1, T2 &&t2)
Equality comparison.
Definition: hybridutilities.hh:441
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition: hybridutilities.hh:314
T accumulate(Range &&range, T value, F &&f)
Accumulate values.
Definition: hybridutilities.hh:337
constexpr void switchCases(const Cases &cases, const Value &value, Branches &&branches)
Switch statement.
Definition: hybridutilities.hh:526
constexpr decltype(auto) elementAt(Container &&c, Index &&i)
Get element at given position from container.
Definition: hybridutilities.hh:138
Dune namespace.
Definition: alignment.hh:11
Helper class for tagging priorities.
Definition: typeutilities.hh:60
Utilities for type computations, constraining overloads, ...