6#ifndef DUNE_COMMON_TYPETREE_TREECONTAINER_HH
7#define DUNE_COMMON_TYPETREE_TREECONTAINER_HH
37 template<
class LeafToValue>
38 class ContainerFactory
49 ContainerFactory(LeafToValue leafToValue) :
50 leafToValue_(leafToValue)
54 requires Dune::TypeTree::Concept::TreeNode<Node>
55 auto operator()(
const Node& node)
57 if constexpr (Dune::TypeTree::Concept::LeafTreeNode<Node>)
58 return leafToValue_(node);
61 if constexpr (Dune::TypeTree::Concept::UniformInnerTreeNode<Node>)
63 if constexpr (Dune::TypeTree::Concept::StaticDegreeInnerTreeNode<Node>)
66 return std::array{(*this)(node.child(indices))...};
67 }, std::make_index_sequence<std::size_t(Node::degree())>());
71 using TransformedChild =
decltype((*this)(node.child(0)));
72 std::vector<TransformedChild> container;
73 container.reserve(node.degree());
74 for (std::size_t i = 0; i < node.degree(); ++i)
75 container.emplace_back((*
this)(node.child(i)));
83 }, std::make_index_sequence<std::size_t(Node::degree())>());
89 LeafToValue leafToValue_;
96 template<
class Container>
97 class TreeContainerVectorBackend
100 static constexpr decltype(
auto) accessByTreePath(C&& container,
const TreePath<>& path)
105 template<
class C,
class... T>
106 static constexpr decltype(
auto) accessByTreePath(C&& container,
const TreePath<T...>& path)
111 }, std::make_index_sequence<
sizeof...(T)-1>());
112 return accessByTreePath(container[
head], tailPath);
116 template<
class C,
class Tree>
117 static void recursiveResize(C& container,
const Tree& tree)
119 if constexpr (not Dune::TypeTree::Concept::LeafTreeNode<Tree>)
121 if constexpr (
requires { container.resize(0u); })
122 container.resize(tree.degree());
124 recursiveResize(container[i], tree.child(i));
131 TreeContainerVectorBackend(Container&& container) :
132 container_(std::move(container))
136 template <
class Tree>
137 requires Dune::TypeTree::Concept::TreeNode<Tree>
138 TreeContainerVectorBackend(
const Tree& tree) :
139 TreeContainerVectorBackend()
145 template <
class C = Container,
146 std::enable_if_t<std::is_default_constructible_v<C>,
bool> =
true>
147 TreeContainerVectorBackend() :
154 return accessByTreePath(container_, path);
160 return accessByTreePath(container_, path);
165 requires Dune::TypeTree::Concept::TreeNode<Tree>
166 void resize(
const Tree& tree)
168 recursiveResize(container_, tree);
171 const Container& data()
const
182 Container container_;
185 template<
class Container>
186 auto makeTreeContainerVectorBackend(Container&& container)
188 return TreeContainerVectorBackend<std::decay_t<Container>>(std::forward<Container>(container));
198 template<
template<
class Node>
class LeafToValue>
199 struct LeafToDefaultConstructibleValue
202 auto operator()(
const Node& node)
const
204 return LeafToValue<Node>{};
229 template<
class Tree,
class LeafToValue>
232 auto f = std::ref(leafToValue);
233 auto factory = Impl::ContainerFactory<decltype(f)>(f);
234 return Impl::makeTreeContainerVectorBackend(factory(tree));
252 template<
class Value,
class Tree>
261 template<
class Value,
class Tree>
267 template<
template<
class Node>
class LeafToValue,
class Tree>
268 using TreeContainer = std::decay_t<decltype(makeTreeContainer(std::declval<const Tree&>(), std::declval<Impl::LeafToDefaultConstructibleValue<LeafToValue>>()))>;
Provides the TupleVector class that augments std::tuple by operator[].
Utilities for reduction like operations on ranges.
constexpr index_constant< 0 > _0
Compile time index with value 0.
Definition indices.hh:52
decltype(auto) constexpr unpackIntegerSequence(F &&f, std::integer_sequence< I, i... > sequence)
Unpack an std::integer_sequence<I,i...> to std::integral_constant<I,i>...
Definition indices.hh:124
std::integral_constant< std::size_t, i > index_constant
An index constant with value i.
Definition indices.hh:29
static constexpr IntegralRange< std::decay_t< T > > range(T &&from, U &&to) noexcept
free standing function for setting up a range based for loop over an integer range for (auto i: range...
Definition rangeutilities.hh:288
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition hybridutilities.hh:261
auto makeTreeContainer(const Tree &tree, LeafToValue &&leafToValue)
Create container having the same structure as the given tree.
Definition treecontainer.hh:230
std::decay_t< decltype(makeTreeContainer< Value >(std::declval< const Tree & >()))> UniformTreeContainer
Alias to container type generated by makeTreeContainer for given tree type and uniform value type.
Definition treecontainer.hh:262
std::decay_t< decltype(makeTreeContainer(std::declval< const Tree & >(), std::declval< Impl::LeafToDefaultConstructibleValue< LeafToValue > >()))> TreeContainer
Alias to container type generated by makeTreeContainer for give tree type and when using LeafToValue ...
Definition treecontainer.hh:268
Dune::HybridMultiIndex< T... > TreePath
A type for representing tree paths that supports both compile time and run time indices.
Definition treepath.hh:43
constexpr auto treePath(const T &... t)
Constructs a new TreePath from the given indices.
Definition treepath.hh:55
constexpr std::integral_constant< T, I0 > head(std::integer_sequence< T, I0, II... >)
For a sequence [head,tail...) return the single head element.
Definition integersequence.hh:53
constexpr auto makeTupleVector(T &&... t)
Definition tuplevector.hh:108
Definition childaccess.hh:23