Loading...
Searching...
No Matches
visitor.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-GPL-2.0-only-with-PDELab-exception
#ifndef DUNE_TYPETREE_VISITOR_HH
#define DUNE_TYPETREE_VISITOR_HH
#include <dune/typetree/treepath.hh>
#include <dune/common/hybridutilities.hh>
namespace Dune {
namespace TypeTree {
struct DefaultVisitor
{
template<typename T, typename TreePath>
void pre(T&&, TreePath) const {}
template<typename T, typename TreePath>
void in(T&&, TreePath) const {}
template<typename T, typename TreePath>
void post(T&&, TreePath) const {}
template<typename T, typename TreePath>
void leaf(T&&, TreePath) const {}
template<typename T, typename Child, typename TreePath, typename ChildIndex>
void beforeChild(T&&, Child&&, TreePath, ChildIndex) const {}
template<typename T, typename Child, typename TreePath, typename ChildIndex>
void afterChild(T&&, Child&&, TreePath, ChildIndex) const {}
};
struct DefaultPairVisitor
{
template<typename T1, typename T2, typename TreePath>
void pre(T1&&, T2&&, TreePath) const {}
template<typename T1, typename T2, typename TreePath>
void in(T1&&, T2&&, TreePath) const {}
template<typename T1, typename T2, typename TreePath>
void post(T1&&, T2&&, TreePath) const {}
template<typename T1, typename T2, typename TreePath>
void leaf(T1&&, T2&&, TreePath) const {}
template<typename T1, typename Child1, typename T2, typename Child2, typename TreePath, typename ChildIndex>
void beforeChild(T1&&, Child1&&, T2&&, Child2&&, TreePath, ChildIndex) const {}
template<typename T1, typename Child1, typename T2, typename Child2, typename TreePath, typename ChildIndex>
void afterChild(T1&&, Child1&&, T2&&, Child2&&, TreePath, ChildIndex) const {}
};
namespace Experimental {
struct DefaultHybridVisitor
{
template<typename T, typename TreePath, typename U>
auto pre(T&&, TreePath, const U& u) const { return u;}
template<typename T, typename TreePath, typename U>
auto in(T&&, TreePath, const U& u) const {return u;}
template<typename T, typename TreePath, typename U>
auto post(T&&, TreePath, const U& u) const {return u;}
template<typename T, typename TreePath, typename U>
auto leaf(T&&, TreePath, const U& u) const { return u;}
template<typename T, typename Child, typename TreePath, typename ChildIndex, typename U>
auto beforeChild(T&&, Child&&, TreePath, ChildIndex, const U& u) const {return u;}
template<typename T, typename Child, typename TreePath, typename ChildIndex, typename U>
auto afterChild(T&&, Child&&, TreePath, ChildIndex, const U& u) const {return u;}
};
} // namespace Experimental
struct VisitDirectChildren
{
// the little trick with the default template arguments
// makes the class usable for both single-tree visitors
// and visitors for pairs of trees
template<typename Node1,
typename Child1,
typename Node2,
typename Child2 = void,
typename TreePath = void>
struct VisitChild
{
static const bool value = false;
};
};
struct VisitTree
{
// the little trick with the default template arguments
// makes the class usable for both single-tree visitors
// and visitors for pairs of trees
template<typename Node1,
typename Child1,
typename Node2,
typename Child2 = void,
typename TreePath = void>
struct VisitChild
{
static const bool value = true;
};
};
struct StaticTraversal
{
static const TreePathType::Type treePathType = TreePathType::fullyStatic;
};
struct DynamicTraversal
{
static const TreePathType::Type treePathType = TreePathType::dynamic;
};
struct TreeVisitor
: public DefaultVisitor
, public VisitTree
{};
struct DirectChildrenVisitor
: public DefaultVisitor
, public VisitDirectChildren
{};
struct TreePairVisitor
: public DefaultPairVisitor
, public VisitTree
{};
struct DirectChildrenPairVisitor
: public DefaultPairVisitor
, public VisitDirectChildren
{};
namespace Experimental::Info {
struct LeafCounterVisitor
: public DefaultHybridVisitor
, public StaticTraversal
, public VisitTree
{
template<class Tree, class Child, class TreePath, class ChildIndex, class U>
auto beforeChild(Tree&&, Child&&, TreePath, ChildIndex, U u) const {
// in this case child index is an integral constant: forward u
return u;
}
template<class Tree, class Child, class TreePath, class U>
std::size_t beforeChild(Tree&&, Child&&, TreePath, std::size_t /*childIndex*/, U u) const {
// in this case child index is a run-time index: cast accumulated u to std::size_t
return std::size_t{u};
}
template<class Tree, class TreePath, class U>
auto leaf(Tree&&, TreePath, U u) const
{
return Hybrid::plus(u,Dune::Indices::_1);
}
};
struct NodeCounterVisitor
: public LeafCounterVisitor
{
template<typename Tree, typename TreePath, typename U>
auto pre(Tree&&, TreePath, U u) const {
return Hybrid::plus(u,Indices::_1);
}
};
struct DepthVisitor
: public DefaultHybridVisitor
, public StaticTraversal
, public VisitTree
{
template<class Tree, class TreePath, class U>
auto leaf(Tree&&, TreePath, U u) const
{
auto path_size = index_constant<treePathSize(TreePath{})>{};
auto depth = Hybrid::plus(path_size,Indices::_1);
return Hybrid::max(depth,u);
}
};
// result is alwayas an integral constant
template<typename Tree>
auto depth(const Tree& tree)
{
return hybridApplyToTree(tree,DepthVisitor{},Indices::_0);
}
// return types is std::integral_constant.
template<typename Tree>
constexpr auto depth()
{
return decltype(hybridApplyToTree(std::declval<Tree>(),DepthVisitor{},Indices::_0)){};
}
// if Tree is dynamic, return type is std::size_t, otherwise std::integral_constant.
template<typename Tree>
auto nodeCount(const Tree& tree)
{
return hybridApplyToTree(tree,NodeCounterVisitor{},Indices::_0);
}
// if Tree is dynamic, return type is std::size_t, otherwise std::integral_constant.
template<typename Tree>
auto leafCount(const Tree& tree)
{
return hybridApplyToTree(tree,LeafCounterVisitor{},Dune::Indices::_0);
}
template<typename Tree>
constexpr bool isDynamic = std::is_same<std::size_t, decltype(leafCount(std::declval<Tree>()))>{};
} // namespace Experimental::Info
} // namespace TypeTree
} //namespace Dune
#endif // DUNE_TYPETREE_VISITOR_HH
treepath.hh
Dune::TypeTree::treePathSize
constexpr std::size_t treePathSize(const HybridTreePath< T... > &)
Returns the size (number of components) of the given HybridTreePath.
Definition treepath.hh:191
Dune
Definition accumulate_static.hh:17
Dune::TypeTree
Definition accumulate_static.hh:18
Dune::TypeTree::Experimental::hybridApplyToTree
auto hybridApplyToTree(Tree &&tree, Visitor &&visitor, Init &&init)
Apply hybrid visitor to TypeTree.
Definition accumulate_static.hh:702
Dune::TypeTree::TreePathType::Type
Type
Definition treepath.hh:108
Dune::TypeTree::TreePathType::fullyStatic
@ fullyStatic
Definition treepath.hh:108
Dune::TypeTree::TreePathType::dynamic
@ dynamic
Definition treepath.hh:108
Dune::TypeTree::Experimental::Info
Definition visitor.hh:455
Dune::TypeTree::Experimental::Info::isDynamic
constexpr bool isDynamic
true if any of the nodes in the tree only has dynamic degree.
Definition visitor.hh:539
Dune::TypeTree::Experimental::Info::leafCount
auto leafCount(const Tree &tree)
The number of leaf nodes in the Tree.
Definition visitor.hh:532
Dune::TypeTree::Experimental::Info::nodeCount
auto nodeCount(const Tree &tree)
The total number of nodes in the Tree.
Definition visitor.hh:524
Dune::TypeTree::Experimental::Info::depth
constexpr auto depth()
The depth of the Tree.
Definition visitor.hh:516
Dune::TypeTree::Experimental::Info::depth
auto depth(const Tree &tree)
The depth of the TypeTree.
Definition visitor.hh:508
Dune::TypeTree::DefaultVisitor
Visitor interface and base class for TypeTree visitors.
Definition visitor.hh:49
Dune::TypeTree::DefaultVisitor::in
void in(T &&, TreePath) const
Method for infix tree traversal.
Definition visitor.hh:72
Dune::TypeTree::DefaultVisitor::afterChild
void afterChild(T &&, Child &&, TreePath, ChildIndex) const
Method for child-parent traversal.
Definition visitor.hh:122
Dune::TypeTree::DefaultVisitor::beforeChild
void beforeChild(T &&, Child &&, TreePath, ChildIndex) const
Method for parent-child traversal.
Definition visitor.hh:107
Dune::TypeTree::DefaultVisitor::post
void post(T &&, TreePath) const
Method for postfix tree traversal.
Definition visitor.hh:83
Dune::TypeTree::DefaultVisitor::leaf
void leaf(T &&, TreePath) const
Method for leaf traversal.
Definition visitor.hh:93
Dune::TypeTree::DefaultVisitor::pre
void pre(T &&, TreePath) const
Method for prefix tree traversal.
Definition visitor.hh:60
Dune::TypeTree::DefaultPairVisitor
Visitor interface and base class for visitors of pairs of TypeTrees.
Definition visitor.hh:164
Dune::TypeTree::DefaultPairVisitor::leaf
void leaf(T1 &&, T2 &&, TreePath) const
Method for leaf traversal.
Definition visitor.hh:216
Dune::TypeTree::DefaultPairVisitor::beforeChild
void beforeChild(T1 &&, Child1 &&, T2 &&, Child2 &&, TreePath, ChildIndex) const
Method for parent-child traversal.
Definition visitor.hh:232
Dune::TypeTree::DefaultPairVisitor::pre
void pre(T1 &&, T2 &&, TreePath) const
Method for prefix tree traversal.
Definition visitor.hh:176
Dune::TypeTree::DefaultPairVisitor::post
void post(T1 &&, T2 &&, TreePath) const
Method for postfix traversal.
Definition visitor.hh:201
Dune::TypeTree::DefaultPairVisitor::in
void in(T1 &&, T2 &&, TreePath) const
Method for infix tree traversal.
Definition visitor.hh:189
Dune::TypeTree::DefaultPairVisitor::afterChild
void afterChild(T1 &&, Child1 &&, T2 &&, Child2 &&, TreePath, ChildIndex) const
Method for child-parent traversal.
Definition visitor.hh:248
Dune::TypeTree::Experimental::DefaultHybridVisitor
Hybrid visitor interface and base class for TypeTree hybrid visitors.
Definition visitor.hh:286
Dune::TypeTree::Experimental::DefaultHybridVisitor::post
auto post(T &&, TreePath, const U &u) const
Method for postfix tree traversal.
Definition visitor.hh:316
Dune::TypeTree::Experimental::DefaultHybridVisitor::pre
auto pre(T &&, TreePath, const U &u) const
Method for prefix tree traversal.
Definition visitor.hh:296
Dune::TypeTree::Experimental::DefaultHybridVisitor::leaf
auto leaf(T &&, TreePath, const U &u) const
Method for leaf traversal.
Definition visitor.hh:326
Dune::TypeTree::Experimental::DefaultHybridVisitor::afterChild
auto afterChild(T &&, Child &&, TreePath, ChildIndex, const U &u) const
Method for child-parent traversal.
Definition visitor.hh:346
Dune::TypeTree::Experimental::DefaultHybridVisitor::in
auto in(T &&, TreePath, const U &u) const
Method for infix tree traversal.
Definition visitor.hh:306
Dune::TypeTree::Experimental::DefaultHybridVisitor::beforeChild
auto beforeChild(T &&, Child &&, TreePath, ChildIndex, const U &u) const
Method for parent-child traversal.
Definition visitor.hh:336
Dune::TypeTree::VisitDirectChildren
Mixin base class for visitors that only want to visit the direct children of a node.
Definition visitor.hh:358
Dune::TypeTree::VisitDirectChildren::VisitChild
Template struct for determining whether or not to visit a given child.
Definition visitor.hh:370
Dune::TypeTree::VisitDirectChildren::VisitChild::value
static const bool value
Do not visit any child.
Definition visitor.hh:372
Dune::TypeTree::VisitTree
Mixin base class for visitors that want to visit the complete tree.
Definition visitor.hh:384
Dune::TypeTree::VisitTree::VisitChild
Template struct for determining whether or not to visit a given child.
Definition visitor.hh:396
Dune::TypeTree::VisitTree::VisitChild::value
static const bool value
Visit any child.
Definition visitor.hh:398
Dune::TypeTree::StaticTraversal
Mixin base class for visitors that require a static TreePath during traversal.
Definition visitor.hh:412
Dune::TypeTree::StaticTraversal::treePathType
static const TreePathType::Type treePathType
Use the static tree traversal algorithm.
Definition visitor.hh:414
Dune::TypeTree::DynamicTraversal
Mixin base class for visitors that only need a dynamic TreePath during traversal.
Definition visitor.hh:426
Dune::TypeTree::DynamicTraversal::treePathType
static const TreePathType::Type treePathType
Use the dynamic tree traversal algorithm.
Definition visitor.hh:428
Dune::TypeTree::TreeVisitor
Convenience base class for visiting the entire tree.
Definition visitor.hh:435
Dune::TypeTree::DirectChildrenVisitor
Convenience base class for visiting the direct children of a node.
Definition visitor.hh:441
Dune::TypeTree::TreePairVisitor
Convenience base class for visiting an entire tree pair.
Definition visitor.hh:447
Dune::TypeTree::DirectChildrenPairVisitor
Convenience base class for visiting the direct children of a node pair.
Definition visitor.hh:453
Dune::TypeTree::Experimental::Info::LeafCounterVisitor
Definition visitor.hh:461
Dune::TypeTree::Experimental::Info::LeafCounterVisitor::leaf
auto leaf(Tree &&, TreePath, U u) const
Definition visitor.hh:475
Dune::TypeTree::Experimental::Info::LeafCounterVisitor::beforeChild
auto beforeChild(Tree &&, Child &&, TreePath, ChildIndex, U u) const
Definition visitor.hh:463
Dune::TypeTree::Experimental::Info::LeafCounterVisitor::beforeChild
std::size_t beforeChild(Tree &&, Child &&, TreePath, std::size_t, U u) const
Definition visitor.hh:469
Dune::TypeTree::Experimental::Info::NodeCounterVisitor
Definition visitor.hh:484
Dune::TypeTree::Experimental::Info::NodeCounterVisitor::pre
auto pre(Tree &&, TreePath, U u) const
Definition visitor.hh:486
Dune::TypeTree::Experimental::Info::DepthVisitor
Definition visitor.hh:495
Dune::TypeTree::Experimental::Info::DepthVisitor::leaf
auto leaf(Tree &&, TreePath, U u) const
Definition visitor.hh:497