Dune Core Modules (2.9.0)

nodes.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_FUNCTIONS_FUNCTIONSPACEBASES_NODES_HH
4#define DUNE_FUNCTIONS_FUNCTIONSPACEBASES_NODES_HH
5
6#include <cassert>
7#include <memory>
8
9#include <dune/common/indices.hh>
10
11#include <dune/typetree/leafnode.hh>
12#include <dune/typetree/powernode.hh>
13#include <dune/typetree/compositenode.hh>
14#include <dune/typetree/traversal.hh>
15#include <dune/typetree/visitor.hh>
16
17namespace Dune {
18 namespace Functions {
19
20
21 namespace Impl {
22
23
24 struct ClearSizeVisitor
25 : public TypeTree::TreeVisitor
26 , public TypeTree::DynamicTraversal
27 {
28
29 template<typename Node, typename TreePath>
30 void pre(Node& node, TreePath treePath)
31 {
32 leaf(node,treePath);
33 node.setSize(0);
34 }
35
36 template<typename Node, typename TreePath>
37 void leaf(Node& node, TreePath treePath)
38 {
39 node.setOffset(offset_);
40 }
41
42 ClearSizeVisitor(std::size_t offset)
43 : offset_(offset)
44 {}
45
46 const std::size_t offset_;
47
48 };
49
50
51 template<typename Entity>
52 struct BindVisitor
53 : public TypeTree::TreeVisitor
54 , public TypeTree::DynamicTraversal
55 {
56
57 template<typename Node, typename TreePath>
58 void pre(Node& node, TreePath)
59 {
60 node.setOffset(offset_);
61 }
62
63 template<typename Node, typename TreePath>
64 void post(Node& node, TreePath)
65 {
66 node.setSize(offset_ - node.offset());
67 }
68
69 template<typename Node, typename TreePath>
70 void leaf(Node& node, TreePath)
71 {
72 node.setOffset(offset_);
73 node.bind(entity_);
74 offset_ += node.size();
75 }
76
77 BindVisitor(const Entity& entity, std::size_t offset = 0)
78 : entity_(entity)
79 , offset_(offset)
80 {}
81
82 const Entity& entity_;
83 std::size_t offset_;
84
85 };
86
87
88 struct InitializeTreeVisitor :
89 public TypeTree::TreeVisitor,
90 public TypeTree::DynamicTraversal
91 {
92 template<typename Node, typename TreePath>
93 void pre(Node& node, TreePath)
94 {
95 node.setTreeIndex(treeIndex_);
96 ++treeIndex_;
97 }
98
99 template<typename Node, typename TreePath>
100 void leaf(Node& node, TreePath)
101 {
102 node.setTreeIndex(treeIndex_);
103 ++treeIndex_;
104 }
105
106 InitializeTreeVisitor(std::size_t treeIndexOffset = 0) :
107 treeIndex_(treeIndexOffset)
108 {}
109
110 std::size_t treeIndex_;
111 };
112
113 } // end namespace Impl
114
115
116 class BasisNodeMixin
117 {
118
119 friend struct Impl::ClearSizeVisitor;
120
121 template<typename>
122 friend struct Impl::BindVisitor;
123
124 friend struct Impl::InitializeTreeVisitor;
125
126 public:
127
128 using size_type = std::size_t;
129
130 BasisNodeMixin() :
131 offset_(0),
132 size_(0),
133 treeIndex_(0)
134 {}
135
136 size_type localIndex(size_type i) const
137 {
138 assert(i < size_);
139 return offset_ + i;
140 }
141
142 size_type size() const
143 {
144 return size_;
145 }
146
147 size_type treeIndex() const
148 {
149 return treeIndex_;
150 }
151
152 protected:
153
154 size_type offset() const
155 {
156 return offset_;
157 }
158
159 void setOffset(const size_type offset)
160 {
161 offset_ = offset;
162 }
163
164 void setSize(const size_type size)
165 {
166 size_ = size;
167 }
168
169 void setTreeIndex(size_type treeIndex)
170 {
171 treeIndex_ = treeIndex;
172 }
173
174 private:
175
176 size_type offset_;
177 size_type size_;
178 size_type treeIndex_;
179
180 };
181
182
183 class LeafBasisNode :
184 public BasisNodeMixin,
185 public TypeTree::LeafNode
186 {};
187
188
189 template<typename T, std::size_t n>
190 class PowerBasisNode :
191 public BasisNodeMixin,
192 public TypeTree::PowerNode<T,n>
193 {
194
195 using Node = TypeTree::PowerNode<T,n>;
196
197 public:
198
199 using Element = typename T::Element;
200
201 PowerBasisNode() = default;
202
203 PowerBasisNode(const typename Node::NodeStorage& children) :
204 Node(children)
205 {}
206
207 const Element& element() const
208 {
209 return this->child(Dune::Indices::_0).element();
210 }
211
212 };
213
214
215 template<typename... T>
216 class CompositeBasisNode :
217 public BasisNodeMixin,
218 public TypeTree::CompositeNode<T...>
219 {
220
221 using Node = TypeTree::CompositeNode<T...>;
222
223 public:
224
225 using Element = typename Node::template Child<0>::Type::Element;
226
227 CompositeBasisNode() = default;
228
229 CompositeBasisNode(const typename Node::NodeStorage& children) :
230 Node(children)
231 {}
232
233 template<typename... Children>
234 CompositeBasisNode(const std::shared_ptr<Children>&... children) :
235 Node(children...)
236 {}
237
238 const Element& element() const
239 {
240 return this->child(Dune::Indices::_0).element();
241 }
242
243 };
244
245
246 template<typename Tree>
247 void clearSize(Tree& tree, std::size_t offset)
248 {
249 TypeTree::applyToTree(tree,Impl::ClearSizeVisitor(offset));
250 }
251
252 template<typename Tree, typename Entity>
253 void bindTree(Tree& tree, const Entity& entity, std::size_t offset = 0)
254 {
255 Impl::BindVisitor<Entity> visitor(entity,offset);
256 TypeTree::applyToTree(tree,visitor);
257 }
258
259 template<typename Tree>
260 void initializeTree(Tree& tree, std::size_t treeIndexOffset = 0)
261 {
262 Impl::InitializeTreeVisitor visitor(treeIndexOffset);
263 TypeTree::applyToTree(tree,visitor);
264 }
265
266
267 } // namespace Functions
268
269} // namespace Dune
270
271#endif // DUNE_FUNCTIONS_FUNCTIONSPACEBASES_NODES_HH
std::tuple< std::shared_ptr< Children >... > NodeStorage
The type used for storing the children.
Definition: compositenode.hh:34
std::array< std::shared_ptr< T >, k > NodeStorage
The type used for storing the children.
Definition: powernode.hh:79
constexpr index_constant< 0 > _0
Compile time index with value 0.
Definition: indices.hh:53
constexpr HybridTreePath< T... > treePath(const T &... t)
Constructs a new HybridTreePath from the given indices.
Definition: treepath.hh:191
void applyToTree(Tree &&tree, Visitor &&visitor)
Apply visitor to TypeTree.
Definition: traversal.hh:237
ImplementationDefined child(Node &&node, Indices... indices)
Extracts the child of a node given by a sequence of compile-time and run-time indices.
Definition: childextraction.hh:126
Dune namespace.
Definition: alignedallocator.hh:13
void post(T &&, TreePath) const
Method for postfix tree traversal.
Definition: visitor.hh:81
void leaf(T &&, TreePath) const
Method for leaf traversal.
Definition: visitor.hh:91
void pre(T &&, TreePath) const
Method for prefix tree traversal.
Definition: visitor.hh:58
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)