| 540 | } |
| 541 | |
| 542 | const Edge* Graph::AddControlEdge(Node* source, Node* dest, |
| 543 | bool allow_duplicates) { |
| 544 | if (!allow_duplicates) { |
| 545 | for (const Edge* edge : dest->in_edges()) { |
| 546 | if (edge->IsControlEdge() && edge->src() == source) { |
| 547 | // The requested edge already exists. |
| 548 | return nullptr; |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | // Modify dest's NodeDef if necessary. |
| 553 | if (!source->IsSource() && !dest->IsSink() && !allow_duplicates) { |
| 554 | // Check if this input is already in dest's NodeDef. |
| 555 | const string new_input = strings::StrCat("^", source->name()); |
| 556 | bool input_exists = false; |
| 557 | for (const string& input : dest->props_->node_def.input()) { |
| 558 | if (input == new_input) { |
| 559 | input_exists = true; |
| 560 | break; |
| 561 | } |
| 562 | } |
| 563 | if (!input_exists) { |
| 564 | dest->MaybeCopyOnWrite(); |
| 565 | dest->props_->node_def.add_input(new_input); |
| 566 | } |
| 567 | } |
| 568 | return AddEdge(source, kControlSlot, dest, kControlSlot); |
| 569 | } |
| 570 | |
| 571 | void Graph::RemoveControlEdge(const Edge* e) { |
| 572 | if (!e->src_->IsSource() && !e->dst_->IsSink()) { |