AMDiS  0.3
The Adaptive Multi-Dimensional Simulation Toolbox
SwitchCases.hpp
1 #pragma once
2 
3 #include <cassert>
4 
5 #include <dune/common/hybridutilities.hh>
6 #include <dune/common/rangeutilities.hh>
7 
8 namespace AMDiS
9 {
10  // static switching. Iterates over all possible values
11  template <class T, T to, T from, class Value, class Then, class Else>
12  constexpr decltype(auto) switchCases(const Dune::StaticIntegralRange<T,to,from>& cases, const Value& value,
13  Then&& thenBranch, Else&& elseBranch)
14  {
15  using integer_sequence = typename Dune::StaticIntegralRange<T,to,from>::integer_sequence;
16  return Dune::Hybrid::switchCases(integer_sequence{}, value, FWD(thenBranch), FWD(elseBranch));
17  }
18 
19  // dynamic switching. Calls the `thenBranch` or `elseBranch` directly
20  template <class T, class Value, class Then, class Else>
21  constexpr decltype(auto) switchCases(const Dune::IntegralRange<T>& cases, const Value& value,
22  Then&& thenBranch, Else&& elseBranch)
23  {
24  if (value >= cases[0] && value <= cases[cases.size()-1])
25  return thenBranch(value);
26  else
27  return elseBranch(value);
28  }
29 
30  // static switching. Iterates over all possible values
31  template<class T, T to, T from, class Value, class Then>
32  constexpr void switchCases(const Dune::StaticIntegralRange<T,to,from>& cases, const Value& value, Then&& thenBranch)
33  {
34  using integer_sequence = typename Dune::StaticIntegralRange<T,to,from>::integer_sequence;
35  Dune::Hybrid::switchCases(integer_sequence{}, value, FWD(thenBranch));
36  }
37 
38  // dynamic switching. Calls the `thenBranch` directly
39  template<class T, class Value, class Then>
40  constexpr void switchCases(const Dune::IntegralRange<T>& cases, const Value& value, Then&& thenBranch)
41  {
42  assert(value >= cases[0] && value <= cases[cases.size()-1]);
43  thenBranch(value);
44  }
45 
46 } // end namespace AMDiS
Definition: AdaptiveGrid.hpp:373
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6