5#ifndef DUNE_COMMON_HYBRIDUTILITIES_HH
6#define DUNE_COMMON_HYBRIDUTILITIES_HH
14#include <dune/common/indices.hh>
26 constexpr auto size(
const T&,
const PriorityTag<2>&)
27 ->
decltype(std::integral_constant<std::size_t,std::tuple_size<T>::value>())
34 constexpr auto size(
const T&,
const PriorityTag<1>&)
35 ->
decltype(std::integral_constant<std::size_t,T::size()>())
42 constexpr auto size(
const T& t,
const PriorityTag<0>&)
73constexpr auto size(
const T& t)
82 template<
class Container,
class Index,
83 std::enable_if_t<IsTuple<std::decay_t<Container>>::value,
int> = 0>
86 return std::get<std::decay_t<Index>::value>(c);
89 template<
class T, T... t,
class Index>
90 constexpr decltype(
auto)
elementAt(std::integer_sequence<T, t...> c, Index, PriorityTag<1>)
95 template<
class Container,
class Index>
96 constexpr decltype(
auto)
elementAt(Container&& c, Index&& i, PriorityTag<0>)
125template<
class Container,
class Index>
126constexpr decltype(
auto)
elementAt(Container&& c, Index&& i)
135 template<
class Begin,
class End,
139 static_assert(Begin::value <= End::value,
"You cannot create an integralRange where end<begin");
143 template<
class Begin,
class End>
144 constexpr auto integralRange(
const Begin& begin,
const End& end,
const PriorityTag<0>&)
146 assert(begin<=end &&
"You cannot create an integralRange where end<begin");
171template<
class Begin,
class End>
201 constexpr void evaluateFoldExpression(std::initializer_list<T>&&)
204 template<
class Range,
class F,
class Index, Index... i>
205 constexpr void forEachIndex(Range&& range, F&& f, std::integer_sequence<Index, i...>)
207 evaluateFoldExpression<int>({(f(
Hybrid::elementAt(range, std::integral_constant<Index,i>())), 0)...});
210 template<
class F,
class Index, Index... i>
211 constexpr void forEach(std::integer_sequence<Index, i...> , F&& f, PriorityTag<2>)
213 evaluateFoldExpression<int>({(f(std::integral_constant<Index,i>()), 0)...});
217 template<
class Range,
class F,
218 std::enable_if_t<IsIntegralConstant<decltype(Hybrid::size(std::declval<Range>()))>::value,
int> = 0>
219 constexpr void forEach(Range&& range, F&& f, PriorityTag<1>)
222 auto indices = std::make_index_sequence<size>();
223 (forEachIndex)(std::forward<Range>(range), std::forward<F>(f), indices);
226 template<
class Range,
class F>
227 constexpr void forEach(Range&& range, F&& f, PriorityTag<0>)
229 for(
auto&& e : range)
255template<
class Range,
class F>
278template<
class Range,
class T,
class F>
281 forEach(std::forward<Range>(range), [&](
auto&& entry) {
282 value = f(value, entry);
293 constexpr T operator()(T&& x)
const {
294 return std::forward<T>(x);
298 template<
class IfFunc,
class ElseFunc>
299 constexpr decltype(
auto)
ifElse(std::true_type, IfFunc&& ifFunc, ElseFunc&& )
304 template<
class IfFunc,
class ElseFunc>
305 constexpr decltype(
auto)
ifElse(std::false_type, IfFunc&& , ElseFunc&& elseFunc)
307 return elseFunc(Id{});
310 template<
class IfFunc,
class ElseFunc>
311 decltype(
auto)
ifElse(
const bool& condition, IfFunc&& ifFunc, ElseFunc&& elseFunc)
316 return elseFunc(Id{});
343template<
class Condition,
class IfFunc,
class ElseFunc>
344decltype(
auto)
ifElse(
const Condition& condition, IfFunc&& ifFunc, ElseFunc&& elseFunc)
346 return Impl::ifElse(condition, std::forward<IfFunc>(ifFunc), std::forward<ElseFunc>(elseFunc));
356template<
class Condition,
class IfFunc>
357void ifElse(
const Condition& condition, IfFunc&& ifFunc)
359 ifElse(condition, std::forward<IfFunc>(ifFunc), [](
auto&&) {});
367 template<
class... Args>
368 constexpr decltype(
auto)
operator()(Args&&... args)
const
370 using T = std::common_type_t<Args...>;
376 template<
class... Args>
377 constexpr decltype(
auto)
operator()(Args&&... args)
const
379 using T = std::common_type_t<Args...>;
417template<
class Functor>
420 static_assert(std::is_default_constructible_v<Functor>,
421 "Operator in integral expressions shall be constexpr default constructible");
423 inline static constexpr Functor _functor = Functor{};
436 template<
class... Args>
437 constexpr decltype(
auto)
operator()(
const Args&... args)
const
439 if constexpr (std::conjunction_v<IsCompileTimeConstant<Args>...>)
441 constexpr auto result = _functor(Args::value...);
444 return std::integral_constant<std::remove_cv_t<
decltype(result)>,result>{};
449 return _functor(args...);
458template<
class Functor>
459constexpr HybridFunctor<Functor> hybridFunctor(
const Functor&)
484inline constexpr auto max = hybridFunctor(Impl::Max{});
506inline constexpr auto min = hybridFunctor(Impl::Min{});
528inline constexpr auto plus = hybridFunctor(std::plus<>{});
550inline constexpr auto minus = hybridFunctor(std::minus<>{});
572inline constexpr auto equal_to = hybridFunctor(std::equal_to<>{});
586template<
class T1,
class T2>
587[[deprecated(
"Use Hybrid::equal_to instead!")]]
constexpr auto equals(T1&& t1, T2&& t2){
588 return equal_to(std::forward<T1>(t1), std::forward<T2>(t2));
595 template<
class Result,
class T, T t0, T... tt,
class ValueType, ValueType value,
class Branches,
class ElseBranch>
596 constexpr Result
switchCases(std::integer_sequence<T, t0, tt...>,
const std::integral_constant<ValueType, value>& , Branches&& branches, ElseBranch&& elseBranch)
604 if constexpr (((t0 == value) || ... || (tt == value)))
605 return branches(std::integral_constant<T, value>{});
611 template<
class Result,
class T,
class Value,
class Branches,
class ElseBranch>
612 constexpr Result
switchCases(std::integer_sequence<T>,
const Value& , Branches&& , ElseBranch&& elseBranch)
617 template<
class Result,
class T, T t0, T... tt,
class Value,
class Branches,
class ElseBranch>
618 constexpr Result
switchCases(std::integer_sequence<T, t0, tt...>,
const Value& value, Branches&& branches, ElseBranch&& elseBranch)
621 return branches(std::integral_constant<T, t0>());
623 return Impl::switchCases<Result>(std::integer_sequence<T, tt...>(), value, branches, elseBranch);
627 template <
class Result,
class T,
class Value,
class Branches,
class ElseBranch>
628 constexpr Result
switchCases(IntegralRange<T> range,
const Value& value, Branches&& branches, ElseBranch&& elseBranch)
630 return range.contains(value) ? branches(T(value)) : elseBranch();
634 template <
class Result,
class T, T to, T from,
class Value,
class Branches,
class ElseBranch>
635 constexpr Result
switchCases(StaticIntegralRange<T, to, from> range,
const Value& value, Branches&& branches, ElseBranch&& elseBranch)
637 using seq =
typename decltype(range)::integer_sequence;
638 return Impl::switchCases<Result>(seq{}, value, branches, elseBranch);
672template<
class Cases,
class Value,
class Branches,
class ElseBranch>
673constexpr decltype(
auto)
switchCases(
const Cases& cases,
const Value& value, Branches&& branches, ElseBranch&& elseBranch)
675 return Impl::switchCases<decltype(elseBranch())>(cases, value, std::forward<Branches>(branches), std::forward<ElseBranch>(elseBranch));
697template<
class Cases,
class Value,
class Branches>
698constexpr void switchCases(
const Cases& cases,
const Value& value, Branches&& branches)
700 Impl::switchCases<void>(cases, value, std::forward<Branches>(branches),
701 []{ assert(
false &&
"value not found in range"); });
722template <
class T,
class Value,
class Branches>
725 assert(range.
contains(value) &&
"value not found in range");
Adapter of a hybrid functor that maintains results hybrid.
Definition: hybridutilities.hh:418
dynamic integer range for use in range-based for loops
Definition: rangeutilities.hh:177
constexpr bool contains(value_type index) const noexcept
check whether given index is within range [from, to)
Definition: rangeutilities.hh:207
static integer range for use in range-based for loops
Definition: rangeutilities.hh:230
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:52
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:492
void ifElse(const Condition &condition, IfFunc &&ifFunc)
A conditional expression.
Definition: hybridutilities.hh:357
constexpr auto size(const T &t)
Size query.
Definition: hybridutilities.hh:73
constexpr auto integralRange(const End &end)
Create an integral range starting from 0.
Definition: hybridutilities.hh:191
constexpr auto minus
Function object for performing subtraction.
Definition: hybridutilities.hh:550
constexpr auto equals(T1 &&t1, T2 &&t2)
Equality comparison.
Definition: hybridutilities.hh:587
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition: hybridutilities.hh:256
constexpr auto equal_to
Function object for performing equality comparison.
Definition: hybridutilities.hh:572
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:484
constexpr auto plus
Function object for performing addition.
Definition: hybridutilities.hh:528
constexpr auto min
Function object that returns the smaller of the given values.
Definition: hybridutilities.hh:506
constexpr decltype(auto) elementAt(Container &&c, Index &&i)
Get element at given position from container.
Definition: hybridutilities.hh:126
constexpr T accumulate(Range &&range, T value, F &&f)
Accumulate values.
Definition: hybridutilities.hh:279
constexpr void switchCases(IntegralRange< T > range, const Value &value, Branches &&branches)
Switch statement.
Definition: hybridutilities.hh:723
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
Utilities for reduction like operations on ranges.
Check if T is an std::integral_constant<I, i>
Definition: typetraits.hh:384
Helper class for tagging priorities.
Definition: typeutilities.hh:73
Utilities for type computations, constraining overloads, ...