| 12 | for GeometryGraph<Direction> |
| 13 | { |
| 14 | fn from(ffi_graph: &crate::cxxbridge::GeometryGraphShim) -> GeometryGraph<Direction> { |
| 15 | let nodes = ffi_graph.nodes(); |
| 16 | // The edges are indices into the nodes array. |
| 17 | let edges = ffi_graph.edges(); |
| 18 | |
| 19 | let mut graph = GeometryGraph::default(); |
| 20 | graph.reserve_exact_nodes(nodes.len()); |
| 21 | graph.reserve_exact_edges(edges.len()); |
| 22 | |
| 23 | for (_cxx_node_index, node) in nodes.iter().enumerate() { |
| 24 | let point = Point::new(node.x, node.y); |
| 25 | // We rely on the implementation detail of petgraph::Graph that when you insert nodes in |
| 26 | // order, the node indices are generated in the same order. |
| 27 | let _node_index = graph.add_node(point); |
| 28 | debug_assert_eq!(_node_index.index(), _cxx_node_index); |
| 29 | } |
| 30 | for edge in &edges { |
| 31 | let crate::cxxbridge::GraphEdge { src, dst } = edge; |
| 32 | let src = petgraph::graph::NodeIndex::new(*src); |
| 33 | let dst = petgraph::graph::NodeIndex::new(*dst); |
| 34 | let _edge_index = graph.add_edge(src, dst, ()); |
| 35 | } |
| 36 | |
| 37 | graph |
| 38 | } |
| 39 | } |