"Fossies" - the Fresh Open Source Software Archive 
Member "dune-typetree-2.8.0/dune/typetree/traversalutilities.hh" (31 Aug 2021, 3004 Bytes) of package /linux/misc/dune/dune-typetree-2.8.0.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "traversalutilities.hh" see the
Fossies "Dox" file reference documentation.
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_TRAVERSALUTILITIES_HH
5 #define DUNE_TYPETREE_TRAVERSALUTILITIES_HH
6
7 #include <dune/typetree/traversal.hh>
8
9 namespace Dune {
10 namespace TypeTree {
11
12 /** \addtogroup Tree Traversal
13 * \ingroup TypeTree
14 * \{
15 */
16
17 namespace {
18
19 //! Visitor that applies a functor and an associated reduction to a TypeTree.
20 /**
21 * \tparam F The functor to apply to leaf nodes. Must return a ResultType.
22 * \tparam R The reduction used to combine the results.
23 * \tparam ResultType The result type of the operation.
24 */
25 template<typename F, typename R, typename ResultType>
26 struct LeafReductionVisitor
27 : public TypeTree::TreeVisitor
28 {
29
30 static const TreePathType::Type treePathType = TreePathType::dynamic;
31
32 template<typename Node, typename TreePath>
33 void leaf(const Node& node, TreePath treePath)
34 {
35 _value = _reduction(_value,_functor(node,treePath));
36 }
37
38 LeafReductionVisitor(F functor, R reduction, ResultType startValue)
39 : _functor(functor)
40 , _reduction(reduction)
41 , _value(startValue)
42 {}
43
44 ResultType result() { return _value; }
45
46 F _functor;
47 R _reduction;
48 ResultType _value;
49
50 };
51
52 } // anonymous namespace
53
54 //! Calculate a quantity as a reduction over the leaf nodes of a TypeTree.
55 /**
56 * This function can be used to easily calculate a quantity that is a result of applying
57 * a functor to the leaf nodes of a TypeTree and combining the functor return values.
58 * The functor, reduction and result should all have cheap copy constructors to ensure
59 * good performance.
60 *
61 * The functor must conform to the pattern
62 * \code
63 * struct Functor
64 * {
65 * template<typename Node, typename TreePath>
66 * ResultType operator()(const Node& node, TreePath treePath) const
67 * {
68 * return ...;
69 * }
70 * };
71 * \endcode
72 *
73 * \param tree The tree on which to perform the calculation.
74 * \param functor The functor to apply to the leaf nodes.
75 * \param reduction The operation used to combine the individual results.
76 * \param startValue The initial value for the result.
77 *
78 * \returns The value obtained by combining the individual results for all leafs.
79 */
80 template<typename ResultType, typename Tree, typename F, typename R>
81 ResultType reduceOverLeafs(const Tree& tree, F functor, R reduction, ResultType startValue)
82 {
83 LeafReductionVisitor<F,R,ResultType> visitor(functor,reduction,startValue);
84 TypeTree::applyToTree(tree,visitor);
85 return visitor.result();
86 }
87
88 //! \} group Tree Traversal
89
90 } // namespace TypeTree
91 } //namespace Dune
92
93 #endif // DUNE_TYPETREE_TRAVERSALUTILITIES_HH