4#ifndef DUNE_TYPETREE_TREECONTAINER_HH
5#define DUNE_TYPETREE_TREECONTAINER_HH
12#include <dune/common/indices.hh>
15#include <dune/typetree/treepath.hh>
33 template<
class LeafToValue>
34 class ContainerFactory
45 ContainerFactory(LeafToValue leafToValue) :
46 leafToValue_(leafToValue)
50 std::enable_if_t<Node::isLeaf, int> = 0>
51 auto operator()(
const Node& node)
53 return leafToValue_(node);
57 std::enable_if_t<Node::isPower, int> = 0>
58 auto operator()(
const Node& node)
60 using TransformedChild =
decltype((*this)(node.child(0)));
61 return std::array<TransformedChild, Node::degree()>();
65 std::enable_if_t<Node::isComposite, int> = 0>
66 auto operator()(
const Node& node)
69 return Dune::makeTupleVector((*
this)(node.child(indices))...);
70 }, std::make_index_sequence<Node::degree()>());
74 LeafToValue leafToValue_;
81 template<
class Container>
82 class TreeContainerVectorBackend
85 static constexpr decltype(
auto) accessByTreePath(C&& container,
const HybridTreePath<>& path)
90 template<
class C,
class... T>
91 static constexpr decltype(
auto) accessByTreePath(C&& container,
const HybridTreePath<T...>& path)
96 }, std::make_index_sequence<
sizeof...(T)-1>());
97 return accessByTreePath(container[head], tailPath);
101 TreeContainerVectorBackend() :
105 TreeContainerVectorBackend(Container&& container) :
106 container_(
std::move(container))
109 TreeContainerVectorBackend(TreeContainerVectorBackend&& other) :
110 container_(
std::move(other.container_))
114 decltype(
auto)
operator[](
const HybridTreePath<T...>& path)
const
116 return accessByTreePath(container_, path);
120 decltype(
auto)
operator[](
const HybridTreePath<T...>& path)
122 return accessByTreePath(container_, path);
125 const Container& data()
const
136 Container container_;
139 template<
class Container>
140 auto makeTreeContainerVectorBackend(Container&& container)
142 return TreeContainerVectorBackend<std::decay_t<Container>>(std::forward<Container>(container));
152 template<
template<
class Node>
class LeafToValue>
153 struct LeafToDefaultConstructibleValue
156 auto operator()(
const Node& node)
const
158 return LeafToValue<Node>{};
183 template<
class Tree,
class LeafToValue>
186 auto f = std::ref(leafToValue);
187 auto factory = Detail::ContainerFactory<decltype(f)>(f);
188 return Detail::makeTreeContainerVectorBackend(factory(tree));
206 template<
class Value,
class Tree>
215 template<
class Value,
class Tree>
221 template<
template<
class Node>
class LeafToValue,
class Tree>
222 using TreeContainer = std::decay_t<decltype(makeTreeContainer(std::declval<const Tree&>(), std::declval<Detail::LeafToDefaultConstructibleValue<LeafToValue>>()))>;
decltype(auto) 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:123
constexpr index_constant< 0 > _0
Compile time index with value 0.
Definition: indices.hh:51
constexpr HybridTreePath< T... > treePath(const T &... t)
Constructs a new HybridTreePath from the given indices.
Definition: treepath.hh:188
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:216
std::decay_t< decltype(makeTreeContainer(std::declval< const Tree & >(), std::declval< Detail::LeafToDefaultConstructibleValue< LeafToValue > >()))> TreeContainer
Alias to container type generated by makeTreeContainer for give tree type and when using LeafToValue ...
Definition: treecontainer.hh:222
auto makeTreeContainer(const Tree &tree)
Create container havin the same structure as the given tree.
Definition: treecontainer.hh:207
Dune namespace.
Definition: alignedallocator.hh:14
Provides the TupleVector class that augments std::tuple by operator[].