Dune Core Modules (2.8.0)

visitor.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3
4#ifndef DUNE_TYPETREE_VISITOR_HH
5#define DUNE_TYPETREE_VISITOR_HH
6
7#include <dune/typetree/treepath.hh>
8#include <dune/typetree/utility.hh>
9
10namespace Dune {
11 namespace TypeTree {
12
19
47 {
48
50
57 template<typename T, typename TreePath>
58 void pre(T&& t, TreePath treePath) const {}
59
61
69 template<typename T, typename TreePath>
70 void in(T&& t, TreePath treePath) const {}
71
73
80 template<typename T, typename TreePath>
81 void post(T&& t, TreePath treePath) const {}
82
84
90 template<typename T, typename TreePath>
91 void leaf(T&& t, TreePath treePath) const {}
92
94
104 template<typename T, typename Child, typename TreePath, typename ChildIndex>
105 void beforeChild(T&& t, Child&& child, TreePath treePath, ChildIndex childIndex) const {}
106
108
119 template<typename T, typename Child, typename TreePath, typename ChildIndex>
120 void afterChild(T&& t, Child&& child, TreePath treePath, ChildIndex childIndex) const {}
121
122 };
123
124
126
162 {
163
165
173 template<typename T1, typename T2, typename TreePath>
174 void pre(T1&& t1, T2&& t2, TreePath treePath) const {}
175
177
186 template<typename T1, typename T2, typename TreePath>
187 void in(T1&& t1, T2&& t2, TreePath treePath) const {}
188
190
198 template<typename T1, typename T2, typename TreePath>
199 void post(T1&& t1, T2&& t2, TreePath treePath) const {}
200
202
213 template<typename T1, typename T2, typename TreePath>
214 void leaf(T1&& t1, T2&& t2, TreePath treePath) const {}
215
217
229 template<typename T1, typename Child1, typename T2, typename Child2, typename TreePath, typename ChildIndex>
230 void beforeChild(T1&& t1, Child1&& child1, T2&& t2, Child2&& child2, TreePath treePath, ChildIndex childIndex) const {}
231
233
245 template<typename T1, typename Child1, typename T2, typename Child2, typename TreePath, typename ChildIndex>
246 void afterChild(T1&& t1, Child1&& child1, T2&& t2, Child2&& child2, TreePath treePath, ChildIndex childIndex) const {}
247
248 };
249
250
251 namespace Experimental {
252
284 {
285
293 template<typename T, typename TreePath, typename U>
294 auto pre(T&& t, TreePath treePath, const U& u) const { return u;}
295
303 template<typename T, typename TreePath, typename U>
304 auto in(T&& t, TreePath treePath, const U& u) const {return u;}
305
313 template<typename T, typename TreePath, typename U>
314 auto post(T&& t, TreePath treePath, const U& u) const {return u;}
315
323 template<typename T, typename TreePath, typename U>
324 auto leaf(T&& t, TreePath treePath, const U& u) const { return u;}
325
333 template<typename T, typename Child, typename TreePath, typename ChildIndex, typename U>
334 auto beforeChild(T&& t, Child&& child, TreePath treePath, ChildIndex childIndex, const U& u) const {return u;}
335
343 template<typename T, typename Child, typename TreePath, typename ChildIndex, typename U>
344 auto afterChild(T&& t, Child&& child, TreePath treePath, ChildIndex childIndex, const U& u) const {return u;}
345
346 };
347 } // namespace Experimental
348
350
356 {
357
358 // the little trick with the default template arguments
359 // makes the class usable for both single-tree visitors
360 // and visitors for pairs of trees
362 template<typename Node1,
363 typename Child1,
364 typename Node2,
365 typename Child2 = void,
366 typename TreePath = void>
368 {
370 static const bool value = false;
371 };
372
373 };
374
375
377
382 {
383
384 // the little trick with the default template arguments
385 // makes the class usable for both single-tree visitors
386 // and visitors for pairs of trees
388 template<typename Node1,
389 typename Child1,
390 typename Node2,
391 typename Child2 = void,
392 typename TreePath = void>
394 {
396 static const bool value = true;
397 };
398
399 };
400
402
410 {
412 static const TreePathType::Type treePathType = TreePathType::fullyStatic;
413 };
414
416
424 {
426 static const TreePathType::Type treePathType = TreePathType::dynamic;
427 };
428
431 : public DefaultVisitor
432 , public VisitTree
433 {};
434
437 : public DefaultVisitor
438 , public VisitDirectChildren
439 {};
440
443 : public DefaultPairVisitor
444 , public VisitTree
445 {};
446
449 : public DefaultPairVisitor
450 , public VisitDirectChildren
451 {};
452
453 namespace Experimental::Info {
454
455 struct LeafCounterVisitor
456 : public DefaultHybridVisitor
457 , public StaticTraversal
458 , public VisitTree
459 {
460 template<class Tree, class Child, class TreePath, class ChildIndex, class U>
461 auto beforeChild(Tree&&, Child&&, TreePath, ChildIndex, U u) const {
462 // in this case child index is an integral constant: forward u
463 return u;
464 }
465
466 template<class Tree, class Child, class TreePath, class U>
467 std::size_t beforeChild(Tree&&, Child&&, TreePath, std::size_t childIndex, U u) const {
468 // in this case child index is a run-time index: cast accumulated u to std::size_t
469 return std::size_t{u};
470 }
471
472 template<class Tree, class TreePath, class U>
473 auto leaf(Tree&&, TreePath, U u) const
474 {
475 return Hybrid::plus(u,Dune::Indices::_1);
476 }
477
478 };
479
480 struct NodeCounterVisitor
481 : public LeafCounterVisitor
482 {
483 template<typename Tree, typename TreePath, typename U>
484 auto pre(Tree&& tree, TreePath treePath, U u) const {
485 return Hybrid::plus(u,Indices::_1);
486 }
487 };
488
489 struct DepthVisitor
490 : public DefaultHybridVisitor
491 , public StaticTraversal
492 , public VisitTree
493 {
494 template<class Tree, class TreePath, class U>
495 auto leaf(Tree&&, TreePath, U u) const
496 {
497 auto path_size = index_constant<treePathSize(TreePath{})>{};
498 auto depth = Hybrid::plus(path_size,Indices::_1);
499 return Hybrid::max(depth,u);
500 }
501 };
502
504 // result is alwayas an integral constant
505 template<typename Tree>
506 auto depth(const Tree& tree)
507 {
508 return hybridApplyToTree(tree,DepthVisitor{},Indices::_0);
509 }
510
512 // return types is std::integral_constant.
513 template<typename Tree>
514 constexpr auto depth()
515 {
516 return decltype(hybridApplyToTree(std::declval<Tree>(),DepthVisitor{},Indices::_0)){};
517 }
518
520 // if Tree is dynamic, return type is std::size_t, otherwise std::integral_constant.
521 template<typename Tree>
522 auto nodeCount(const Tree& tree)
523 {
524 return hybridApplyToTree(tree,NodeCounterVisitor{},Indices::_0);
525 }
526
528 // if Tree is dynamic, return type is std::size_t, otherwise std::integral_constant.
529 template<typename Tree>
530 auto leafCount(const Tree& tree)
531 {
532 return hybridApplyToTree(tree,LeafCounterVisitor{},Dune::Indices::_0);
533 }
534
536 template<typename Tree>
537 constexpr bool isDynamic = std::is_same<std::size_t, decltype(leafCount(std::declval<Tree>()))>{};
538
539 } // namespace Experimental::Info
540
542
543 } // namespace TypeTree
544} //namespace Dune
545
546#endif // DUNE_TYPETREE_VISITOR_HH
A hybrid version of TreePath that supports both compile time and run time indices.
Definition: treepath.hh:79
constexpr index_constant< 0 > _0
Compile time index with value 0.
Definition: indices.hh:51
constexpr index_constant< 1 > _1
Compile time index with value 1.
Definition: indices.hh:54
std::integral_constant< std::size_t, i > index_constant
An index constant with value i.
Definition: indices.hh:28
auto max(ADLTag< 0 >, const V &v1, const V &v2)
implements binary Simd::max()
Definition: defaults.hh:79
constexpr std::size_t treePathSize(const HybridTreePath< T... > &)
Returns the size (number of components) of the given HybridTreePath.
Definition: treepath.hh:196
constexpr HybridTreePath< T... > treePath(const T &... t)
Constructs a new HybridTreePath from the given indices.
Definition: treepath.hh:188
typename impl::_Child< Node, indices... >::type Child
Template alias for the type of a child node given by a list of child indices.
Definition: childextraction.hh:223
ImplementationDefined child(Node &&node, Indices... indices)
Extracts the child of a node given by a sequence of compile-time and run-time indices.
Definition: childextraction.hh:126
Dune namespace.
Definition: alignedallocator.hh:11
Visitor interface and base class for visitors of pairs of TypeTrees.
Definition: visitor.hh:162
void pre(T1 &&t1, T2 &&t2, TreePath treePath) const
Method for prefix tree traversal.
Definition: visitor.hh:174
void leaf(T1 &&t1, T2 &&t2, TreePath treePath) const
Method for leaf traversal.
Definition: visitor.hh:214
void afterChild(T1 &&t1, Child1 &&child1, T2 &&t2, Child2 &&child2, TreePath treePath, ChildIndex childIndex) const
Method for child-parent traversal.
Definition: visitor.hh:246
void post(T1 &&t1, T2 &&t2, TreePath treePath) const
Method for postfix traversal.
Definition: visitor.hh:199
void beforeChild(T1 &&t1, Child1 &&child1, T2 &&t2, Child2 &&child2, TreePath treePath, ChildIndex childIndex) const
Method for parent-child traversal.
Definition: visitor.hh:230
void in(T1 &&t1, T2 &&t2, TreePath treePath) const
Method for infix tree traversal.
Definition: visitor.hh:187
Visitor interface and base class for TypeTree visitors.
Definition: visitor.hh:47
void in(T &&t, TreePath treePath) const
Method for infix tree traversal.
Definition: visitor.hh:70
void pre(T &&t, TreePath treePath) const
Method for prefix tree traversal.
Definition: visitor.hh:58
void post(T &&t, TreePath treePath) const
Method for postfix tree traversal.
Definition: visitor.hh:81
void beforeChild(T &&t, Child &&child, TreePath treePath, ChildIndex childIndex) const
Method for parent-child traversal.
Definition: visitor.hh:105
void leaf(T &&t, TreePath treePath) const
Method for leaf traversal.
Definition: visitor.hh:91
void afterChild(T &&t, Child &&child, TreePath treePath, ChildIndex childIndex) const
Method for child-parent traversal.
Definition: visitor.hh:120
Convenience base class for visiting the direct children of a node pair.
Definition: visitor.hh:451
Convenience base class for visiting the direct children of a node.
Definition: visitor.hh:439
Mixin base class for visitors that only need a dynamic TreePath during traversal.
Definition: visitor.hh:424
static const TreePathType::Type treePathType
Use the dynamic tree traversal algorithm.
Definition: visitor.hh:426
Hybrid visitor interface and base class for TypeTree hybrid visitors.
Definition: visitor.hh:284
auto post(T &&t, TreePath treePath, const U &u) const
Method for postfix tree traversal.
Definition: visitor.hh:314
auto afterChild(T &&t, Child &&child, TreePath treePath, ChildIndex childIndex, const U &u) const
Method for child-parent traversal.
Definition: visitor.hh:344
auto leaf(T &&t, TreePath treePath, const U &u) const
Method for leaf traversal.
Definition: visitor.hh:324
auto pre(T &&t, TreePath treePath, const U &u) const
Method for prefix tree traversal.
Definition: visitor.hh:294
auto beforeChild(T &&t, Child &&child, TreePath treePath, ChildIndex childIndex, const U &u) const
Method for parent-child traversal.
Definition: visitor.hh:334
auto in(T &&t, TreePath treePath, const U &u) const
Method for infix tree traversal.
Definition: visitor.hh:304
Mixin base class for visitors that require a static TreePath during traversal.
Definition: visitor.hh:410
static const TreePathType::Type treePathType
Use the static tree traversal algorithm.
Definition: visitor.hh:412
Convenience base class for visiting an entire tree pair.
Definition: visitor.hh:445
Convenience base class for visiting the entire tree.
Definition: visitor.hh:433
Template struct for determining whether or not to visit a given child.
Definition: visitor.hh:368
static const bool value
Do not visit any child.
Definition: visitor.hh:370
Mixin base class for visitors that only want to visit the direct children of a node.
Definition: visitor.hh:356
Template struct for determining whether or not to visit a given child.
Definition: visitor.hh:394
static const bool value
Visit any child.
Definition: visitor.hh:396
Mixin base class for visitors that want to visit the complete tree.
Definition: visitor.hh:382
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)