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