4#ifndef DUNE_TYPETREE_UTILITY_HH
5#define DUNE_TYPETREE_UTILITY_HH
14#include <dune/common/indices.hh>
15#include <dune/common/hybridutilities.hh>
16#include <dune/typetree/nodeinterface.hh>
17#include <dune/typetree/nodetags.hh>
29 std::shared_ptr<T> convert_arg(
const T& t)
31 return std::make_shared<T>(t);
35 std::shared_ptr<T> convert_arg(T& t)
40 template<
typename BaseType,
typename T>
41 T& assertGridViewType(T& t)
43 static_assert((std::is_same<
typename BaseType::Traits::GridViewType,
44 typename T::Traits::GridViewType>::value),
45 "GridViewType must be equal in all components of composite type");
51 typename std::enable_if<!std::is_lvalue_reference<T>::value,std::shared_ptr<T> >::type convert_arg(T&& t)
53 return std::make_shared<T>(std::forward<T>(t));
57 namespace Experimental {
67 template<
class BinaryOp,
class Arg>
68 constexpr decltype(
auto)
69 left_fold(BinaryOp&& binary_op, Arg&& arg)
71 return std::forward<Arg>(arg);
95 template<
class BinaryOp,
class Init,
class Arg0,
class... Args>
96 constexpr decltype(
auto)
97 left_fold(BinaryOp&& binary_op, Init&& init, Arg0&& arg_0, Args&&... args)
100 std::forward<BinaryOp>(binary_op),
101 binary_op(std::forward<Init>(init), std::forward<Arg0>(arg_0)),
102 std::forward<Args>(args)...);
107 using namespace Dune::Hybrid;
110 template<
class Op,
class... Args>
111 constexpr auto applyOperator(Op&& op, Args&&... args)
113 using T = std::common_type_t<Args...>;
114 return op(
static_cast<T
>(args)...);
117 template<
class Op,
class T, T... Args>
118 constexpr auto applyOperator(Op, std::integral_constant<T,Args>...)
120 static_assert(std::is_default_constructible_v<Op>,
121 "Operator in integral expressions shall be default constructible");
122 constexpr auto result = Op{}(T{Args}...);
123 return std::integral_constant<std::decay_t<
decltype(result)>,result>{};
128 template<
class... Args>
129 constexpr auto operator()(Args&&... args)
const
131 using T = std::common_type_t<Args...>;
132 return std::max({
static_cast<T
>(args)...});
137 static constexpr auto max = [](
const auto& a,
const auto& b)
139 return Detail::applyOperator(Detail::Max{}, a, b);
142 static constexpr auto plus = [](
const auto& a,
const auto& b)
144 return Detail::applyOperator(std::plus<>{}, a, b);
147 static constexpr auto minus = [](
const auto& a,
const auto& b)
149 return Detail::applyOperator(std::minus<>{}, a, b);
166 template<
typename Tree,
typename Tag = StartTag>
196 template<
typename Node>
200 static const std::size_t
depth = 1;
210 template<
typename Node>
211 struct TreeInfo<Node,PowerNodeTag>
214 typedef TreeInfo<typename Node::ChildType,NodeTag<typename Node::ChildType>> ChildInfo;
216 static const std::size_t
depth = 1 + ChildInfo::depth;
218 static const std::size_t
nodeCount = 1 + StaticDegree<Node>::value * ChildInfo::nodeCount;
220 static const std::size_t
leafCount = StaticDegree<Node>::value * ChildInfo::leafCount;
229 template<
typename Node, std::
size_t k, std::
size_t n>
230 struct generic_compositenode_children_info
233 typedef generic_compositenode_children_info<Node,k+1,n> NextChild;
236 typedef typename Node::template Child<k>::Type
Child;
237 typedef NodeTag<Child> ChildTag;
238 typedef TreeInfo<Child,ChildTag> ChildInfo;
241 static const std::size_t maxDepth = ChildInfo::depth > NextChild::maxDepth ? ChildInfo::depth : NextChild::maxDepth;
243 static const std::size_t nodeCount = ChildInfo::nodeCount + NextChild::nodeCount;
245 static const std::size_t leafCount = ChildInfo::leafCount + NextChild::leafCount;
250 template<
typename Node, std::
size_t n>
251 struct generic_compositenode_children_info<Node,n,n>
253 static const std::size_t maxDepth = 0;
255 static const std::size_t nodeCount = 0;
257 static const std::size_t leafCount = 0;
264 template<
typename Node>
265 struct GenericCompositeNodeInfo
268 typedef generic_compositenode_children_info<Node,0,StaticDegree<Node>::value> Children;
270 static const std::size_t depth = 1 + Children::maxDepth;
272 static const std::size_t nodeCount = 1 + Children::nodeCount;
274 static const std::size_t leafCount = Children::leafCount;
280 template<
typename Node>
281 struct TreeInfo<Node,CompositeNodeTag>
282 :
public GenericCompositeNodeInfo<Node>
293 template<
typename... Args>
298 namespace apply_to_tuple_policy {
314 template<
typename T,
typename F, std::size_t... i>
317 discard((f(std::get<i>(std::forward<T>(t))),0)...);
321 template<
typename T,
typename F, std::size_t... i>
322 void _apply_to_tuple(T&& t, F&& f, std::index_sequence<i...>,apply_to_tuple_policy::pass_index)
337 template<
typename T,
typename F,
typename Policy>
340 const std::size_t size = std::tuple_size<typename std::decay<T>::type>::value;
344 std::make_index_sequence<size>{},
std::integral_constant< std::size_t, i > index_constant
An index constant with value i.
Definition: indices.hh:28
auto max(ADLTag< 0 >, const V &v1, const V &v2)
implements binary Simd::max()
Definition: defaults.hh:79
void apply_to_tuple(T &&t, F &&f, Policy=apply_to_tuple_policy::default_policy())
Apply a functor to each element of a std::tuple.
Definition: utility.hh:338
void discard(Args &&... args)
No-op function to make calling a function on a variadic template argument pack legal C++.
Definition: utility.hh:294
Namespace with predefined compile time indices for the range [0,19].
Definition: indices.hh:49
no_pass_index default_policy
Default policy.
Definition: utility.hh:307
Dune namespace.
Definition: alignedallocator.hh:11
std::shared_ptr< T > stackobject_to_shared_ptr(T &t)
Create a shared_ptr for a stack-allocated object.
Definition: shared_ptr.hh:70
This file implements several utilities related to std::shared_ptr.
Tag designating a leaf node.
Definition: nodetags.hh:16
Struct for obtaining some basic structural information about a TypeTree.
Definition: utility.hh:168
static const std::size_t leafCount
The number of leaf nodes in the TypeTree.
Definition: utility.hh:183
static const std::size_t depth
The depth of the TypeTree.
Definition: utility.hh:177
static const std::size_t nodeCount
The total number of nodes in the TypeTree.
Definition: utility.hh:180
Do not pass the index of the current tuple to the functor.
Definition: utility.hh:301
Pass the index of the current tuple to the functor as its first argument in a std::integral_constant.
Definition: utility.hh:304