@@ -527,16 +527,19 @@ def _add_to_adjacency_list(self, src_node_ptr, tgt_node_ptr):
527527 current_count = self .builder .load (adj_count_ptr )
528528 current_capacity = self .builder .load (adj_cap_ptr )
529529
530+ needs_allocation = self .builder .icmp_signed ('==' , current_capacity , ir .Constant (self .int_type , 0 ))
531+ needs_resize = self .builder .icmp_signed ('>=' , current_count , current_capacity )
532+ needs_realloc = self .builder .or_ (needs_allocation , needs_resize )
533+
530534 resize_adj_block = self .builder .block .parent .append_basic_block (name = "resize_adj" )
531535 add_adj_block = self .builder .block .parent .append_basic_block (name = "add_adj" )
532536
533- needs_resize = self .builder .icmp_signed ('>=' , current_count , current_capacity )
534- self .builder .cbranch (needs_resize , resize_adj_block , add_adj_block )
537+ self .builder .cbranch (needs_realloc , resize_adj_block , add_adj_block )
535538
536539 self .builder .position_at_end (resize_adj_block )
537540 new_capacity = self .builder .select (
538- self . builder . icmp_signed ( '==' , current_capacity , ir . Constant ( self . int_type , 0 )) ,
539- ir .Constant (self .int_type , 4 ),
541+ needs_allocation ,
542+ ir .Constant (self .int_type , 1 ),
540543 self .builder .mul (current_capacity , ir .Constant (self .int_type , 2 ))
541544 )
542545
@@ -547,10 +550,10 @@ def _add_to_adjacency_list(self, src_node_ptr, tgt_node_ptr):
547550 new_array = self .builder .bitcast (new_array_mem , node_ptr_type .as_pointer ())
548551
549552 copy_adj_block = self .builder .block .parent .append_basic_block (name = "copy_adj" )
550- no_copy_adj_block = self .builder .block .parent .append_basic_block (name = "no_copy_adj " )
553+ store_new_block = self .builder .block .parent .append_basic_block (name = "store_new " )
551554
552- has_existing_adj = self .builder .icmp_signed ('>' , current_count , ir .Constant (self .int_type , 0 ))
553- self .builder .cbranch (has_existing_adj , copy_adj_block , no_copy_adj_block )
555+ has_existing_data = self .builder .icmp_signed ('>' , current_count , ir .Constant (self .int_type , 0 ))
556+ self .builder .cbranch (has_existing_data , copy_adj_block , store_new_block )
554557
555558 self .builder .position_at_end (copy_adj_block )
556559 old_adj_array_void = self .builder .load (adj_list_ptr )
@@ -559,11 +562,10 @@ def _add_to_adjacency_list(self, src_node_ptr, tgt_node_ptr):
559562
560563 new_array_void = self .builder .bitcast (new_array , self .void_ptr )
561564 self .builder .call (self .memcpy_func , [new_array_void , old_adj_array_void , old_size_bytes ])
562-
563565 self .builder .call (self .free_func , [old_adj_array_void ])
564- self .builder .branch (no_copy_adj_block )
566+ self .builder .branch (store_new_block )
565567
566- self .builder .position_at_end (no_copy_adj_block )
568+ self .builder .position_at_end (store_new_block )
567569 new_array_as_void = self .builder .bitcast (new_array , self .void_ptr )
568570 self .builder .store (new_array_as_void , adj_list_ptr )
569571 self .builder .store (new_capacity , adj_cap_ptr )
@@ -575,7 +577,6 @@ def _add_to_adjacency_list(self, src_node_ptr, tgt_node_ptr):
575577
576578 current_count_final = self .builder .load (adj_count_ptr )
577579 tgt_slot_ptr = self .builder .gep (adj_array_typed , [current_count_final ])
578-
579580 self .builder .store (tgt_node_ptr , tgt_slot_ptr )
580581
581582 new_adj_count = self .builder .add (current_count_final , ir .Constant (self .int_type , 1 ))
0 commit comments