dune-functions  2.9.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 
7 #warning This file is deprecated. Please use TreeContainer from dune-typetree instead.
8 
9 #include <memory>
10 
11 #include <dune/common/shared_ptr.hh>
12 
13 #include <dune/typetree/pairtraversal.hh>
14 
17 
18 namespace Dune {
19 namespace Functions {
20 
34 template<class SimpleNodeVisitorImp, bool leafOnly>
35 struct
36 [[deprecated("This is an implementation detail of the deprecated class TreeDate and thus deprecated itself.")]]
38  public TypeTree::TreeVisitor,
39  public TypeTree::DynamicTraversal
40 {
41  // This is only enabled, if we want to incorporate inner nodes.
42  // Checking leafOnly would be sufficient, but for SFINAE the
43  // the enable_if condition must depend on the template parameter.
44  template<typename Node, typename TreePath,
45  typename std::enable_if<(not leafOnly) and (not Node::isLeaf), int>::type = 0>
46  void pre(Node& node, TreePath treePath)
47  {
48  static_cast<SimpleNodeVisitorImp*>(this)->apply(node, treePath);
49  }
50 
51  template<typename Node, typename TreePath,
52  typename std::enable_if<(leafOnly) and (not Node::isLeaf), int>::type = 0>
53  void pre(Node& node, TreePath treePath)
54  {}
55 
56  template<typename Node, typename TreePath>
57  void leaf(Node& node, TreePath treePath)
58  {
59  static_cast<SimpleNodeVisitorImp*>(this)->apply(node, treePath);
60  }
61 };
62 
63 
64 
91 template<class T, template<class> class ND, bool LO>
92 class
93 [[deprecated("This class is deprecated. Please use TreeContainer from dune-typetree instead.")]]
95 {
96 
97 public:
98 
100  using Tree = T;
101 
103  using size_type = typename Tree::size_type;
104 
106  static const bool leafOnly = LO;
107 
109  template<class Node>
110  using NodeData = ND<Node>;
111 
112 protected:
113  using RawContainer = std::vector<void*>;
114 
115 
116  // Since we can generate the node data type only if
117  // we know the type of the node, we have to do
118  // initialization, copy, and destruction via a
119  // tree traversal. Once we can use C++14 this can
120  // be written in a much easier and more selfcontained
121  // ways using generic lambda functions.
122  // Until then we need explicit visitor classes for
123  // each operation.
124 
125  struct InitVisitor :
126  public UniformNodeVisitor<InitVisitor, leafOnly>
127  {
129  data_(data)
130  {}
131 
132  template<typename Node, typename TreePath>
133  void apply(Node& node, TreePath treePath)
134  {
135  auto&& index = node.treeIndex();
136  if (data_.size() < index+1)
137  data_.resize(index+1, nullptr);
138  data_[index] = new NodeData<Node>;
139  }
140 
141 
143  };
144 
145  struct DestroyVisitor :
146  public UniformNodeVisitor<DestroyVisitor, leafOnly>
147  {
149  data_(data)
150  {}
151 
152  template<typename Node, typename TreePath>
153  void apply(Node& node, TreePath treePath)
154  {
155  auto&& index = node.treeIndex();
156  auto p = (NodeData<Node>*)(data_[index]);
157  delete p;
158  data_[index] = nullptr;
159  }
160 
162  };
163 
164  struct CopyVisitor :
165  public UniformNodeVisitor<CopyVisitor, leafOnly>
166  {
167  CopyVisitor(TreeData& thisTD, const TreeData& otherTD) :
168  thisTD_(thisTD),
169  otherTD_(otherTD)
170  {}
171 
172  template<typename Node, typename TreePath>
173  void apply(Node& node, TreePath treePath)
174  {
175  thisTD_[node] = otherTD_[node];
176  }
177 
180  };
181 
182 public:
183 
186  tree_(nullptr)
187  {}
188 
196  void init(const Tree& tree)
197  {
198  if (tree_)
199  destroy();
200  tree_ = &tree;
201  TypeTree::applyToTree(*tree_, InitVisitor(data_));
202  }
203 
205  TreeData(const TreeData& other) :
206  tree_(other.tree_)
207  {
208  TypeTree::applyToTree(*tree_, InitVisitor(data_));
209  TypeTree::applyToTree(*tree_, CopyVisitor(*this, other));
210  }
211 
213  TreeData& operator=(const TreeData& other)
214  {
215  if (tree_)
216  TypeTree::applyToTree(*tree_, DestroyVisitor(data_));
217  tree_ = other.tree_;
218  TypeTree::applyToTree(*tree_, CopyVisitor(*this, other));
219  return *this;
220  }
221 
223  void destroy()
224  {
225  if (tree_)
226  TypeTree::applyToTree(*tree_, DestroyVisitor(data_));
227  tree_ = nullptr;
228  }
229 
232  {
233  if (tree_)
234  TypeTree::applyToTree(*tree_, DestroyVisitor(data_));
235  }
236 
238  template<class Node>
239  NodeData<Node>& operator[](const Node& node)
240  {
241  return *(NodeData<Node>*)(data_[node.treeIndex()]);
242  }
243 
245  template<class Node>
246  const NodeData<Node>& operator[](const Node& node) const
247  {
248  return *(NodeData<Node>*)(data_[node.treeIndex()]);
249  }
250 
251 protected:
252 
253  const Tree* tree_;
255 };
256 
257 
258 
259 } // namespace Functions
260 } // namespace Dune
261 
262 #endif // DUNE_FUNCTIONS_COMMON_TREEDATA_HH
Definition: polynomial.hh:10
Mixin for visitors that should apply the same action on all nodes.
Definition: treedata.hh:40
void pre(Node &node, TreePath treePath)
Definition: treedata.hh:46
void leaf(Node &node, TreePath treePath)
Definition: treedata.hh:57
Container allowing to attach data to each node of a tree.
Definition: treedata.hh:95
TreeData(const TreeData &other)
Copy constructor.
Definition: treedata.hh:205
void init(const Tree &tree)
Initialize from tree.
Definition: treedata.hh:196
T Tree
Type of tree the data is associated with.
Definition: treedata.hh:100
const Tree * tree_
Definition: treedata.hh:253
void destroy()
Destroy data.
Definition: treedata.hh:223
TreeData & operator=(const TreeData &other)
Copy assignment.
Definition: treedata.hh:213
~TreeData()
Destructor.
Definition: treedata.hh:231
ND< Node > NodeData
Template to determine the data type for given node type.
Definition: treedata.hh:110
TreeData()
Default constructor.
Definition: treedata.hh:185
std::vector< void * > RawContainer
Definition: treedata.hh:113
typename Tree::size_type size_type
Type used for indices and size information.
Definition: treedata.hh:103
NodeData< Node > & operator[](const Node &node)
Get mutable reference to data associated to given node.
Definition: treedata.hh:239
const NodeData< Node > & operator[](const Node &node) const
Get reference to data associated to given node.
Definition: treedata.hh:246
RawContainer data_
Definition: treedata.hh:254
Definition: treedata.hh:127
InitVisitor(RawContainer &data)
Definition: treedata.hh:128
void apply(Node &node, TreePath treePath)
Definition: treedata.hh:133
RawContainer & data_
Definition: treedata.hh:142
RawContainer & data_
Definition: treedata.hh:161
DestroyVisitor(RawContainer &data)
Definition: treedata.hh:148
void apply(Node &node, TreePath treePath)
Definition: treedata.hh:153
Definition: treedata.hh:166
CopyVisitor(TreeData &thisTD, const TreeData &otherTD)
Definition: treedata.hh:167
void apply(Node &node, TreePath treePath)
Definition: treedata.hh:173
TreeData & thisTD_
Definition: treedata.hh:178
const TreeData & otherTD_
Definition: treedata.hh:179