Dune Core Modules (2.9.0)

treedata.hh
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
12
13#include <dune/typetree/pairtraversal.hh>
14
15#include <dune/functions/gridfunctions/gridviewentityset.hh>
16#include <dune/functions/gridfunctions/gridfunction.hh>
17
18namespace Dune {
19namespace Functions {
20
34template<class SimpleNodeVisitorImp, bool leafOnly>
35struct
36[[deprecated("This is an implementation detail of the deprecated class TreeDate and thus deprecated itself.")]]
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
91template<class T, template<class> class ND, bool LO>
92class
93[[deprecated("This class is deprecated. Please use TreeContainer from dune-typetree instead.")]]
95{
96
97public:
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
112protected:
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 {
128 InitVisitor(RawContainer& data) :
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
142 RawContainer& data_;
143 };
144
145 struct DestroyVisitor :
146 public UniformNodeVisitor<DestroyVisitor, leafOnly>
147 {
148 DestroyVisitor(RawContainer& data) :
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
161 RawContainer& data_;
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
178 TreeData& thisTD_;
179 const TreeData& otherTD_;
180 };
181
182public:
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
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
251protected:
252
253 const Tree* tree_;
254 RawContainer data_;
255};
256
257
258
259} // namespace Functions
260} // namespace Dune
261
262#endif // DUNE_FUNCTIONS_COMMON_TREEDATA_HH
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
void destroy()
Destroy data.
Definition: treedata.hh:223
const NodeData< Node > & operator[](const Node &node) const
Get reference to data associated to given node.
Definition: treedata.hh:246
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
NodeData< Node > & operator[](const Node &node)
Get mutable reference to data associated to given node.
Definition: treedata.hh:239
typename Tree::size_type size_type
Type used for indices and size information.
Definition: treedata.hh:103
constexpr HybridTreePath< T... > treePath(const T &... t)
Constructs a new HybridTreePath from the given indices.
Definition: treepath.hh:191
void applyToTree(Tree &&tree, Visitor &&visitor)
Apply visitor to TypeTree.
Definition: traversal.hh:237
Dune namespace.
Definition: alignedallocator.hh:13
This file implements several utilities related to std::shared_ptr.
Mixin for visitors that should apply the same action on all nodes.
Definition: treedata.hh:40
Mixin base class for visitors that only need a dynamic TreePath during traversal.
Definition: visitor.hh:424
Convenience base class for visiting the entire tree.
Definition: visitor.hh:433
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)