@@ -20,7 +20,7 @@ use crate::TreeSequenceFlags;
2020use crate :: TskReturnValue ;
2121use crate :: TskitTypeAccess ;
2222use crate :: { tsk_flags_t, tsk_id_t, tsk_size_t, TSK_NULL } ;
23- use crate :: { IndividualId , NodeId } ;
23+ use crate :: { IndividualId , NodeId , PopulationId } ;
2424use ll_bindings:: tsk_table_collection_free;
2525
2626/// A table collection.
@@ -241,11 +241,11 @@ impl TableCollection {
241241 ///
242242 /// Migration tables are not currently supported
243243 /// by tree sequence simplification.
244- pub fn add_migration < N : Into < NodeId > > (
244+ pub fn add_migration < N : Into < NodeId > , SOURCE : Into < PopulationId > , DEST : Into < PopulationId > > (
245245 & mut self ,
246246 span : ( f64 , f64 ) ,
247247 node : N ,
248- source_dest : ( tsk_id_t , tsk_id_t ) ,
248+ source_dest : ( SOURCE , DEST ) ,
249249 time : f64 ,
250250 ) -> TskReturnValue {
251251 self . add_migration_with_metadata ( span, node, source_dest, time, None )
@@ -257,11 +257,15 @@ impl TableCollection {
257257 ///
258258 /// Migration tables are not currently supported
259259 /// by tree sequence simplification.
260- pub fn add_migration_with_metadata < N : Into < NodeId > > (
260+ pub fn add_migration_with_metadata <
261+ N : Into < NodeId > ,
262+ SOURCE : Into < PopulationId > ,
263+ DEST : Into < PopulationId > ,
264+ > (
261265 & mut self ,
262266 span : ( f64 , f64 ) ,
263267 node : N ,
264- source_dest : ( tsk_id_t , tsk_id_t ) ,
268+ source_dest : ( SOURCE , DEST ) ,
265269 time : f64 ,
266270 metadata : Option < & dyn MetadataRoundtrip > ,
267271 ) -> TskReturnValue {
@@ -272,8 +276,8 @@ impl TableCollection {
272276 span. 0 ,
273277 span. 1 ,
274278 node. into ( ) . 0 ,
275- source_dest. 0 ,
276- source_dest. 1 ,
279+ source_dest. 0 . into ( ) . 0 ,
280+ source_dest. 1 . into ( ) . 0 ,
277281 time,
278282 md. as_ptr ( ) ,
279283 md. len ( ) ,
@@ -283,22 +287,22 @@ impl TableCollection {
283287 }
284288
285289 /// Add a row to the node table
286- pub fn add_node < I : Into < IndividualId > > (
290+ pub fn add_node < I : Into < IndividualId > , POP : Into < PopulationId > > (
287291 & mut self ,
288292 flags : ll_bindings:: tsk_flags_t ,
289293 time : f64 ,
290- population : tsk_id_t ,
294+ population : POP ,
291295 individual : I ,
292296 ) -> Result < NodeId , TskitError > {
293297 self . add_node_with_metadata ( flags, time, population, individual, None )
294298 }
295299
296300 /// Add a row with metadata to the node table
297- pub fn add_node_with_metadata < I : Into < IndividualId > > (
301+ pub fn add_node_with_metadata < I : Into < IndividualId > , POP : Into < PopulationId > > (
298302 & mut self ,
299303 flags : ll_bindings:: tsk_flags_t ,
300304 time : f64 ,
301- population : tsk_id_t ,
305+ population : POP ,
302306 individual : I ,
303307 metadata : Option < & dyn MetadataRoundtrip > ,
304308 ) -> Result < NodeId , TskitError > {
@@ -308,7 +312,7 @@ impl TableCollection {
308312 & mut ( * self . as_mut_ptr ( ) ) . nodes ,
309313 flags,
310314 time,
311- population,
315+ population. into ( ) . 0 ,
312316 individual. into ( ) . 0 ,
313317 md. as_ptr ( ) ,
314318 md. len ( ) ,
@@ -390,15 +394,15 @@ impl TableCollection {
390394 }
391395
392396 /// Add a row to the population_table
393- pub fn add_population ( & mut self ) -> TskReturnValue {
397+ pub fn add_population ( & mut self ) -> Result < PopulationId , TskitError > {
394398 self . add_population_with_metadata ( None )
395399 }
396400
397401 /// Add a row with metadata to the population_table
398402 pub fn add_population_with_metadata (
399403 & mut self ,
400404 metadata : Option < & dyn MetadataRoundtrip > ,
401- ) -> TskReturnValue {
405+ ) -> Result < PopulationId , TskitError > {
402406 let md = EncodedMetadata :: new ( metadata) ?;
403407 let rv = unsafe {
404408 ll_bindings:: tsk_population_table_add_row (
@@ -408,7 +412,7 @@ impl TableCollection {
408412 )
409413 } ;
410414
411- handle_tsk_return_value ! ( rv)
415+ handle_tsk_return_value ! ( rv, PopulationId :: from ( rv ) )
412416 }
413417
414418 /// Build the "input" and "output"
@@ -1030,30 +1034,33 @@ mod test {
10301034 #[ test]
10311035 fn test_add_population ( ) {
10321036 let mut tables = TableCollection :: new ( 1000. ) . unwrap ( ) ;
1033- tables. add_population ( ) . unwrap ( ) ;
1037+ let pop_id = tables. add_population ( ) . unwrap ( ) ;
1038+ assert_eq ! ( pop_id, 0 ) ;
10341039 assert_eq ! ( tables. populations( ) . num_rows( ) , 1 ) ;
1040+
1041+ tables
1042+ . add_node ( crate :: TSK_NODE_IS_SAMPLE , 0.0 , pop_id, crate :: TSK_NULL )
1043+ . unwrap ( ) ;
1044+
1045+ match tables. nodes ( ) . row ( NodeId :: from ( 0 ) ) {
1046+ Ok ( x) => match x. population {
1047+ PopulationId ( 0 ) => ( ) ,
1048+ _ => panic ! ( "expected PopulationId(0)" ) ,
1049+ } ,
1050+ Err ( _) => panic ! ( "expected Ok(_)" ) ,
1051+ } ;
10351052 }
10361053
10371054 #[ test]
10381055 fn test_dump_tables ( ) {
10391056 let treefile = "trees.trees" ;
10401057 let mut tables = TableCollection :: new ( 1000. ) . unwrap ( ) ;
1041- tables. add_population ( ) . unwrap ( ) ;
1058+ let pop_id = tables. add_population ( ) . unwrap ( ) ;
10421059 tables
1043- . add_node (
1044- crate :: TSK_NODE_IS_SAMPLE ,
1045- 0.0 ,
1046- crate :: TSK_NULL ,
1047- crate :: TSK_NULL ,
1048- )
1060+ . add_node ( crate :: TSK_NODE_IS_SAMPLE , 0.0 , pop_id, crate :: TSK_NULL )
10491061 . unwrap ( ) ;
10501062 tables
1051- . add_node (
1052- crate :: TSK_NODE_IS_SAMPLE ,
1053- 1.0 ,
1054- crate :: TSK_NULL ,
1055- crate :: TSK_NULL ,
1056- )
1063+ . add_node ( crate :: TSK_NODE_IS_SAMPLE , 1.0 , pop_id, crate :: TSK_NULL )
10571064 . unwrap ( ) ;
10581065 tables. add_edge ( 0. , tables. sequence_length ( ) , 1 , 0 ) . unwrap ( ) ;
10591066 tables
0 commit comments