Dune Core Modules (2.10.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// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-GPL-2.0-only-with-PDELab-exception
5
6#ifndef DUNE_TYPETREE_VISITOR_HH
7#define DUNE_TYPETREE_VISITOR_HH
8
9#include <dune/typetree/treepath.hh>
10#include <dune/common/hybridutilities.hh>
11
12namespace Dune {
13 namespace TypeTree {
14
21
49 {
50
52
59 template<typename T, typename TreePath>
60 void pre(T&&, TreePath) const {}
61
63
71 template<typename T, typename TreePath>
72 void in(T&&, TreePath) const {}
73
75
82 template<typename T, typename TreePath>
83 void post(T&&, TreePath) const {}
84
86
92 template<typename T, typename TreePath>
93 void leaf(T&&, TreePath) const {}
94
96
106 template<typename T, typename Child, typename TreePath, typename ChildIndex>
107 void beforeChild(T&&, Child&&, TreePath, ChildIndex) const {}
108
110
121 template<typename T, typename Child, typename TreePath, typename ChildIndex>
122 void afterChild(T&&, Child&&, TreePath, ChildIndex) const {}
123
124 };
125
126
128
164 {
165
167
175 template<typename T1, typename T2, typename TreePath>
176 void pre(T1&&, T2&&, TreePath) const {}
177
179
188 template<typename T1, typename T2, typename TreePath>
189 void in(T1&&, T2&&, TreePath) const {}
190
192
200 template<typename T1, typename T2, typename TreePath>
201 void post(T1&&, T2&&, TreePath) const {}
202
204
215 template<typename T1, typename T2, typename TreePath>
216 void leaf(T1&&, T2&&, TreePath) const {}
217
219
231 template<typename T1, typename Child1, typename T2, typename Child2, typename TreePath, typename ChildIndex>
232 void beforeChild(T1&&, Child1&&, T2&&, Child2&&, TreePath, ChildIndex) const {}
233
235
247 template<typename T1, typename Child1, typename T2, typename Child2, typename TreePath, typename ChildIndex>
248 void afterChild(T1&&, Child1&&, T2&&, Child2&&, TreePath, ChildIndex) const {}
249
250 };
251
252
253 namespace Experimental {
254
286 {
287
295 template<typename T, typename TreePath, typename U>
296 auto pre(T&&, TreePath, const U& u) const { return u;}
297
305 template<typename T, typename TreePath, typename U>
306 auto in(T&&, TreePath, const U& u) const {return u;}
307
315 template<typename T, typename TreePath, typename U>
316 auto post(T&&, TreePath, const U& u) const {return u;}
317
325 template<typename T, typename TreePath, typename U>
326 auto leaf(T&&, TreePath, const U& u) const { return u;}
327
335 template<typename T, typename Child, typename TreePath, typename ChildIndex, typename U>
336 auto beforeChild(T&&, Child&&, TreePath, ChildIndex, const U& u) const {return u;}
337
345 template<typename T, typename Child, typename TreePath, typename ChildIndex, typename U>
346 auto afterChild(T&&, Child&&, TreePath, ChildIndex, const U& u) const {return u;}
347
348 };
349 } // namespace Experimental
350
352
358 {
359
360 // the little trick with the default template arguments
361 // makes the class usable for both single-tree visitors
362 // and visitors for pairs of trees
364 template<typename Node1,
365 typename Child1,
366 typename Node2,
367 typename Child2 = void,
368 typename TreePath = void>
370 {
372 static const bool value = false;
373 };
374
375 };
376
377
379
384 {
385
386 // the little trick with the default template arguments
387 // makes the class usable for both single-tree visitors
388 // and visitors for pairs of trees
390 template<typename Node1,
391 typename Child1,
392 typename Node2,
393 typename Child2 = void,
394 typename TreePath = void>
396 {
398 static const bool value = true;
399 };
400
401 };
402
404
412 {
414 static const TreePathType::Type treePathType = TreePathType::fullyStatic;
415 };
416
418
426 {
428 static const TreePathType::Type treePathType = TreePathType::dynamic;
429 };
430
433 : public DefaultVisitor
434 , public VisitTree
435 {};
436
439 : public DefaultVisitor
440 , public VisitDirectChildren
441 {};
442
445 : public DefaultPairVisitor
446 , public VisitTree
447 {};
448
451 : public DefaultPairVisitor
452 , public VisitDirectChildren
453 {};
454
455 namespace Experimental::Info {
456
457 struct LeafCounterVisitor
458 : public DefaultHybridVisitor
459 , public StaticTraversal
460 , public VisitTree
461 {
462 template<class Tree, class Child, class TreePath, class ChildIndex, class U>
463 auto beforeChild(Tree&&, Child&&, TreePath, ChildIndex, U u) const {
464 // in this case child index is an integral constant: forward u
465 return u;
466 }
467
468 template<class Tree, class Child, class TreePath, class U>
469 std::size_t beforeChild(Tree&&, Child&&, TreePath, std::size_t /*childIndex*/, U u) const {
470 // in this case child index is a run-time index: cast accumulated u to std::size_t
471 return std::size_t{u};
472 }
473
474 template<class Tree, class TreePath, class U>
475 auto leaf(Tree&&, TreePath, U u) const
476 {
478 }
479
480 };
481
482 struct NodeCounterVisitor
483 : public LeafCounterVisitor
484 {
485 template<typename Tree, typename TreePath, typename U>
486 auto pre(Tree&&, TreePath, U u) const {
487 return Hybrid::plus(u,Indices::_1);
488 }
489 };
490
491 struct DepthVisitor
492 : public DefaultHybridVisitor
493 , public StaticTraversal
494 , public VisitTree
495 {
496 template<class Tree, class TreePath, class U>
497 auto leaf(Tree&&, TreePath, U u) const
498 {
499 auto path_size = index_constant<treePathSize(TreePath{})>{};
500 auto depth = Hybrid::plus(path_size,Indices::_1);
501 return Hybrid::max(depth,u);
502 }
503 };
504
506 // result is alwayas an integral constant
507 template<typename Tree>
508 auto depth(const Tree& tree)
509 {
510 return hybridApplyToTree(tree,DepthVisitor{},Indices::_0);
511 }
512
514 // return types is std::integral_constant.
515 template<typename Tree>
516 constexpr auto depth()
517 {
518 return decltype(hybridApplyToTree(std::declval<Tree>(),DepthVisitor{},Indices::_0)){};
519 }
520
522 // if Tree is dynamic, return type is std::size_t, otherwise std::integral_constant.
523 template<typename Tree>
524 auto nodeCount(const Tree& tree)
525 {
526 return hybridApplyToTree(tree,NodeCounterVisitor{},Indices::_0);
527 }
528
530 // if Tree is dynamic, return type is std::size_t, otherwise std::integral_constant.
531 template<typename Tree>
532 auto leafCount(const Tree& tree)
533 {
534 return hybridApplyToTree(tree,LeafCounterVisitor{},Dune::Indices::_0);
535 }
536
538 template<typename Tree>
539 constexpr bool isDynamic = std::is_same<std::size_t, decltype(leafCount(std::declval<Tree>()))>{};
540
541 } // namespace Experimental::Info
542
544
545 } // namespace TypeTree
546} //namespace Dune
547
548#endif // DUNE_TYPETREE_VISITOR_HH
constexpr index_constant< 0 > _0
Compile time index with value 0.
Definition: indices.hh:52
constexpr index_constant< 1 > _1
Compile time index with value 1.
Definition: indices.hh:55
std::integral_constant< std::size_t, i > index_constant
An index constant with value i.
Definition: indices.hh:29
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:484
constexpr auto plus
Function object for performing addition.
Definition: hybridutilities.hh:528
constexpr std::size_t treePathSize(const HybridTreePath< T... > &)
Returns the size (number of components) of the given HybridTreePath.
Definition: treepath.hh:334
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:225
Dune namespace.
Definition: alignedallocator.hh:13
Visitor interface and base class for visitors of pairs of TypeTrees.
Definition: visitor.hh:164
void leaf(T1 &&, T2 &&, TreePath) const
Method for leaf traversal.
Definition: visitor.hh:216
void beforeChild(T1 &&, Child1 &&, T2 &&, Child2 &&, TreePath, ChildIndex) const
Method for parent-child traversal.
Definition: visitor.hh:232
void pre(T1 &&, T2 &&, TreePath) const
Method for prefix tree traversal.
Definition: visitor.hh:176
void post(T1 &&, T2 &&, TreePath) const
Method for postfix traversal.
Definition: visitor.hh:201
void in(T1 &&, T2 &&, TreePath) const
Method for infix tree traversal.
Definition: visitor.hh:189
void afterChild(T1 &&, Child1 &&, T2 &&, Child2 &&, TreePath, ChildIndex) const
Method for child-parent traversal.
Definition: visitor.hh:248
Visitor interface and base class for TypeTree visitors.
Definition: visitor.hh:49
void in(T &&, TreePath) const
Method for infix tree traversal.
Definition: visitor.hh:72
void afterChild(T &&, Child &&, TreePath, ChildIndex) const
Method for child-parent traversal.
Definition: visitor.hh:122
void beforeChild(T &&, Child &&, TreePath, ChildIndex) const
Method for parent-child traversal.
Definition: visitor.hh:107
void post(T &&, TreePath) const
Method for postfix tree traversal.
Definition: visitor.hh:83
void leaf(T &&, TreePath) const
Method for leaf traversal.
Definition: visitor.hh:93
void pre(T &&, TreePath) const
Method for prefix tree traversal.
Definition: visitor.hh:60
Convenience base class for visiting the direct children of a node pair.
Definition: visitor.hh:453
Convenience base class for visiting the direct children of a node.
Definition: visitor.hh:441
Mixin base class for visitors that only need a dynamic TreePath during traversal.
Definition: visitor.hh:426
static const TreePathType::Type treePathType
Use the dynamic tree traversal algorithm.
Definition: visitor.hh:428
Hybrid visitor interface and base class for TypeTree hybrid visitors.
Definition: visitor.hh:286
auto post(T &&, TreePath, const U &u) const
Method for postfix tree traversal.
Definition: visitor.hh:316
auto pre(T &&, TreePath, const U &u) const
Method for prefix tree traversal.
Definition: visitor.hh:296
auto leaf(T &&, TreePath, const U &u) const
Method for leaf traversal.
Definition: visitor.hh:326
auto afterChild(T &&, Child &&, TreePath, ChildIndex, const U &u) const
Method for child-parent traversal.
Definition: visitor.hh:346
auto in(T &&, TreePath, const U &u) const
Method for infix tree traversal.
Definition: visitor.hh:306
auto beforeChild(T &&, Child &&, TreePath, ChildIndex, const U &u) const
Method for parent-child traversal.
Definition: visitor.hh:336
Mixin base class for visitors that require a static TreePath during traversal.
Definition: visitor.hh:412
static const TreePathType::Type treePathType
Use the static tree traversal algorithm.
Definition: visitor.hh:414
Convenience base class for visiting an entire tree pair.
Definition: visitor.hh:447
Convenience base class for visiting the entire tree.
Definition: visitor.hh:435
Template struct for determining whether or not to visit a given child.
Definition: visitor.hh:370
static const bool value
Do not visit any child.
Definition: visitor.hh:372
Mixin base class for visitors that only want to visit the direct children of a node.
Definition: visitor.hh:358
Template struct for determining whether or not to visit a given child.
Definition: visitor.hh:396
static const bool value
Visit any child.
Definition: visitor.hh:398
Mixin base class for visitors that want to visit the complete tree.
Definition: visitor.hh:384
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 27, 23:30, 2024)