3#ifndef DUNE_COMMON_HYBRIDUTILITIES_HH
4#define DUNE_COMMON_HYBRIDUTILITIES_HH
12#include <dune/common/indices.hh>
13#include <dune/common/assertandreturn.hh>
24 template<
class T,
int i>
26 ->
decltype(std::integral_constant<std::size_t,i>())
33 constexpr auto size(
const T&,
const PriorityTag<3>&)
34 ->
decltype(std::integral_constant<std::size_t,std::tuple_size<T>::value>())
41 constexpr auto size(
const T&,
const PriorityTag<1>&)
42 ->
decltype(std::integral_constant<std::size_t,T::size()>())
49 constexpr auto size(
const T& t,
const PriorityTag<0>&)
80constexpr auto size(
const T& t)
89 template<
class Container,
class Index,
90 std::enable_if_t<IsTuple<std::decay_t<Container>>::value,
int> = 0>
93 return std::get<std::decay_t<Index>::value>(c);
96 template<
class T, T... t,
class Index>
97 constexpr decltype(
auto)
elementAt(std::integer_sequence<T, t...> c, Index, PriorityTag<1>)
102 template<
class Container,
class Index>
103 constexpr decltype(
auto)
elementAt(Container&& c, Index&& i, PriorityTag<0>)
132template<
class Container,
class Index>
133constexpr decltype(
auto)
elementAt(Container&& c, Index&& i)
142 template<
class Begin,
class End>
147 template<std::
size_t i>
150 return std::integral_constant<typename Begin::value_type, Begin::value+i>();
153 static constexpr auto size()
155 return std::integral_constant<
typename Begin::value_type, End::value - Begin::value>();
160 class DynamicIntegralRange
163 constexpr DynamicIntegralRange(
const T& begin,
const T& end):
168 constexpr auto size()
const
170 return end_ - begin_;
173 constexpr T operator[](
const T&i)
const
181 template<
class Begin,
class End,
182 std::enable_if_t<IsIntegralConstant<Begin>::value and IsIntegralConstant<End>::value,
int> = 0>
183 constexpr auto integralRange(
const Begin& ,
const End& ,
const PriorityTag<1>&)
185 static_assert(Begin::value <= End::value,
"You cannot create an integralRange where end<begin");
186 return Impl::StaticIntegralRange<Begin,End>();
193 template<
class Begin,
class End>
194 constexpr auto integralRange(
const Begin& begin,
const End& end,
const PriorityTag<0>&)
220template<
class Begin,
class End>
250 void evaluateFoldExpression(std::initializer_list<T>&&)
253 template<
class Range,
class F,
class Index, Index... i>
254 constexpr void forEachIndex(Range&& range, F&& f, std::integer_sequence<Index, i...>)
256 evaluateFoldExpression<int>({(f(
Hybrid::elementAt(range, std::integral_constant<Index,i>())), 0)...});
259 template<
class F,
class Index, Index... i>
260 constexpr void forEach(std::integer_sequence<Index, i...> , F&& f, PriorityTag<2>)
262 evaluateFoldExpression<int>({(f(std::integral_constant<Index,i>()), 0)...});
266 template<
class Range,
class F,
267 std::enable_if_t<IsIntegralConstant<decltype(Hybrid::size(std::declval<Range>()))>::value,
int> = 0>
268 constexpr void forEach(Range&& range, F&& f, PriorityTag<1>)
271 auto indices = std::make_index_sequence<size>();
272 (forEachIndex)(std::forward<Range>(range), std::forward<F>(f), indices);
275 template<
class Range,
class F>
276 constexpr void forEach(Range&& range, F&& f, PriorityTag<0>)
278 for(std::size_t i=0; i<range.size(); ++i)
307template<
class Range,
class F>
330template<
class Range,
class T,
class F>
333 forEach(std::forward<Range>(range), [&](
auto&& entry) {
334 value = f(value, entry);
343 template<
class IfFunc,
class ElseFunc>
344 constexpr decltype(
auto)
ifElse(std::true_type, IfFunc&& ifFunc, ElseFunc&& )
346 return ifFunc([](
auto&& x) ->
decltype(
auto) {
return std::forward<decltype(x)>(x);});
349 template<
class IfFunc,
class ElseFunc>
350 constexpr decltype(
auto)
ifElse(std::false_type, IfFunc&& , ElseFunc&& elseFunc)
352 return elseFunc([](
auto&& x) ->
decltype(
auto) {
return std::forward<decltype(x)>(x);});
355 template<
class IfFunc,
class ElseFunc>
356 decltype(
auto)
ifElse(
const bool& condition, IfFunc&& ifFunc, ElseFunc&& elseFunc)
359 return ifFunc([](
auto&& x) ->
decltype(
auto) {
return std::forward<decltype(x)>(x);});
361 return elseFunc([](
auto&& x) ->
decltype(
auto) {
return std::forward<decltype(x)>(x);});
388template<
class Condition,
class IfFunc,
class ElseFunc>
389decltype(
auto)
ifElse(
const Condition& condition, IfFunc&& ifFunc, ElseFunc&& elseFunc)
391 return Impl::ifElse(condition, std::forward<IfFunc>(ifFunc), std::forward<ElseFunc>(elseFunc));
401template<
class Condition,
class IfFunc>
402void ifElse(
const Condition& condition, IfFunc&& ifFunc)
411 template<
class T1,
class T2>
412 constexpr auto equals(
const T1& ,
const T2& ,
PriorityTag<1>) ->
decltype(T1::value, T2::value, std::integral_constant<bool,T1::value == T2::value>())
415 template<
class T1,
class T2>
416 constexpr auto equals(
const T1& t1,
const T2& t2, PriorityTag<0>)
434template<
class T1,
class T2>
444 template<
class Result,
class T,
class Value,
class Branches,
class ElseBranch>
445 constexpr Result
switchCases(std::integer_sequence<T>,
const Value& , Branches&& , ElseBranch&& elseBranch)
450 template<
class Result,
class T, T t0, T... tt,
class Value,
class Branches,
class ElseBranch>
451 constexpr Result
switchCases(std::integer_sequence<T, t0, tt...>,
const Value& value, Branches&& branches, ElseBranch&& elseBranch)
455 [&](
auto id) ->
decltype(
auto) {
456 return id(branches)(std::integral_constant<T, t0>());
457 }, [&](
auto id) ->
decltype(
auto) {
458 return Impl::switchCases<Result>(
id(std::integer_sequence<T, tt...>()), value, branches, elseBranch);
493template<
class Cases,
class Value,
class Branches,
class ElseBranch>
494constexpr decltype(
auto)
switchCases(
const Cases& cases,
const Value& value, Branches&& branches, ElseBranch&& elseBranch)
496 return Impl::switchCases<decltype(elseBranch())>(cases, value, std::forward<Branches>(branches), std::forward<ElseBranch>(elseBranch));
519template<
class Cases,
class Value,
class Branches>
520constexpr void switchCases(
const Cases& cases,
const Value& value, Branches&& branches)
522 return Impl::switchCases<void>(cases, value, std::forward<Branches>(branches), []() {});
vector space out of a tensor product of fields.
Definition: fvector.hh:93
static integer range for use in range-based for loops
Definition: rangeutilities.hh:221
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:50
std::integral_constant< std::size_t, i > index_constant
An index constant with value i.
Definition: indices.hh:27
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:518
#define DUNE_ASSERT_AND_RETURN(C, X)
Asserts a condition and return on success in constexpr context.
Definition: assertandreturn.hh:20
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentionally unused function parameters with.
Definition: unused.hh:25
void ifElse(const Condition &condition, IfFunc &&ifFunc)
A conditional expression.
Definition: hybridutilities.hh:402
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:240
constexpr auto equals(T1 &&t1, T2 &&t2)
Equality comparison.
Definition: hybridutilities.hh:435
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition: hybridutilities.hh:308
T accumulate(Range &&range, T value, F &&f)
Accumulate values.
Definition: hybridutilities.hh:331
constexpr void switchCases(const Cases &cases, const Value &value, Branches &&branches)
Switch statement.
Definition: hybridutilities.hh:520
constexpr decltype(auto) elementAt(Container &&c, Index &&i)
Get element at given position from container.
Definition: hybridutilities.hh:133
Dune namespace.
Definition: alignedallocator.hh:10
Helper class for tagging priorities.
Definition: typeutilities.hh:71
Traits for type conversions and type information.
Utilities for type computations, constraining overloads, ...
Definition of the DUNE_UNUSED macro for the case that config.h is not available.