@@ -69,7 +69,7 @@ impl<'a> Context<'a> {
69
69
/// Exports the root module of the HUGR graph.
70
70
pub fn export_root ( & mut self ) {
71
71
let hugr_children = self . hugr . children ( self . hugr . root ( ) ) ;
72
- let mut children = Vec :: with_capacity ( hugr_children. len ( ) ) ;
72
+ let mut children = Vec :: with_capacity ( hugr_children. size_hint ( ) . 0 ) ;
73
73
74
74
for child in self . hugr . children ( self . hugr . root ( ) ) {
75
75
children. push ( self . export_node ( child) ) ;
@@ -110,7 +110,7 @@ impl<'a> Context<'a> {
110
110
num_ports : usize ,
111
111
) -> & ' a [ model:: LinkRef < ' a > ] {
112
112
let ports = self . hugr . node_ports ( node, direction) ;
113
- let mut links = BumpVec :: with_capacity_in ( ports. len ( ) , self . bump ) ;
113
+ let mut links = BumpVec :: with_capacity_in ( ports. size_hint ( ) . 0 , self . bump ) ;
114
114
115
115
for port in ports. take ( num_ports) {
116
116
links. push ( model:: LinkRef :: Id ( self . get_link_id ( node, port) ) ) ;
@@ -579,7 +579,7 @@ impl<'a> Context<'a> {
579
579
let targets = self . make_ports ( output_node, Direction :: Incoming , output_op. types . len ( ) ) ;
580
580
581
581
// Export the remaining children of the node.
582
- let mut region_children = BumpVec :: with_capacity_in ( children. len ( ) , self . bump ) ;
582
+ let mut region_children = BumpVec :: with_capacity_in ( children. size_hint ( ) . 0 , self . bump ) ;
583
583
584
584
for child in children {
585
585
region_children. push ( self . export_node ( child) ) ;
@@ -609,7 +609,7 @@ impl<'a> Context<'a> {
609
609
/// Creates a control flow region from the given node's children.
610
610
pub fn export_cfg ( & mut self , node : Node ) -> model:: RegionId {
611
611
let mut children = self . hugr . children ( node) ;
612
- let mut region_children = BumpVec :: with_capacity_in ( children. len ( ) + 1 , self . bump ) ;
612
+ let mut region_children = BumpVec :: with_capacity_in ( children. size_hint ( ) . 0 + 1 , self . bump ) ;
613
613
614
614
// The first child is the entry block.
615
615
// We create a source port on the control flow region and connect it to the
@@ -623,16 +623,16 @@ impl<'a> Context<'a> {
623
623
let source = model:: LinkRef :: Id ( self . get_link_id ( entry_block, IncomingPort :: from ( 0 ) ) ) ;
624
624
region_children. push ( self . export_node ( entry_block) ) ;
625
625
626
- // Export the remaining children of the node, except for the last one.
627
- for _ in 0 ..children. len ( ) - 1 {
628
- region_children. push ( self . export_node ( children. next ( ) . unwrap ( ) ) ) ;
629
- }
630
-
631
626
// The last child is the exit block.
632
627
// Contrary to the entry block, the exit block does not have a dataflow subgraph.
633
628
// We therefore do not export the block itself, but simply use its output ports
634
629
// as the target ports of the control flow region.
635
- let exit_block = children. next ( ) . unwrap ( ) ;
630
+ let exit_block = children. next_back ( ) . unwrap ( ) ;
631
+
632
+ // Export the remaining children of the node, except for the last one.
633
+ for child in children {
634
+ region_children. push ( self . export_node ( child) ) ;
635
+ }
636
636
637
637
let OpType :: ExitBlock ( _) = self . hugr . get_optype ( exit_block) else {
638
638
panic ! ( "expected an `ExitBlock` node as the last child node" ) ;
@@ -657,7 +657,7 @@ impl<'a> Context<'a> {
657
657
/// Export the `Case` node children of a `Conditional` node as data flow regions.
658
658
pub fn export_conditional_regions ( & mut self , node : Node ) -> & ' a [ model:: RegionId ] {
659
659
let children = self . hugr . children ( node) ;
660
- let mut regions = BumpVec :: with_capacity_in ( children. len ( ) , self . bump ) ;
660
+ let mut regions = BumpVec :: with_capacity_in ( children. size_hint ( ) . 0 , self . bump ) ;
661
661
662
662
for child in children {
663
663
let OpType :: Case ( case_op) = self . hugr . get_optype ( child) else {
0 commit comments