CoDiPack  3.0.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
compileTimeTraversalLogic.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 "../../traits/misc/removeAll.hpp"
43#include "nodeInterface.hpp"
44
46namespace codi {
47
58 template<typename T_ResultType, typename T_Impl>
60 public:
61
62 using ResultType = CODI_DD(T_ResultType, size_t);
64
65 static ResultType constexpr NeutralElement = {};
66
70 template<typename Node, typename... Args>
71 CODI_INLINE static ResultType constexpr eval(Args&&... args) {
72 return toNode<Node>(std::forward<Args>(args)...);
73 }
74
75 /*******************************************************************************/
78
87 return a + b;
88 }
89
99 template<typename Node, typename... Args>
100 CODI_INLINE static ResultType constexpr node(Args&&... args) {
101 // Default logic forwards to all links.
102 return Impl::template toLinks<Node>(std::forward<Args>(args)...);
103 }
104
112 template<typename Node, typename... Args>
113 CODI_INLINE static ResultType constexpr leaf(Args&&... CODI_UNUSED_ARG(args)) {
114 // Default logic does nothing.
115 return Impl::NeutralElement;
116 }
117
127 template<size_t ChildNumber, typename Child, typename Root, typename... Args>
128 CODI_INLINE static ResultType constexpr link(Args&&... args) {
129 // Default logic forwards to node evaluation.
130 return Impl::template toNode<Child>(std::forward<Args>(args)...);
131 }
132
134
135 protected:
136
137#ifndef DOXYGEN_DISABLE
138 template<typename TraversalImpl, bool endPoint = false>
139 struct CallSwitch {
140 public:
141 template<typename Node, typename... Args>
142 CODI_INLINE static ResultType constexpr call(Args&&... args) {
143 return TraversalImpl::template node<Node>(std::forward<Args>(args)...);
144 }
145 };
146
147 template<typename TraversalImpl>
148 struct CallSwitch<TraversalImpl, true> {
149 public:
150 template<typename Node, typename... Args>
151 CODI_INLINE static ResultType constexpr call(Args&&... args) {
152 return TraversalImpl::template leaf<Node>(std::forward<Args>(args)...);
153 }
154 };
155#endif
156
158 template<typename Node, typename... Args>
159 CODI_INLINE static ResultType constexpr toNode(Args&&... args) {
160 return CallSwitch < Impl, 0 == Node::LinkCount > ::template call<Node>(std::forward<Args>(args)...);
161 }
162
163 private:
164
165 // Reduce variadic for just one argument.
166 template<typename Arg1>
167 CODI_INLINE static ResultType constexpr reduceVariadic(Arg1&& arg1) {
168 return arg1;
169 }
170
171 // Recursively calls reduceVariadic on the remainder and reduces the result with Impl::reduce together with
172 // the first argument.
173 template<typename Arg1, typename... Args>
174 CODI_INLINE static ResultType constexpr reduceVariadic(Arg1&& arg1, Args&&... args) {
175 return Impl::reduce(std::forward<Arg1>(arg1), reduceVariadic(std::forward<Args>(args)...));
176 }
177
178 // Expands the index sequence with the argument type of the getLink method from the Node. Calls reduceVariadic
179 // with all arguments.
180 template<typename Node, std::size_t... Is, typename... Args>
181 CODI_INLINE static ResultType constexpr toLinksImpl(std::index_sequence<Is...>, Args&&... args) {
182 return reduceVariadic(
183 Impl::template link<Is, remove_all<decltype(std::declval<Node>().template getLink<Is>())>, Node>(
184 std::forward<Args>(args)...)...);
185 }
186
187 public:
188
190 template<typename Node, typename... Args>
191 CODI_INLINE static ResultType constexpr toLinks(Args&&... args) {
192 return toLinksImpl<Node>(std::make_index_sequence<Node::LinkCount>(), std::forward<Args>(args)...);
193 }
194 };
195}
#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_UNUSED_ARG(arg)
Used in a constexpr context, where using CODI_UNUSED spoils the constexpr.
Definition macros.hpp:57
CoDiPack - Code Differentiation Package.
Definition codi.hpp:94
typename std::remove_cv< typename std::remove_reference< typename std::remove_cv< T >::type >::type >::type remove_all
Removes all reference, constant and volatile modifiers.
Definition removeAll.hpp:47
Traversal of CoDiPack expressions during compile time.
Definition compileTimeTraversalLogic.hpp:59
static ResultType constexpr NeutralElement
Definition compileTimeTraversalLogic.hpp:65
T_Impl Impl
See CompileTimeTraversalLogic.
Definition compileTimeTraversalLogic.hpp:63
inlinestatic ResultType constexpr eval(Args &&... args)
Start the evaluation of the logic on the given expression.
Definition compileTimeTraversalLogic.hpp:71
T_ResultType ResultType
See CompileTimeTraversalLogic.
Definition compileTimeTraversalLogic.hpp:62
inlinestatic ResultType constexpr link(Args &&... args)
Called for all links in the expression.
Definition compileTimeTraversalLogic.hpp:128
inlinestatic ResultType constexpr node(Args &&... args)
Called for each node in the expression.
Definition compileTimeTraversalLogic.hpp:100
inlinestatic ResultType constexpr leaf(Args &&...)
Called for all leaf nodes in the expression.
Definition compileTimeTraversalLogic.hpp:113
inlinestatic ResultType constexpr toLinks(Args &&... args)
Helper method which calls the method 'link' on all links of the node and reduces the results.
Definition compileTimeTraversalLogic.hpp:191
inlinestatic ResultType constexpr reduce(ResultType a, ResultType b)
Reduction operation for the results of two links.
Definition compileTimeTraversalLogic.hpp:86
inlinestatic ResultType constexpr toNode(Args &&... args)
Helper method to distinguish between leaf nodes and normal nodes.
Definition compileTimeTraversalLogic.hpp:159