| 539 | } |
| 540 | |
| 541 | Graph Graph::Subgraph( |
| 542 | std::uint32_t begin, |
| 543 | std::uint32_t end, |
| 544 | std::vector<const Node*>* subgraph_to_graph) const { |
| 545 | if (!subgraph_to_graph) { |
| 546 | throw std::invalid_argument( |
| 547 | "[spoa::Graph::Subgraph] error: invalid ptr to subgraph_to_graph"); |
| 548 | } |
| 549 | |
| 550 | auto is_in_subgraph = ExtractSubgraph(nodes_[end].get(), nodes_[begin].get()); |
| 551 | |
| 552 | // init subgraph |
| 553 | Graph subgraph{}; |
| 554 | subgraph.num_codes_ = num_codes_; |
| 555 | subgraph.coder_ = coder_; |
| 556 | subgraph.decoder_ = decoder_; |
| 557 | // subgraph.sequences_ = TODO(rvaser) maybe add sequences |
| 558 | |
| 559 | // create a map from subgraph nodes to graph nodes and vice versa |
| 560 | subgraph_to_graph->clear(); |
| 561 | subgraph_to_graph->resize(nodes_.size(), nullptr); |
| 562 | |
| 563 | std::vector<Node*> graph_to_subgraph(nodes_.size(), nullptr); |
| 564 | |
| 565 | for (const auto& it : nodes_) { |
| 566 | if (!is_in_subgraph[it->id]) { |
| 567 | continue; |
| 568 | } |
| 569 | subgraph.AddNode(it->code); |
| 570 | graph_to_subgraph[it->id] = subgraph.nodes_.back().get(); |
| 571 | (*subgraph_to_graph)[subgraph.nodes_.back()->id] = it.get(); |
| 572 | } |
| 573 | |
| 574 | // connect nodes |
| 575 | for (const auto& it : nodes_) { |
| 576 | if (!is_in_subgraph[it->id]) { |
| 577 | continue; |
| 578 | } |
| 579 | auto jt = graph_to_subgraph[it->id]; |
| 580 | for (const auto& kt : it->inedges) { |
| 581 | if (graph_to_subgraph[kt->tail->id]) { |
| 582 | subgraph.AddEdge(graph_to_subgraph[kt->tail->id], jt, kt->weight); |
| 583 | } |
| 584 | } |
| 585 | for (const auto& kt : it->aligned_nodes) { |
| 586 | if (graph_to_subgraph[kt->id]) { |
| 587 | jt->aligned_nodes.emplace_back(graph_to_subgraph[kt->id]); |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | subgraph.TopologicalSort(); |
| 593 | |
| 594 | return subgraph; |
| 595 | } |
| 596 | |
| 597 | void Graph::UpdateAlignment( |
| 598 | const std::vector<const Node*>& subgraph_to_graph, |
no test coverage detected