Loading...
Searching...
No Matches
Hybrid Utilities
Common » Utilities

Hybrid utility functions that work on homogeneous as well as heterogeneous containers. More...

Collaboration diagram for Hybrid Utilities:

## Classes

class | Dune::Hybrid::HybridFunctor< Functor >
| Adapter of a hybrid functor that maintains results hybrid. More...

## Functions

template<class T>
constexpr auto | Dune::Hybrid::size (const T &t)
| Size query.
template<class Container, class Index>
constexpr decltype(auto) | Dune::Hybrid::elementAt (Container &&c, Index &&i)
| Get element at given position from container.
template<class Begin, class End>
constexpr auto | Dune::Hybrid::integralRange (const Begin &begin, const End &end)
| Create an integral range.
template<class End>
constexpr auto | Dune::Hybrid::integralRange (const End &end)
| Create an integral range starting from 0.
template<class Range, class F>
constexpr void | Dune::Hybrid::forEach (Range &&range, F &&f)
| Range based for loop.
template<class Range, class T, class F>
constexpr T | Dune::Hybrid::accumulate (Range &&range, T value, F &&f)
| Accumulate values.
template<class Condition, class IfFunc, class ElseFunc>
decltype(auto) | Dune::Hybrid::ifElse (const Condition &condition, IfFunc &&ifFunc, ElseFunc &&elseFunc)
| A conditional expression.
template<class Condition, class IfFunc>
void | Dune::Hybrid::ifElse (const Condition &condition, IfFunc &&ifFunc)
| A conditional expression.
template<class... Args>
constexpr decltype(auto) | Dune::Hybrid::HybridFunctor< Functor >::operator() (const Args &... args) const
| Adapter of a hybrid functor that keeps results hybrid.
template<class Cases, class Value, class Branches, class ElseBranch>
constexpr decltype(auto) | Dune::Hybrid::switchCases (const Cases &cases, const Value &value, Branches &&branches, ElseBranch &&elseBranch)
| Switch statement.
template<class Cases, class Value, class Branches>
constexpr void | Dune::Hybrid::switchCases (const Cases &cases, const Value &value, Branches &&branches)
| Switch statement.
template<class T, class Value, class Branches>
constexpr void | Dune::Hybrid::switchCases (IntegralRange< T > range, const Value &value, Branches &&branches)
| Switch statement.

## Variables

constexpr auto | Dune::Hybrid::max = hybridFunctor(Impl::Max{})
| Function object that returns the greater of the given values.
constexpr auto | Dune::Hybrid::min = hybridFunctor(Impl::Min{})
| Function object that returns the smaller of the given values.
constexpr auto | Dune::Hybrid::plus = hybridFunctor(std::plus<>{})
| Function object for performing addition.
constexpr auto | Dune::Hybrid::minus = hybridFunctor(std::minus<>{})
| Function object for performing subtraction.
constexpr auto | Dune::Hybrid::equal_to = hybridFunctor(std::equal_to<>{})
| Function object for performing equality comparison.

## Detailed Description

Hybrid utility functions that work on homogeneous as well as heterogeneous containers.

## Function Documentation

## accumulate()

template<class Range, class T, class F>

T Dune::Hybrid::accumulate | ( | Range && | range,
|  | T | value,
|  | F && | f )

constexpr

Accumulate values.

Template Parameters

  Range | Type of given range
  T | Type of accumulated value
  F | Type of binary accumulation operator

Parameters

  range | The range of values to accumulate
  value | Initial value for accumulation
  f | Binary operator for accumulation

This supports looping over the same ranges as Hybrid::forEach

## elementAt()

template<class Container, class Index>

decltype(auto) Dune::Hybrid::elementAt | ( | Container && | c,
|  | Index && | i )

constexpr

Get element at given position from container.

Template Parameters

  Container | Type of given container
  Index | Type of index

Parameters

  c | Given container
  i | Index of element to obtain

Returns
  The element at position i, i.e. c[i]

If this returns the i-th entry of c. It supports the following containers

- Containers providing dynamic access via operator[]
- Heterogeneous containers providing access via operator[](integral_constant<...>)
- std::tuple<...>
- std::integer_sequence

## forEach()

template<class Range, class F>

void Dune::Hybrid::forEach | ( | Range && | range,
|  | F && | f )

constexpr

Range based for loop.

Template Parameters

  Range | Type of given range
  F | Type of given predicate

Parameters

  range | The range to loop over
  f | A predicate that will be called with each entry of the range

This supports looping over the following ranges

- ranges obtained from integralRange()
- all ranges that provide Hybrid::size() and Hybrid::elementAt()

This especially included instances of std::integer_sequence, std::tuple, Dune::TupleVector, and Dune::MultiTypeBlockVector.

## ifElse() [1/2]

