@@ -9542,71 +9542,115 @@ simplifier_finalise_site_references(
9542
9542
}
9543
9543
9544
9544
static int TSK_WARN_UNUSED
9545
- simplifier_finalise_population_references (
9546
- simplifier_t * self , const bool * population_referenced , tsk_id_t * population_id_map )
9545
+ simplifier_finalise_population_references (simplifier_t * self )
9547
9546
{
9548
9547
int ret = 0 ;
9549
- tsk_id_t ret_id ;
9550
9548
tsk_size_t j ;
9549
+ tsk_id_t pop_id , ret_id ;
9551
9550
tsk_population_t pop ;
9551
+ tsk_id_t * node_population = self -> tables -> nodes .population ;
9552
+ const tsk_size_t num_nodes = self -> tables -> nodes .num_rows ;
9552
9553
const tsk_size_t num_populations = self -> input_tables .populations .num_rows ;
9554
+ bool * population_referenced
9555
+ = tsk_calloc (num_populations , sizeof (* population_referenced ));
9556
+ tsk_id_t * population_id_map
9557
+ = tsk_malloc (num_populations * sizeof (* population_id_map ));
9553
9558
9554
- if (self -> options & TSK_SIMPLIFY_FILTER_POPULATIONS ) {
9555
- for (j = 0 ; j < num_populations ; j ++ ) {
9556
- tsk_population_table_get_row_unsafe (
9557
- & self -> input_tables .populations , (tsk_id_t ) j , & pop );
9558
- population_id_map [j ] = TSK_NULL ;
9559
- if (population_referenced [j ]) {
9560
- ret_id = tsk_population_table_add_row (
9561
- & self -> tables -> populations , pop .metadata , pop .metadata_length );
9562
- if (ret_id < 0 ) {
9563
- ret = (int ) ret_id ;
9564
- goto out ;
9565
- }
9566
- population_id_map [j ] = ret_id ;
9559
+ tsk_bug_assert (self -> options & TSK_SIMPLIFY_FILTER_POPULATIONS );
9560
+
9561
+ if (population_referenced == NULL || population_id_map == NULL ) {
9562
+ ret = TSK_ERR_NO_MEMORY ;
9563
+ goto out ;
9564
+ }
9565
+
9566
+ for (j = 0 ; j < num_nodes ; j ++ ) {
9567
+ pop_id = node_population [j ];
9568
+ if (pop_id != TSK_NULL ) {
9569
+ population_referenced [pop_id ] = true;
9570
+ }
9571
+ }
9572
+
9573
+ for (j = 0 ; j < num_populations ; j ++ ) {
9574
+ tsk_population_table_get_row_unsafe (
9575
+ & self -> input_tables .populations , (tsk_id_t ) j , & pop );
9576
+ population_id_map [j ] = TSK_NULL ;
9577
+ if (population_referenced [j ]) {
9578
+ ret_id = tsk_population_table_add_row (
9579
+ & self -> tables -> populations , pop .metadata , pop .metadata_length );
9580
+ if (ret_id < 0 ) {
9581
+ ret = (int ) ret_id ;
9582
+ goto out ;
9567
9583
}
9584
+ population_id_map [j ] = ret_id ;
9568
9585
}
9569
- } else {
9570
- tsk_bug_assert (self -> tables -> populations .num_rows == num_populations );
9571
- for (j = 0 ; j < num_populations ; j ++ ) {
9572
- population_id_map [j ] = (tsk_id_t ) j ;
9586
+ }
9587
+
9588
+ /* Remap the IDs in the node table */
9589
+ for (j = 0 ; j < num_nodes ; j ++ ) {
9590
+ pop_id = node_population [j ];
9591
+ if (pop_id != TSK_NULL ) {
9592
+ node_population [j ] = population_id_map [pop_id ];
9573
9593
}
9574
9594
}
9575
9595
out :
9596
+ tsk_safe_free (population_id_map );
9597
+ tsk_safe_free (population_referenced );
9576
9598
return ret ;
9577
9599
}
9578
9600
9579
9601
static int TSK_WARN_UNUSED
9580
- simplifier_finalise_individual_references (
9581
- simplifier_t * self , const bool * individual_referenced , tsk_id_t * individual_id_map )
9602
+ simplifier_finalise_individual_references (simplifier_t * self )
9582
9603
{
9583
9604
int ret = 0 ;
9584
- tsk_id_t ret_id ;
9585
9605
tsk_size_t j ;
9606
+ tsk_id_t pop_id , ret_id ;
9586
9607
tsk_individual_t ind ;
9587
- const tsk_size_t num_individuals = self -> input_tables . individuals . num_rows ;
9608
+ tsk_id_t * node_individual = self -> tables -> nodes . individual ;
9588
9609
tsk_id_t * parents ;
9610
+ const tsk_size_t num_nodes = self -> tables -> nodes .num_rows ;
9611
+ const tsk_size_t num_individuals = self -> input_tables .individuals .num_rows ;
9612
+ bool * individual_referenced
9613
+ = tsk_calloc (num_individuals , sizeof (* individual_referenced ));
9614
+ tsk_id_t * individual_id_map
9615
+ = tsk_malloc (num_individuals * sizeof (* individual_id_map ));
9589
9616
9590
- if (self -> options & TSK_SIMPLIFY_FILTER_INDIVIDUALS ) {
9591
- for (j = 0 ; j < num_individuals ; j ++ ) {
9592
- tsk_individual_table_get_row_unsafe (
9593
- & self -> input_tables .individuals , (tsk_id_t ) j , & ind );
9594
- individual_id_map [j ] = TSK_NULL ;
9595
- if (individual_referenced [j ]) {
9596
- ret_id = tsk_individual_table_add_row (& self -> tables -> individuals ,
9597
- ind .flags , ind .location , ind .location_length , ind .parents ,
9598
- ind .parents_length , ind .metadata , ind .metadata_length );
9599
- if (ret_id < 0 ) {
9600
- ret = (int ) ret_id ;
9601
- goto out ;
9602
- }
9603
- individual_id_map [j ] = ret_id ;
9617
+ tsk_bug_assert (self -> options & TSK_SIMPLIFY_FILTER_INDIVIDUALS );
9618
+
9619
+ if (individual_referenced == NULL || individual_id_map == NULL ) {
9620
+ ret = TSK_ERR_NO_MEMORY ;
9621
+ goto out ;
9622
+ }
9623
+
9624
+ for (j = 0 ; j < num_nodes ; j ++ ) {
9625
+ pop_id = node_individual [j ];
9626
+ if (pop_id != TSK_NULL ) {
9627
+ individual_referenced [pop_id ] = true;
9628
+ }
9629
+ }
9630
+
9631
+ for (j = 0 ; j < num_individuals ; j ++ ) {
9632
+ tsk_individual_table_get_row_unsafe (
9633
+ & self -> input_tables .individuals , (tsk_id_t ) j , & ind );
9634
+ individual_id_map [j ] = TSK_NULL ;
9635
+ if (individual_referenced [j ]) {
9636
+ /* Can't remap the parents inline here because we have no
9637
+ * guarantees about sortedness */
9638
+ ret_id = tsk_individual_table_add_row (& self -> tables -> individuals , ind .flags ,
9639
+ ind .location , ind .location_length , ind .parents , ind .parents_length ,
9640
+ ind .metadata , ind .metadata_length );
9641
+ if (ret_id < 0 ) {
9642
+ ret = (int ) ret_id ;
9643
+ goto out ;
9604
9644
}
9645
+ individual_id_map [j ] = ret_id ;
9605
9646
}
9606
- } else {
9607
- tsk_bug_assert (self -> tables -> individuals .num_rows == num_individuals );
9608
- for (j = 0 ; j < num_individuals ; j ++ ) {
9609
- individual_id_map [j ] = (tsk_id_t ) j ;
9647
+ }
9648
+
9649
+ /* Remap the IDs in the node table */
9650
+ for (j = 0 ; j < num_nodes ; j ++ ) {
9651
+ pop_id = node_individual [j ];
9652
+ if (pop_id != TSK_NULL ) {
9653
+ node_individual [j ] = individual_id_map [pop_id ];
9610
9654
}
9611
9655
}
9612
9656
@@ -9619,7 +9663,10 @@ simplifier_finalise_individual_references(
9619
9663
parents [j ] = individual_id_map [parents [j ]];
9620
9664
}
9621
9665
}
9666
+
9622
9667
out :
9668
+ tsk_safe_free (individual_id_map );
9669
+ tsk_safe_free (individual_referenced );
9623
9670
return ret ;
9624
9671
}
9625
9672
@@ -9687,26 +9734,6 @@ static int TSK_WARN_UNUSED
9687
9734
simplifier_flush_output (simplifier_t * self )
9688
9735
{
9689
9736
int ret = 0 ;
9690
- const tsk_size_t num_nodes = self -> tables -> nodes .num_rows ;
9691
- const tsk_size_t num_populations = self -> input_tables .populations .num_rows ;
9692
- const tsk_size_t num_individuals = self -> input_tables .individuals .num_rows ;
9693
- tsk_id_t * node_population = self -> tables -> nodes .population ;
9694
- bool * population_referenced
9695
- = tsk_calloc (num_populations , sizeof (* population_referenced ));
9696
- tsk_id_t * population_id_map
9697
- = tsk_malloc (num_populations * sizeof (* population_id_map ));
9698
- tsk_id_t * node_individual = self -> tables -> nodes .individual ;
9699
- bool * individual_referenced
9700
- = tsk_calloc (num_individuals , sizeof (* individual_referenced ));
9701
- tsk_id_t * individual_id_map
9702
- = tsk_malloc (num_individuals * sizeof (* individual_id_map ));
9703
- tsk_size_t j ;
9704
- tsk_id_t pop_id , ind_id ;
9705
-
9706
- if (population_referenced == NULL || population_id_map == NULL
9707
- || individual_referenced == NULL || individual_id_map == NULL ) {
9708
- goto out ;
9709
- }
9710
9737
9711
9738
/* TODO Migrations fit reasonably neatly into the pattern that we have here. We
9712
9739
* can consider references to populations from migration objects in the same way
@@ -9727,44 +9754,20 @@ simplifier_flush_output(simplifier_t *self)
9727
9754
goto out ;
9728
9755
}
9729
9756
9730
- for (j = 0 ; j < num_nodes ; j ++ ) {
9731
- pop_id = node_population [j ];
9732
- if (pop_id != TSK_NULL ) {
9733
- population_referenced [pop_id ] = true;
9734
- }
9735
- ind_id = node_individual [j ];
9736
- if (ind_id != TSK_NULL ) {
9737
- individual_referenced [ind_id ] = true;
9757
+ if (self -> options & TSK_SIMPLIFY_FILTER_POPULATIONS ) {
9758
+ ret = simplifier_finalise_population_references (self );
9759
+ if (ret != 0 ) {
9760
+ goto out ;
9738
9761
}
9739
9762
}
9740
-
9741
- ret = simplifier_finalise_population_references (
9742
- self , population_referenced , population_id_map );
9743
- if (ret != 0 ) {
9744
- goto out ;
9745
- }
9746
- ret = simplifier_finalise_individual_references (
9747
- self , individual_referenced , individual_id_map );
9748
- if (ret != 0 ) {
9749
- goto out ;
9750
- }
9751
-
9752
- /* Remap node IDs referencing the above */
9753
- for (j = 0 ; j < num_nodes ; j ++ ) {
9754
- pop_id = node_population [j ];
9755
- if (pop_id != TSK_NULL ) {
9756
- node_population [j ] = population_id_map [pop_id ];
9757
- }
9758
- ind_id = node_individual [j ];
9759
- if (ind_id != TSK_NULL ) {
9760
- node_individual [j ] = individual_id_map [ind_id ];
9763
+ if (self -> options & TSK_SIMPLIFY_FILTER_INDIVIDUALS ) {
9764
+ ret = simplifier_finalise_individual_references (self );
9765
+ if (ret != 0 ) {
9766
+ goto out ;
9761
9767
}
9762
9768
}
9769
+
9763
9770
out :
9764
- tsk_safe_free (population_referenced );
9765
- tsk_safe_free (population_id_map );
9766
- tsk_safe_free (individual_referenced );
9767
- tsk_safe_free (individual_id_map );
9768
9771
return ret ;
9769
9772
}
9770
9773
0 commit comments