1 #ifndef CPPAD_CG_OPERATION_PATH_INCLUDED 2 #define CPPAD_CG_OPERATION_PATH_INCLUDED 18 #include <cppad/cg/operation_path_node.hpp> 19 #include <cppad/cg/bidir_graph.hpp> 35 inline bool findPathGraph(BidirGraph<Base>& foundGraph,
36 OperationNode<Base>& root,
37 OperationNode<Base>& target,
39 size_t maxBifurcations = (std::numeric_limits<size_t>::max)()) {
40 if (bifurcations >= maxBifurcations) {
44 if (&root == &target) {
48 if(foundGraph.contains(root)) {
52 auto* h = root.getCodeHandler();
54 if(h->isVisited(root)) {
61 PathNodeEdges<Base>& info = foundGraph[root];
63 const auto& args = root.getArguments();
66 for(
size_t i = 0; i < args.size(); ++i) {
67 const Argument<Base>& a = args[i];
68 if(a.getOperation() != nullptr ) {
69 auto& aNode = *a.getOperation();
70 if(findPathGraph(foundGraph, aNode, target, bifurcations, maxBifurcations)) {
71 foundGraph.connect(info, root, i);
82 foundGraph.erase(root);
89 inline BidirGraph<Base> CodeHandler<Base>::findPathGraph(OperationNode<Base>& root,
90 OperationNode<Base>& target) {
91 size_t bifurcations = 0;
92 return findPathGraph(root, target, bifurcations);
96 inline BidirGraph<Base> CodeHandler<Base>::findPathGraph(OperationNode<Base>& root,
97 OperationNode<Base>& target,
99 size_t maxBifurcations) {
100 startNewOperationTreeVisit();
102 BidirGraph<Base> foundGraph;
104 if (bifurcations <= maxBifurcations) {
105 if (&root == &target) {
108 CppAD::cg::findPathGraph<Base>(foundGraph, root, target, bifurcations, maxBifurcations);
120 std::vector<std::vector<OperationPathNode<Base> > > found;
122 startNewOperationTreeVisit();
125 std::vector<OperationPathNode<Base> > path2node;
126 path2node.reserve(30);
129 if (&root == &code) {
130 found.push_back(path2node);
132 findPaths(path2node, code, found, max);
142 std::vector<SourceCodePath>& found,
146 if (&code == currNode) {
147 found.push_back(currPath);
151 const std::vector<Argument<Base> >& args = currNode->
getArguments();
155 if (isVisited(*currNode)) {
158 std::vector<SourceCodePath> pathsFromNode = findPathsFromNode(found, *currNode);
159 for (
const SourceCodePath& pathFromNode : pathsFromNode) {
160 SourceCodePath newPath(currPath.size() + pathFromNode.size());
161 std::copy(currPath.begin(), currPath.end(), newPath.begin());
162 std::copy(pathFromNode.begin(), pathFromNode.end(), newPath.begin() + currPath.size());
163 found.push_back(newPath);
168 markVisited(*currNode);
170 size_t size = args.size();
171 for (
size_t i = 0; i < size; ++i) {
175 findPaths(currPath, code, found, max);
177 if (found.size() == max) {
189 std::vector<SourceCodePath> foundPaths;
190 std::set<size_t> argsFound;
192 for (
const SourceCodePath& path : nodePaths) {
193 size_t size = path.size();
194 for (
size_t i = 0; i < size - 1; i++) {
196 if (pnode.
node == &node) {
197 if (argsFound.find(path[i + 1].argIndex) == argsFound.end()) {
198 foundPaths.push_back(SourceCodePath(path.begin() + i + 1, path.end()));
199 argsFound.insert(path[i + 1].argIndex);
std::vector< SourceCodePath > findPaths(Node &root, Node &target, size_t max)
const std::vector< Argument< Base > > & getArguments() const
OperationNode< Base > * node