template<class Condition, class IfFunc>

void Dune::Hybrid::ifElse | ( | const Condition & | condition,
|  | IfFunc && | ifFunc )

A conditional expression.

This provides an ifElse conditional with empty else clause.

## ifElse() [2/2]

template<class Condition, class IfFunc, class ElseFunc>

decltype(auto) Dune::Hybrid::ifElse | ( | const Condition & | condition,
|  | IfFunc && | ifFunc,
|  | ElseFunc && | elseFunc )

A conditional expression.

This will call either ifFunc or elseFunc depending on the condition. In any case a single argument will be passed to the called function. This will always be the identity function. Passing an expression through this function will lead to lazy evaluation. This way both 'branches' can contain expressions that are only valid within this branch if the condition is a std::integral_constant<bool,*>.

In order to do this, the passed functors must have a single argument of type auto.

Due to the lazy evaluation mechanism and support for std::integral_constant<bool,*> this allows to emulate a static if statement.

## integralRange() [1/2]

template<class Begin, class End>

auto Dune::Hybrid::integralRange | ( | const Begin & | begin,
|  | const End & | end )

constexpr

Create an integral range.

Template Parameters

  Begin | Type of begin entry of the range
  End | Type of end entry of the range

Parameters

  begin | First entry of the range
  end | One past the last entry of the range

Returns
  An object encoding the given range

If Begin and End are both instances of type std::integral_constant, the returned range encodes begin and end statically.

## integralRange() [2/2]

template<class End>

auto Dune::Hybrid::integralRange | ( | const End & | end | ) |

constexpr

Create an integral range starting from 0.

Template Parameters

  End | Type of end entry of the range

Parameters

  end | One past the last entry of the range

Returns
  An object encoding the given range

This is a short cut for integralRange(_0, end).

## operator()()

template<class Functor>
template<class... Args>

decltype(auto) Dune::Hybrid::HybridFunctor< Functor >::operator() | ( | const Args &... | args | ) | const

inline constexpr

Adapter of a hybrid functor that keeps results hybrid.

Implements an operator that promotes the results of the underlying functor to an integral constant if all the function arguments are integral constants, otherwise, usual promotion rules apply.

## size()

template<class T>

auto Dune::Hybrid::size | ( | const T & | t | ) |

constexpr

Size query.

Template Parameters

  T | Type of container whose size is queried

Parameters

  t | Container whose size is queried

Returns
  Size of t

If the size of t is known at compile type the size is returned as std::integral_constant<std::size_t, size>. Otherwise the result of t.size() is returned.

Supported types for deriving the size at compile time are:

- instances of std::integer_sequence
- all types std::tuple_size is implemented for
- all types that have a static constexpr method ::size() The latter e.g. includes Dune::FieldVector

## switchCases() [1/3]

template<class Cases, class Value, class Branches>

void Dune::Hybrid::switchCases | ( | const Cases & | cases,
|  | const Value & | value,
|  | Branches && | branches )

constexpr

Switch statement.

Template Parameters

  Cases | Type of case range
  Value | Type of value to check against the cases
  Branches | Type of branch function

Parameters

  cases | A range of cases to check for
  value | The value to check against the cases
  branches | A callback that will be executed with matching entry from case list

Value is checked against all entries of the given range. If one matches, then branches is executed with the matching value as single argument. If the range is an std::integer_sequence, or StaticIntegralRange, the value is passed as std::integral_constant. If non of the entries matches, the behavior is undefined.

## switchCases() [2/3]

template<class Cases, class Value, class Branches, class ElseBranch>

decltype(auto) Dune::Hybrid::switchCases | ( | const Cases & | cases,
|  | const Value & | value,
|  | Branches && | branches,
|  | ElseBranch && | elseBranch )

constexpr

Switch statement.

Template Parameters

  Cases | Type of case range
  Value | Type of value to check against the cases
  Branches | Type of branch function
  ElseBranch | Type of branch function

Parameters

  cases | A range of cases to check for
  value | The value to check against the cases
  branches | A callback that will be executed with matching entry from case list
  elseBranch | A callback that will be executed if no other entry matches

Value is checked against all entries of the given range. If one matches, then branches is executed with the matching value as single argument. If the range is an std::integer_sequence, or StaticIntegralRange, the value is passed as std::integral_constant. If non of the entries matches, then elseBranch is executed without any argument.

Notice that this short circuits, e.g., if one case matches, the others are no longer evaluated.

The return value will be deduced from the else branch.

## switchCases() [3/3]

template<class T, class Value, class Branches>

void Dune::Hybrid::switchCases | ( | IntegralRange< T > | range,
|  | const Value & | value,
|  | Branches && | branches )

constexpr

Switch statement.

