Skip to content
Snippets Groups Projects
Commit 662a6b31 authored by Praetorius, Simon's avatar Praetorius, Simon
Browse files

add hybrid utility SwitchCases for dynamic and static dispatching

parent e01dcaf2
Branches
Tags
1 merge request!51add hybrid utility SwitchCases for dynamic and static dispatching
......@@ -25,6 +25,7 @@ install(FILES
Resize.hpp
StaticSize.hpp
String.hpp
SwitchCases.hpp
Tags.hpp
Transposed.hpp
TupleUtility.hpp
......
#pragma once
#include <cassert>
#include <dune/common/hybridutilities.hh>
#include <dune/common/rangeutilities.hh>
namespace AMDiS
{
template <class T, T to, T from, class Value, class Then, class Else>
constexpr decltype(auto) switchCases(const Dune::StaticIntegralRange<T,to,from>& cases, const Value& value,
Then&& thenBranch, Else&& elseBranch)
{
using integer_sequence = typename Dune::StaticIntegralRange<T,to,from>::integer_sequence;
return Dune::Hybrid::switchCases(integer_sequence{}, value, FWD(thenBranch), FWD(elseBranch));
}
template <class T, class Value, class Then, class Else>
constexpr decltype(auto) switchCases(const Dune::IntegralRange<T>& cases, const Value& value,
Then&& thenBranch, Else&& elseBranch)
{
if (value >= cases[0] && value <= cases[cases.size()-1])
return thenBranch(value);
else
return elseBranch(value);
}
template<class T, T to, T from, class Value, class Then>
constexpr void switchCases(const Dune::StaticIntegralRange<T,to,from>& cases, const Value& value, Then&& thenBranch)
{
using integer_sequence = typename Dune::StaticIntegralRange<T,to,from>::integer_sequence;
Dune::Hybrid::switchCases(integer_sequence{}, value, FWD(thenBranch));
}
template<class T, class Value, class Then>
constexpr void switchCases(const Dune::IntegralRange<T>& cases, const Value& value, Then&& thenBranch)
{
assert(value >= cases[0] && value <= cases[cases.size()-1]);
thenBranch(value);
}
} // end namespace AMDiS
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment