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>
35inline 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);
89inline BidirGraph<Base> CodeHandler<Base>::findPathGraph(OperationNode<Base>& root,
90 OperationNode<Base>& target) {
91 size_t bifurcations = 0;
92 return findPathGraph(root, target, bifurcations);
96inline 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) {
120 std::vector<std::vector<OperationPathNode<Base> > >
found;
122 startNewOperationTreeVisit();
125 std::vector<OperationPathNode<Base> >
path2node;
142 std::vector<SourceCodePath>&
found,
151 const std::vector<Argument<Base> >&
args =
currNode->getArguments();
168 markVisited(*currNode);
170 size_t size = args.size();
171 for (
size_t i = 0; i < size; ++i) {
172 OperationNode<Base>* a = args[i].getOperation();
174 currPath.push_back(OperationPathNode<Base> (a, i));
175 findPaths(currPath, code, found, max);
177 if (found.size() == max) {
186inline std::vector<std::vector<OperationPathNode<Base> > > CodeHandler<Base>::findPathsFromNode(
const std::vector<SourceCodePath> nodePaths,
187 OperationNode<Base>& node) {
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++) {
195 const OperationPathNode<Base>& pnode = path[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)