Returns a string describing how an edge from src to dst would create a cycle.
| 44 | // Returns a string describing how an edge from src to dst would |
| 45 | // create a cycle. |
| 46 | string DescribeCycle(const GraphCycles* cycles, const Graph& graph, int src, |
| 47 | int dst) { |
| 48 | int32 max_path_size = graph.num_node_ids() + 1; |
| 49 | std::vector<int32> path(max_path_size); |
| 50 | int32 path_size = cycles->FindPath(dst, src, max_path_size, path.data()); |
| 51 | if (path_size == 0) { |
| 52 | return ""; |
| 53 | } |
| 54 | |
| 55 | auto node_name = [&graph](int node_id) { |
| 56 | if (!FastBoundsCheck(node_id, graph.num_node_ids())) { |
| 57 | return string("(null)"); |
| 58 | } |
| 59 | auto* node = graph.FindNodeId(node_id); |
| 60 | if (node == nullptr) { |
| 61 | return string("(null)"); |
| 62 | } |
| 63 | return node->name(); |
| 64 | }; |
| 65 | |
| 66 | string description; |
| 67 | absl::StrAppend(&description, "Edge from ", node_name(src), " to ", |
| 68 | node_name(dst), " would create a cycle.\n"); |
| 69 | path.resize(path_size); |
| 70 | for (int32 node_id : path) { |
| 71 | string ascii_art; |
| 72 | if (node_id == dst) { |
| 73 | ascii_art = "+-> "; |
| 74 | } else if (node_id != src) { |
| 75 | ascii_art = "| "; |
| 76 | } else { |
| 77 | ascii_art = "+-- "; |
| 78 | } |
| 79 | absl::StrAppend(&description, ascii_art, node_name(node_id), "\n"); |
| 80 | } |
| 81 | return description; |
| 82 | } |
| 83 | |
| 84 | bool AlwaysForwardsRefInput(const Node& node) { return node.IsIdentity(); } |
| 85 |
no test coverage detected