dune-functions  2.5.0
treedata.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_COMMON_TREEDATA_HH
4 #define DUNE_FUNCTIONS_COMMON_TREEDATA_HH
5 
6 #include <memory>
7 
8 #include <dune/common/shared_ptr.hh>
9 
12 
13 namespace Dune {
14 namespace Functions {
15 
27 template<class SimpleNodeVisitorImp, bool leafOnly>
29  public TypeTree::TreeVisitor,
30  public TypeTree::DynamicTraversal
31 {
32  // This is only enabled, if we want to incorporate inner nodes.
33  // Checking leafOnly would be sufficient, but for SFINAE the
34  // the enable_if condition must depend on the template parameter.
35  template<typename Node, typename TreePath,
36  typename std::enable_if<(not leafOnly) and (not Node::isLeaf), int>::type = 0>
37  void pre(Node& node, TreePath treePath)
38  {
39  static_cast<SimpleNodeVisitorImp*>(this)->apply(node, treePath);
40  }
41 
42  template<typename Node, typename TreePath,
43  typename std::enable_if<(leafOnly) and (not Node::isLeaf), int>::type = 0>
44  void pre(Node& node, TreePath treePath)
45  {}
46 
47  template<typename Node, typename TreePath>
48  void leaf(Node& node, TreePath treePath)
49  {
50  static_cast<SimpleNodeVisitorImp*>(this)->apply(node, treePath);
51  }
52 };
53 
54 
55 
80 template<class T, template<class> class ND, bool LO>
81 class TreeData
82 {
83 
84 public:
85 
87  using Tree = T;
88 
90  using size_type = typename Tree::size_type;
91 
93  static const bool leafOnly = LO;
94 
96  template<class Node>
97  using NodeData = ND<Node>;
98 
99 protected:
100  using RawContainer = std::vector<void*>;
101 
102 
103  // Since we can generate the node data type only if
104  // we know the type of the node, we have to do
105  // initialization, copy, and destruction via a
106  // tree traversal. Once we can use C++14 this can
107  // be written in a much easier and more selfcontained
108  // ways using generic lambda functions.
109  // Until then we need explicite visitor classes for
110  // each operation.
111 
112  struct InitVisitor :
113  public UniformNodeVisitor<InitVisitor, leafOnly>
114  {
116  data_(data)
117  {}
118 
119  template<typename Node, typename TreePath>
120  void apply(Node& node, TreePath treePath)
121  {
122  auto&& index = node.treeIndex();
123  if (data_.size() < index+1)
124  data_.resize(index+1, nullptr);
125  data_[index] = new NodeData<Node>;
126  }
127 
128 
130  };
131 
132  struct DestroyVisitor :
133  public UniformNodeVisitor<DestroyVisitor, leafOnly>
134  {
136  data_(data)
137  {}
138 
139  template<typename Node, typename TreePath>
140  void apply(Node& node, TreePath treePath)
141  {
142  auto&& index = node.treeIndex();
143  auto p = (NodeData<Node>*)(data_[index]);
144  delete p;
145  data_[index] = nullptr;
146  }
147 
149  };
150 
151  struct CopyVisitor :
152  public UniformNodeVisitor<CopyVisitor, leafOnly>
153  {
154  CopyVisitor(TreeData& thisTD, const TreeData& otherTD) :
155  thisTD_(thisTD),
156  otherTD_(otherTD)
157  {}
158 
159  template<typename Node, typename TreePath>
160  void apply(Node& node, TreePath treePath)
161  {
162  thisTD_[node] = otherTD_[node];
163  }
164 
167  };
168 
169 public:
170 
173  tree_(nullptr)
174  {}
175 
183  void init(const Tree& tree)
184  {
185  if (tree_)
186  destroy();
187  tree_ = &tree;
188  TypeTree::applyToTree(*tree_, InitVisitor(data_));
189  }
190 
192  TreeData(const TreeData& other) :
193  tree_(other.tree_)
194  {
195  TypeTree::applyToTree(*tree_, InitVisitor(data_));
196  TypeTree::applyToTree(*tree_, CopyVisitor(*this, other));
197  }
198 
200  TreeData& operator=(const TreeData& other)
201  {
202  if (tree_)
203  TypeTree::applyToTree(*tree_, DestroyVisitor(data_));
204  tree_ = other.tree_;
205  TypeTree::applyToTree(*tree_, CopyVisitor(*this, other));
206  return *this;
207  }
208 
210  void destroy()
211  {
212  if (tree_)
213  TypeTree::applyToTree(*tree_, DestroyVisitor(data_));
214  tree_ = nullptr;
215  }
216 
219  {
220  if (tree_)
221  TypeTree::applyToTree(*tree_, DestroyVisitor(data_));
222  }
223 
225  template<class Node>
226  NodeData<Node>& operator[](const Node& node)
227  {
228  return *(NodeData<Node>*)(data_[node.treeIndex()]);
229  }
230 
232  template<class Node>
233  const NodeData<Node>& operator[](const Node& node) const
234  {
235  return *(NodeData<Node>*)(data_[node.treeIndex()]);
236  }
237 
238 protected:
239 
240  const Tree* tree_;
242 };
243 
244 
245 
246 } // namespace Functions
247 } // namespace Dune
248 
249 #endif // DUNE_FUNCTIONS_COMMON_TREEDATA_HH
Definition: treedata.hh:112
CopyVisitor(TreeData &thisTD, const TreeData &otherTD)
Definition: treedata.hh:154
std::vector< void *> RawContainer
Definition: treedata.hh:100
NodeData< Node > NodeData
Template to determine the data type for given node type.
Definition: treedata.hh:97
Definition: treedata.hh:151
TreeData()
Default constructor.
Definition: treedata.hh:172
InitVisitor(RawContainer &data)
Definition: treedata.hh:115
Definition: polynomial.hh:7
const NodeData< Node > & operator[](const Node &node) const
Get reference to data associated to given node.
Definition: treedata.hh:233
RawContainer data_
Definition: treedata.hh:241
SubTree Tree
Type of tree the data is associated with.
Definition: treedata.hh:87
const TreeData & otherTD_
Definition: treedata.hh:166
RawContainer & data_
Definition: treedata.hh:148
void init(const Tree &tree)
Initialize from tree.
Definition: treedata.hh:183
TreeData & thisTD_
Definition: treedata.hh:165
void apply(Node &node, TreePath treePath)
Definition: treedata.hh:140
void pre(Node &node, TreePath treePath)
Definition: treedata.hh:37
~TreeData()
Destructor.
Definition: treedata.hh:218
const Tree * tree_
Definition: treedata.hh:240
TreeData & operator=(const TreeData &other)
Copy assignment.
Definition: treedata.hh:200
void apply(Node &node, TreePath treePath)
Definition: treedata.hh:160
Container allowing to attach data to each node of a tree.
Definition: treedata.hh:81
NodeData< Node > & operator[](const Node &node)
Get mutable reference to data associated to given node.
Definition: treedata.hh:226
RawContainer & data_
Definition: treedata.hh:129
DestroyVisitor(RawContainer &data)
Definition: treedata.hh:135
void leaf(Node &node, TreePath treePath)
Definition: treedata.hh:48
void destroy()
Destroy data.
Definition: treedata.hh:210
void apply(Node &node, TreePath treePath)
Definition: treedata.hh:120
typename Tree::size_type size_type
Type used for indices and size information.
Definition: treedata.hh:90
Mixin for visitors that should apply the same action on all nodes.
Definition: treedata.hh:28
TreeData(const TreeData &other)
Copy constructor.
Definition: treedata.hh:192