Dune Core Modules (2.9.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 
10 namespace Dune {
11  namespace TypeTree {
12 
19 
47  {
48 
50 
57  template<typename T, typename TreePath>
58  void pre(T&&, TreePath) const {}
59 
61 
69  template<typename T, typename TreePath>
70  void in(T&&, TreePath) const {}
71 
73 
80  template<typename T, typename TreePath>
81  void post(T&&, TreePath) const {}
82 
84 
90  template<typename T, typename TreePath>
91  void leaf(T&&, TreePath) const {}
92 
94 
104  template<typename T, typename Child, typename TreePath, typename ChildIndex>
105  void beforeChild(T&&, Child&&, TreePath, ChildIndex) const {}
106 
108 
119  template<typename T, typename Child, typename TreePath, typename ChildIndex>
120  void afterChild(T&&, Child&&, TreePath, ChildIndex) const {}
121 
122  };
123 
124 
126 
162  {
163 
165 
173  template<typename T1, typename T2, typename TreePath>
174  void pre(T1&&, T2&&, TreePath) const {}
175 
177 
186  template<typename T1, typename T2, typename TreePath>
187  void in(T1&&, T2&&, TreePath) const {}
188 
190 
198  template<typename T1, typename T2, typename TreePath>
199  void post(T1&&, T2&&, TreePath) const {}
200 
202 
213  template<typename T1, typename T2, typename TreePath>
214  void leaf(T1&&, T2&&, TreePath) const {}
215 
217 
229  template<typename T1, typename Child1, typename T2, typename Child2, typename TreePath, typename ChildIndex>
230  void beforeChild(T1&&, Child1&&, T2&&, Child2&&, TreePath, ChildIndex) const {}
231 
233 
245  template<typename T1, typename Child1, typename T2, typename Child2, typename TreePath, typename ChildIndex>
246  void afterChild(T1&&, Child1&&, T2&&, Child2&&, TreePath, ChildIndex) const {}
247 
248  };
249 
250 
251  namespace Experimental {
252 
284  {
285 
293  template<typename T, typename TreePath, typename U>
294  auto pre(T&&, TreePath, const U& u) const { return u;}
295 
303  template<typename T, typename TreePath, typename U>
304  auto in(T&&, TreePath, const U& u) const {return u;}
305 
313  template<typename T, typename TreePath, typename U>
314  auto post(T&&, TreePath, const U& u) const {return u;}
315 
323  template<typename T, typename TreePath, typename U>
324  auto leaf(T&&, TreePath, const U& u) const { return u;}
325 
333  template<typename T, typename Child, typename TreePath, typename ChildIndex, typename U>
334  auto beforeChild(T&&, Child&&, TreePath, ChildIndex, const U& u) const {return u;}
335 
343  template<typename T, typename Child, typename TreePath, typename ChildIndex, typename U>
344  auto afterChild(T&&, Child&&, TreePath, 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>
367  struct VisitChild
368  {
370  static const bool value = false;
371  };
372 
373  };
374 
375 
377 
381  struct VisitTree
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>
393  struct VisitChild
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 
430  struct TreeVisitor
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&&, 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:53
constexpr index_constant< 1 > _1
Compile time index with value 1.
Definition: indices.hh:56
std::integral_constant< std::size_t, i > index_constant
An index constant with value i.
Definition: indices.hh:30
auto max(ADLTag< 0 >, const V &v1, const V &v2)
implements binary Simd::max()
Definition: defaults.hh:81
constexpr std::size_t treePathSize(const HybridTreePath< T... > &)
Returns the size (number of components) of the given HybridTreePath.
Definition: treepath.hh:199
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
Dune namespace.
Definition: alignedallocator.hh:13
Visitor interface and base class for visitors of pairs of TypeTrees.
Definition: visitor.hh:162
void leaf(T1 &&, T2 &&, TreePath) const
Method for leaf traversal.
Definition: visitor.hh:214
void beforeChild(T1 &&, Child1 &&, T2 &&, Child2 &&, TreePath, ChildIndex) const
Method for parent-child traversal.
Definition: visitor.hh:230
void pre(T1 &&, T2 &&, TreePath) const
Method for prefix tree traversal.
Definition: visitor.hh:174
void post(T1 &&, T2 &&, TreePath) const
Method for postfix traversal.
Definition: visitor.hh:199
void in(T1 &&, T2 &&, TreePath) const
Method for infix tree traversal.
Definition: visitor.hh:187
void afterChild(T1 &&, Child1 &&, T2 &&, Child2 &&, TreePath, ChildIndex) const
Method for child-parent traversal.
Definition: visitor.hh:246
Visitor interface and base class for TypeTree visitors.
Definition: visitor.hh:47
void in(T &&, TreePath) const
Method for infix tree traversal.
Definition: visitor.hh:70
void afterChild(T &&, Child &&, TreePath, ChildIndex) const
Method for child-parent traversal.
Definition: visitor.hh:120
void beforeChild(T &&, Child &&, TreePath, ChildIndex) const
Method for parent-child traversal.
Definition: visitor.hh:105
void post(T &&, TreePath) const
Method for postfix tree traversal.
Definition: visitor.hh:81
void leaf(T &&, TreePath) const
Method for leaf traversal.
Definition: visitor.hh:91
void pre(T &&, TreePath) const
Method for prefix tree traversal.
Definition: visitor.hh:58
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 &&, TreePath, const U &u) const
Method for postfix tree traversal.
Definition: visitor.hh:314
auto pre(T &&, TreePath, const U &u) const
Method for prefix tree traversal.
Definition: visitor.hh:294
auto leaf(T &&, TreePath, const U &u) const
Method for leaf traversal.
Definition: visitor.hh:324
auto afterChild(T &&, Child &&, TreePath, ChildIndex, const U &u) const
Method for child-parent traversal.
Definition: visitor.hh:344
auto in(T &&, TreePath, const U &u) const
Method for infix tree traversal.
Definition: visitor.hh:304
auto beforeChild(T &&, Child &&, TreePath, ChildIndex, const U &u) const
Method for parent-child traversal.
Definition: visitor.hh:334
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.80.0 (Apr 27, 22:29, 2024)