dune-functions  2.5.0
nodes.hh
Go to the documentation of this file.
1 #ifndef DUNE_FUNCTIONS_FUNCTIONSPACEBASES_NODES_HH
2 #define DUNE_FUNCTIONS_FUNCTIONSPACEBASES_NODES_HH
3 
4 #include <dune/typetree/leafnode.hh>
5 #include <dune/typetree/powernode.hh>
6 #include <dune/typetree/compositenode.hh>
7 #include <dune/typetree/traversal.hh>
8 #include <dune/typetree/visitor.hh>
9 
10 namespace Dune {
11  namespace Functions {
12 
13 
14  namespace {
15 
16 
17  template<typename size_type>
18  struct ClearSizeVisitor
19  : public TypeTree::TreeVisitor
20  , public TypeTree::DynamicTraversal
21  {
22 
23  template<typename Node, typename TreePath>
24  void pre(Node& node, TreePath treePath)
25  {
26  leaf(node,treePath);
27  node.setSize(0);
28  }
29 
30  template<typename Node, typename TreePath>
31  void leaf(Node& node, TreePath treePath)
32  {
33  node.setOffset(offset_);
34  }
35 
36  ClearSizeVisitor(size_type offset)
37  : offset_(offset)
38  {}
39 
40  const size_type offset_;
41 
42  };
43 
44 
45  template<typename Entity, typename size_type>
46  struct BindVisitor
47  : public TypeTree::TreeVisitor
48  , public TypeTree::DynamicTraversal
49  {
50 
51  template<typename Node, typename TreePath>
52  void pre(Node& node, TreePath treePath)
53  {
54  node.setOffset(offset_);
55  }
56 
57  template<typename Node, typename TreePath>
58  void post(Node& node, TreePath treePath)
59  {
60  node.setSize(offset_ - node.offset());
61  }
62 
63  template<typename Node, typename TreePath>
64  void leaf(Node& node, TreePath treePath)
65  {
66  node.setOffset(offset_);
67  node.bind(entity_);
68  offset_ += node.size();
69  }
70 
71  BindVisitor(const Entity& entity, size_type offset = 0)
72  : entity_(entity)
73  , offset_(offset)
74  {}
75 
76  const Entity& entity_;
77  size_type offset_;
78 
79  };
80 
81 
82  template<typename size_type>
83  struct InitializeTreeVisitor :
84  public TypeTree::TreeVisitor,
85  public TypeTree::DynamicTraversal
86  {
87  template<typename Node, typename TreePath>
88  void pre(Node& node, TreePath treePath)
89  {
90  node.setTreeIndex(treeIndex_);
91  ++treeIndex_;
92  }
93 
94  template<typename Node, typename TreePath>
95  void leaf(Node& node, TreePath treePath)
96  {
97  node.setTreeIndex(treeIndex_);
98  ++treeIndex_;
99  }
100 
101  InitializeTreeVisitor(size_type treeIndexOffset = 0) :
102  treeIndex_(treeIndexOffset)
103  {}
104 
105  size_type treeIndex_;
106  };
107 
108  }
109 
110 
111  template<typename TP>
113  {
114 
115  template<typename>
116  friend struct ClearSizeVisitor;
117 
118  template<typename,typename>
119  friend struct BindVisitor;
120 
121  template<typename>
122  friend struct InitializeTreeVisitor;
123 
124  public:
125 
126  using TreePath = TP;
127  using size_type = std::size_t;
128 
129  BasisNodeMixin(const TreePath& treePath) :
130  offset_(0),
131  size_(0),
132  treePath_(treePath),
133  treeIndex_(0)
134  {}
135 
137  {
138  return offset_ + i;
139  }
140 
141  size_type size() const
142  {
143  return size_;
144  }
145 
146  const TreePath& treePath() const
147  {
148  return treePath_;
149  }
150 
151  const size_type treeIndex() const
152  {
153  return treeIndex_;
154  }
155 
157  {
158  return offset_;
159  }
160 
161  protected:
162 
163  void setOffset(const size_type offset)
164  {
165  offset_ = offset;
166  }
167 
168  void setSize(const size_type size)
169  {
170  size_ = size;
171  }
172 
173  void setTreeIndex(const size_type treeIndex)
174  {
175  treeIndex_ = treeIndex;
176  }
177 
178  private:
179 
181  size_type size_;
182  const TreePath treePath_;
184 
185  };
186 
187 
188  template<typename SIZE_T_DUMMY, typename TP>
190  public BasisNodeMixin<TP>,
191  public TypeTree::LeafNode
192  {
193 
194  using Mixin = BasisNodeMixin<TP>;
195 
196  public:
197 
198  using TreePath = TP;
199  using size_type = std::size_t;
200 
202  Mixin(treePath)
203  {}
204 
205  };
206 
207 
208  template<typename SIZE_T_DUMMY, typename TP, typename T, std::size_t n>
210  public BasisNodeMixin<TP>,
211  public TypeTree::PowerNode<T,n>
212  {
213 
214  using Mixin = BasisNodeMixin<TP>;
215  using Node = TypeTree::PowerNode<T,n>;
216 
217  public:
218 
219  PowerBasisNode(const TP& tp) :
220  Mixin(tp)
221  {}
222 
223  PowerBasisNode(const TP& tp, const typename Node::NodeStorage& children) :
224  Mixin(tp),
225  Node(children)
226  {}
227 
228  };
229 
230 
231  template<typename SIZE_T_DUMMY, typename TP, typename... T>
233  public BasisNodeMixin<TP>,
234  public TypeTree::CompositeNode<T...>
235  {
236 
237  using Mixin = BasisNodeMixin<TP>;
238  using Node = TypeTree::CompositeNode<T...>;
239 
240  public:
241 
242  CompositeBasisNode(const TP& tp)
243  : Mixin(tp)
244  {}
245 
246  CompositeBasisNode(const TP& tp, const typename Node::NodeStorage& children) :
247  Mixin(tp),
248  Node(children)
249  {}
250 
251  template<typename... Children>
252  CompositeBasisNode(const shared_ptr<Children>&... children, const TP& tp)
253  : Mixin(tp)
254  , Node(children...)
255  {}
256 
257  };
258 
259 
260  template<typename Tree>
261  void clearSize(Tree& tree, std::size_t offset)
262  {
263  TypeTree::applyToTree(tree,ClearSizeVisitor<std::size_t>(offset));
264  }
265 
266  template<typename Tree, typename Entity>
267  void bindTree(Tree& tree, const Entity& entity, std::size_t offset = 0)
268  {
269  BindVisitor<Entity,std::size_t> visitor(entity,offset);
270  TypeTree::applyToTree(tree,visitor);
271  }
272 
273  template<typename Tree>
274  void initializeTree(Tree& tree, std::size_t treeIndexOffset = 0)
275  {
276  InitializeTreeVisitor<std::size_t> visitor(treeIndexOffset);
277  TypeTree::applyToTree(tree,visitor);
278  }
279 
280 
281  } // namespace Functions
282 
283 } // namespace Dune
284 
285 #endif // DUNE_FUNCTIONS_FUNCTIONSPACEBASES_NODES_HH
Definition: nodes.hh:209
const size_type offset_
Definition: nodes.hh:40
const Entity & entity_
Definition: nodes.hh:76
CompositeBasisNode(const shared_ptr< Children > &... children, const TP &tp)
Definition: nodes.hh:252
std::size_t size_type
Definition: nodes.hh:127
void setSize(const size_type size)
Definition: nodes.hh:168
void initializeTree(Tree &tree, std::size_t treeIndexOffset=0)
Definition: nodes.hh:274
void bindTree(Tree &tree, const Entity &entity, std::size_t offset=0)
Definition: nodes.hh:267
Definition: polynomial.hh:7
Definition: nodes.hh:189
void setOffset(const size_type offset)
Definition: nodes.hh:163
CompositeBasisNode(const TP &tp, const typename Node::NodeStorage &children)
Definition: nodes.hh:246
BasisNodeMixin(const TreePath &treePath)
Definition: nodes.hh:129
size_type size() const
Definition: nodes.hh:141
size_type localIndex(size_type i) const
Definition: nodes.hh:136
void setTreeIndex(const size_type treeIndex)
Definition: nodes.hh:173
Definition: nodes.hh:232
CompositeBasisNode(const TP &tp)
Definition: nodes.hh:242
const size_type treeIndex() const
Definition: nodes.hh:151
size_type treeIndex_
Definition: nodes.hh:105
PowerBasisNode(const TP &tp, const typename Node::NodeStorage &children)
Definition: nodes.hh:223
TP TreePath
Definition: nodes.hh:126
size_type offset() const
Definition: nodes.hh:156
const TreePath & treePath() const
Definition: nodes.hh:146
LeafBasisNode(TreePath treePath=TreePath())
Definition: nodes.hh:201
void clearSize(Tree &tree, std::size_t offset)
Definition: nodes.hh:261
PowerBasisNode(const TP &tp)
Definition: nodes.hh:219
Definition: nodes.hh:112