dune-functions  2.9.0
nodes.hh
Go to the documentation of this file.
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 
17 namespace 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 
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 
131  offset_(0),
132  size_(0),
133  treeIndex_(0)
134  {}
135 
137  {
138  assert(i < size_);
139  return offset_ + i;
140  }
141 
142  size_type size() const
143  {
144  return size_;
145  }
146 
148  {
149  return treeIndex_;
150  }
151 
152  protected:
153 
155  {
156  return offset_;
157  }
158 
160  {
161  offset_ = offset;
162  }
163 
164  void setSize(const size_type size)
165  {
166  size_ = size;
167  }
168 
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 
184  public BasisNodeMixin,
185  public TypeTree::LeafNode
186  {};
187 
188 
189  template<typename T, std::size_t n>
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>
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
Definition: polynomial.hh:10
void clearSize(Tree &tree, std::size_t offset)
Definition: nodes.hh:247
void bindTree(Tree &tree, const Entity &entity, std::size_t offset=0)
Definition: nodes.hh:253
void initializeTree(Tree &tree, std::size_t treeIndexOffset=0)
Definition: nodes.hh:260
Definition: nodes.hh:117
size_type treeIndex() const
Definition: nodes.hh:147
size_type localIndex(size_type i) const
Definition: nodes.hh:136
size_type offset() const
Definition: nodes.hh:154
size_type size() const
Definition: nodes.hh:142
void setOffset(const size_type offset)
Definition: nodes.hh:159
std::size_t size_type
Definition: nodes.hh:128
BasisNodeMixin()
Definition: nodes.hh:130
void setSize(const size_type size)
Definition: nodes.hh:164
void setTreeIndex(size_type treeIndex)
Definition: nodes.hh:169
Definition: nodes.hh:186
Definition: nodes.hh:193
typename T::Element Element
Definition: nodes.hh:199
PowerBasisNode(const typename Node::NodeStorage &children)
Definition: nodes.hh:203
const Element & element() const
Definition: nodes.hh:207
Definition: nodes.hh:219
CompositeBasisNode(const typename Node::NodeStorage &children)
Definition: nodes.hh:229
const Element & element() const
Definition: nodes.hh:238
typename Node::template Child< 0 >::Type::Element Element
Definition: nodes.hh:225
CompositeBasisNode(const std::shared_ptr< Children > &... children)
Definition: nodes.hh:234