| 257 | } |
| 258 | |
| 259 | Status Node::input_edge(int idx, const Edge** e) const { |
| 260 | if (idx < 0 || idx >= num_inputs()) { |
| 261 | return errors::InvalidArgument("Invalid input_edge index: ", idx, ", Node ", |
| 262 | name(), " only has ", num_inputs(), |
| 263 | " inputs."); |
| 264 | } |
| 265 | |
| 266 | // This does a linear search over the edges. In the common case, |
| 267 | // the number of elements is small enough that this search isn't |
| 268 | // expensive. Should it become a bottleneck, one can make an |
| 269 | // optimization where, if the number of edges is small, we use |
| 270 | // linear iteration, and if the number of edges is large, we perform |
| 271 | // an indexing step during construction that keeps an array of Edges |
| 272 | // indexed by pointer. This would keep the size of each Node small |
| 273 | // in the common case but make this function faster when the number |
| 274 | // of edges is large. |
| 275 | for (const Edge* edge : in_edges()) { |
| 276 | if (edge->dst_input() == idx) { |
| 277 | *e = edge; |
| 278 | return Status::OK(); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | return errors::NotFound("Could not find input edge ", idx, " for ", name()); |
| 283 | } |
| 284 | |
| 285 | // Returns a vector of the non-control input edges to a node, indexed by ID. |
| 286 | Status Node::input_edges(std::vector<const Edge*>* input_edges) const { |