CoDiPack  3.0.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
traversalLogic.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2025 Chair for Scientific Computing (SciComp), University of Kaiserslautern-Landau
5 * Homepage: http://scicomp.rptu.de
6 * Contact: Prof. Nicolas R. Gauger (codi@scicomp.uni-kl.de)
7 *
8 * Lead developers: Max Sagebaum, Johannes Blühdorn (SciComp, University of Kaiserslautern-Landau)
9 *
10 * This file is part of CoDiPack (http://scicomp.rptu.de/software/codi).
11 *
12 * CoDiPack is free software: you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * CoDiPack is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty
19 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * See the GNU General Public License for more details.
22 * You should have received a copy of the GNU
23 * General Public License along with CoDiPack.
24 * If not, see <http://www.gnu.org/licenses/>.
25 *
26 * For other licensing options please contact us.
27 *
28 * Authors:
29 * - SciComp, University of Kaiserslautern-Landau:
30 * - Max Sagebaum
31 * - Johannes Blühdorn
32 * - Former members:
33 * - Tim Albring
34 */
35#pragma once
36
37#include <utility>
38
39#include "../../config.h"
40#include "../../misc/compileTimeLoop.hpp"
41#include "../../misc/macros.hpp"
42#include "nodeInterface.hpp"
43
45namespace codi {
46
56 template<typename T_Impl>
58 public:
59
60 using Impl = CODI_DD(T_Impl, TraversalLogic);
61
64 return static_cast<Impl&>(*this);
65 }
66
70 template<typename Node, typename... Args>
71 CODI_INLINE void eval(NodeInterface<Node> const& node, Args&&... args) {
72 cast().toNode(node.cast(), std::forward<Args>(args)...);
73 }
74
75 /*******************************************************************************/
78
86 template<typename Node, typename... Args>
87 CODI_INLINE void node(Node const& node, Args&&... args) {
88 // Default logic forwards to all links.
89 cast().toLinks(node, std::forward<Args>(args)...);
90 }
91
97 template<typename Node, typename... Args>
98 CODI_INLINE void leaf(Node const& node, Args&&... args) {
99 CODI_UNUSED(node, args...);
100 // Default logic does nothing.
101 }
102
110 template<size_t ChildNumber, typename Child, typename Root, typename... Args>
111 CODI_INLINE void link(Child const& child, Root const& root, Args&&... args) {
112 CODI_UNUSED(root, args...);
113 // Default logic forwards to node evaluation.
114 cast().toNode(child, std::forward<Args>(args)...);
115 }
116
118
119 protected:
120
121#ifndef DOXYGEN_DISABLE
122 template<typename TraversalImpl, bool endPoint = false>
123 struct CallSwitch {
124 public:
125 template<typename... Args>
126 CODI_INLINE static void call(TraversalImpl& impl, Args&&... args) {
127 impl.node(std::forward<Args>(args)...);
128 }
129 };
130
131 template<typename TraversalImpl>
132 struct CallSwitch<TraversalImpl, true> {
133 public:
134 template<typename... Args>
135 CODI_INLINE static void call(TraversalImpl& impl, Args&&... args) {
136 impl.leaf(std::forward<Args>(args)...);
137 }
138 };
139#endif
140
142 template<typename Node, typename... Args>
143 CODI_INLINE void toNode(Node const& node, Args&&... args) {
144 CallSwitch<Impl, 0 == Node::LinkCount>::call(cast(), node, std::forward<Args>(args)...);
145 }
146
148 template<typename Node, typename... Args>
149 CODI_INLINE void toLinks(CODI_DD(Node, CODI_T(NodeInterface<void>)) const& node, Args&&... args) {
151 cast().template link<i.value>(node.template getLink<i.value>(), node, std::forward<Args>(args)...);
152 });
153 }
154 };
155}
#define CODI_LAMBDA_INLINE
See codi::Config::ForcedInlines.
Definition config.h:473
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:469
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:96
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:116
CoDiPack - Code Differentiation Package.
Definition codi.hpp:94
inlinevoid static_for(F func, Args &&... args)
Static for with i = 0 .. (N - 1). See CompileTimeLoop for details.
Definition compileTimeLoop.hpp:110
inlinevoid CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition macros.hpp:54
Node side interface for the traversal of expressions.
Definition nodeInterface.hpp:56
Traversal of CoDiPack expressions.
Definition traversalLogic.hpp:57
inlinevoid eval(NodeInterface< Node > const &node, Args &&... args)
Start the evaluation of the logic on the given expression.
Definition traversalLogic.hpp:71
inlineImpl & cast()
Cast to the implementation.
Definition traversalLogic.hpp:63
inlinevoid toLinks(Node const &node, Args &&... args)
Helper method which calls link for each child.
Definition traversalLogic.hpp:149
inlinevoid link(Child const &child, Root const &root, Args &&... args)
Called for all links in the expression.
Definition traversalLogic.hpp:111
inlinevoid leaf(Node const &node, Args &&... args)
Called for all leaf nodes in the expression.
Definition traversalLogic.hpp:98
T_Impl Impl
See TraversalLogic.
Definition traversalLogic.hpp:60
inlinevoid toNode(Node const &node, Args &&... args)
Helper method to distinguish between leaf nodes and normal nodes.
Definition traversalLogic.hpp:143
inlinevoid node(Node const &node, Args &&... args)
Called for each node in the expression.
Definition traversalLogic.hpp:87