Template Parameters

  T | The type of the cases
  Value | Type of value to check against the cases
  Branches | Type of branch function

Parameters

  range | A dynamic range of cases to check for
  value | The value to check against the cases
  branches | A callback that will be executed with matching entry from case list

This overload of the switchCases utility is selected if the range of cases is passed as an IntegralRange. If the value is contained in that range, it is passed as single argument to the callback branches. If not, the behavior is undefined.

## Variable Documentation

## equal_to

auto Dune::Hybrid::equal_to = hybridFunctor(std::equal_to<>{})

inline constexpr

Function object for performing equality comparison.

See also
  HybridFunctor

If both arguments have a static member value, the result of comparing these for equality is returned as std::integral_constant<bool, *>. Otherwise the result of a comparison of the two arguments is directly returned.

using namespace Dune::Indices;
{ // hybrid transformation!
auto j = Dune::Hybrid::equal_to( 2, 1); // -> false
auto j = Dune::Hybrid::equal_to( 2, _1); // -> false
auto k = Dune::Hybrid::equal_to(_2, _1); // -> std::false_type
// independent of the context, `k` encodes its value in the type system
}
Dune::Indices::_1
constexpr index_constant< 1 > _1
Compile time index with value 1.
Definition indices.hh:55
Dune::Indices::_2
constexpr index_constant< 2 > _2
Compile time index with value 2.
Definition indices.hh:58
Dune::Hybrid::equal_to
constexpr auto equal_to
Function object for performing equality comparison.
Definition hybridutilities.hh:577
Dune::Indices
Namespace with predefined compile time indices for the range [0,19].
Definition indices.hh:50

## max

auto Dune::Hybrid::max = hybridFunctor(Impl::Max{})

inline constexpr

Function object that returns the greater of the given values.

See also
  HybridFunctor

If all arguments have a static member value, the maximum value of these is returned as std::integral_constant<*, *>. Otherwise the result of a direct max of the arguments is returned.

using namespace Dune::Indices;
{ // hybrid transformation!
auto j = Dune::Hybrid::max( 1, 2); // -> 2
auto j = Dune::Hybrid::max( 1, _2); // -> 2
auto k = Dune::Hybrid::max(_1, _2); // -> Dune::Indices::_2
// independent of the context, `k` encodes its value in the type system
}
Dune::Hybrid::max
constexpr auto max
Function object that returns the greater of the given values.
Definition hybridutilities.hh:489

## min

auto Dune::Hybrid::min = hybridFunctor(Impl::Min{})

inline constexpr

Function object that returns the smaller of the given values.

See also
  HybridFunctor

If all arguments have a static member value, the minimum value of these is returned as std::integral_constant<*, *>. Otherwise the result of a direct min of the arguments is returned.

using namespace Dune::Indices;
{ // hybrid transformation!
auto j = Dune::Hybrid::min( 1, 2); // -> 1
auto j = Dune::Hybrid::min( 1, _2); // -> 1
auto k = Dune::Hybrid::min(_1, _2); // -> Dune::Indices::_1
// independent of the context, `k` encodes its value in the type system
}
Dune::Hybrid::min
constexpr auto min
Function object that returns the smaller of the given values.
Definition hybridutilities.hh:511

## minus

auto Dune::Hybrid::minus = hybridFunctor(std::minus<>{})

inline constexpr

Function object for performing subtraction.

See also
  HybridFunctor

If all arguments have a static member value, the subtracted value of these is returned as std::integral_constant<*, *>. Otherwise the result of a direct subtraction of the arguments is returned.

using namespace Dune::Indices;
{ // hybrid transformation!
auto j = Dune::Hybrid::minus( 2, 1); // -> 1
auto j = Dune::Hybrid::minus( 2, _1); // -> 1
auto k = Dune::Hybrid::minus(_2, _1); // -> Dune::Indices::_1
// independent of the context, `k` encodes its value in the type system
}
Dune::Hybrid::minus
constexpr auto minus
Function object for performing subtraction.
Definition hybridutilities.hh:555

## plus

auto Dune::Hybrid::plus = hybridFunctor(std::plus<>{})

inline constexpr

Function object for performing addition.

See also
  HybridFunctor

If all arguments have a static member value, the added value of these is returned as std::integral_constant<*, *>. Otherwise the result of a direct addition of the arguments is returned.

using namespace Dune::Indices;
{ // hybrid transformation!
auto j = Dune::Hybrid::plus( 1, 2); // -> 3
auto j = Dune::Hybrid::plus( 1, _2); // -> 3
auto k = Dune::Hybrid::plus(_1, _2); // -> Dune::Indices::_3
// independent of the context, `k` encodes its value in the type system
}
Dune::Hybrid::plus
constexpr auto plus
Function object for performing addition.
Definition hybridutilities.hh:533