@@ -391,6 +391,7 @@ impl Tree {
391
391
392
392
/// Obtain the list of samples for the current tree/tree sequence
393
393
/// as a vector.
394
+ #[ deprecated( since = "0.2.3" , note = "Please use Tree::sample_nodes instead" ) ]
394
395
pub fn samples_to_vec ( & self ) -> Vec < tsk_id_t > {
395
396
let num_samples =
396
397
unsafe { ll_bindings:: tsk_treeseq_get_num_samples ( ( * self . as_ptr ( ) ) . tree_sequence ) } ;
@@ -403,16 +404,34 @@ impl Tree {
403
404
rv
404
405
}
405
406
407
+ /// Get the list of sample nodes as a slice.
408
+ pub fn sample_nodes ( & self ) -> & [ tsk_id_t ] {
409
+ let num_samples =
410
+ unsafe { ll_bindings:: tsk_treeseq_get_num_samples ( ( * self . as_ptr ( ) ) . tree_sequence ) } ;
411
+ tree_array_slice ! ( self , samples, num_samples)
412
+ }
413
+
406
414
/// Return an [`Iterator`] from the node `u` to the root of the tree.
407
415
///
408
416
/// # Errors
409
417
///
410
418
/// [`TskitError::IndexError`] if `u` is out of range.
419
+ #[ deprecated( since = "0.2.3" , note = "Please use Tree::parents instead" ) ]
411
420
pub fn path_to_root (
412
421
& self ,
413
422
u : tsk_id_t ,
414
423
) -> Result < impl Iterator < Item = tsk_id_t > + ' _ , TskitError > {
415
- PathToRootIterator :: new ( self , u)
424
+ self . parents ( u)
425
+ }
426
+
427
+ /// Return an [`Iterator`] from the node `u` to the root of the tree,
428
+ /// travering all parent nodes.
429
+ ///
430
+ /// # Errors
431
+ ///
432
+ /// [`TskitError::IndexError`] if `u` is out of range.
433
+ pub fn parents ( & self , u : tsk_id_t ) -> Result < impl Iterator < Item = tsk_id_t > + ' _ , TskitError > {
434
+ ParentsIterator :: new ( self , u)
416
435
}
417
436
418
437
/// Return an [`Iterator`] over the children of node `u`.
@@ -466,9 +485,13 @@ impl Tree {
466
485
///
467
486
/// * `order`: A value from [`NodeTraversalOrder`] specifying the
468
487
/// iteration order.
469
- pub fn traverse_nodes ( & self , order : NodeTraversalOrder ) -> impl Iterator < Item = tsk_id_t > + ' _ {
488
+ // Return value is dyn for later addition of other traversal orders
489
+ pub fn traverse_nodes (
490
+ & self ,
491
+ order : NodeTraversalOrder ,
492
+ ) -> Box < dyn Iterator < Item = tsk_id_t > + ' _ > {
470
493
match order {
471
- NodeTraversalOrder :: Preorder => PreorderNodeIterator :: new ( & self ) ,
494
+ NodeTraversalOrder :: Preorder => Box :: new ( PreorderNodeIterator :: new ( & self ) ) ,
472
495
}
473
496
}
474
497
@@ -719,17 +742,17 @@ impl NodeIterator for ChildIterator<'_> {
719
742
720
743
iterator_for_nodeiterator ! ( ChildIterator <' _>) ;
721
744
722
- struct PathToRootIterator < ' a > {
745
+ struct ParentsIterator < ' a > {
723
746
current_node : Option < tsk_id_t > ,
724
747
next_node : tsk_id_t ,
725
748
tree : & ' a Tree ,
726
749
}
727
750
728
- impl < ' a > PathToRootIterator < ' a > {
751
+ impl < ' a > ParentsIterator < ' a > {
729
752
fn new ( tree : & ' a Tree , u : tsk_id_t ) -> Result < Self , TskitError > {
730
753
match u >= tree. num_nodes as tsk_id_t {
731
754
true => Err ( TskitError :: IndexError ) ,
732
- false => Ok ( PathToRootIterator {
755
+ false => Ok ( ParentsIterator {
733
756
current_node : None ,
734
757
next_node : u,
735
758
tree,
@@ -738,7 +761,7 @@ impl<'a> PathToRootIterator<'a> {
738
761
}
739
762
}
740
763
741
- impl NodeIterator for PathToRootIterator < ' _ > {
764
+ impl NodeIterator for ParentsIterator < ' _ > {
742
765
fn next_node ( & mut self ) {
743
766
self . current_node = match self . next_node {
744
767
TSK_NULL => None ,
@@ -756,7 +779,7 @@ impl NodeIterator for PathToRootIterator<'_> {
756
779
}
757
780
}
758
781
759
- iterator_for_nodeiterator ! ( PathToRootIterator <' _>) ;
782
+ iterator_for_nodeiterator ! ( ParentsIterator <' _>) ;
760
783
761
784
struct SamplesIterator < ' a > {
762
785
current_node : Option < tsk_id_t > ,
@@ -978,6 +1001,10 @@ impl TreeSequence {
978
1001
}
979
1002
980
1003
/// Get the list of samples as a vector.
1004
+ #[ deprecated(
1005
+ since = "0.2.3" ,
1006
+ note = "Please use TreeSequence::sample_nodes instead"
1007
+ ) ]
981
1008
pub fn samples_to_vec ( & self ) -> Vec < tsk_id_t > {
982
1009
let num_samples = unsafe { ll_bindings:: tsk_treeseq_get_num_samples ( self . as_ptr ( ) ) } ;
983
1010
let mut rv = vec ! [ ] ;
@@ -989,6 +1016,12 @@ impl TreeSequence {
989
1016
rv
990
1017
}
991
1018
1019
+ /// Get the list of sample nodes as a slice.
1020
+ pub fn sample_nodes ( & self ) -> & [ tsk_id_t ] {
1021
+ let num_samples = unsafe { ll_bindings:: tsk_treeseq_get_num_samples ( self . as_ptr ( ) ) } ;
1022
+ tree_array_slice ! ( self , samples, num_samples)
1023
+ }
1024
+
992
1025
/// Get the number of trees.
993
1026
pub fn num_trees ( & self ) -> tsk_size_t {
994
1027
unsafe { ll_bindings:: tsk_treeseq_get_num_trees ( self . as_ptr ( ) ) }
@@ -1146,7 +1179,7 @@ pub(crate) mod test_trees {
1146
1179
fn test_create_treeseq_new_from_tables ( ) {
1147
1180
let tables = make_small_table_collection ( ) ;
1148
1181
let treeseq = TreeSequence :: new ( tables, TreeSequenceFlags :: default ( ) ) . unwrap ( ) ;
1149
- let samples = treeseq. samples_to_vec ( ) ;
1182
+ let samples = treeseq. sample_nodes ( ) ;
1150
1183
assert_eq ! ( samples. len( ) , 2 ) ;
1151
1184
for i in 1 ..3 {
1152
1185
assert_eq ! ( samples[ i - 1 ] , i as tsk_id_t) ;
@@ -1168,13 +1201,13 @@ pub(crate) mod test_trees {
1168
1201
while let Some ( tree) = tree_iter. next ( ) {
1169
1202
ntrees += 1 ;
1170
1203
assert_eq ! ( tree. current_tree, ntrees) ;
1171
- let samples = tree. samples_to_vec ( ) ;
1204
+ let samples = tree. sample_nodes ( ) ;
1172
1205
assert_eq ! ( samples. len( ) , 2 ) ;
1173
1206
for i in 1 ..3 {
1174
1207
assert_eq ! ( samples[ i - 1 ] , i as tsk_id_t) ;
1175
1208
1176
1209
let mut nsteps = 0 ;
1177
- for _ in tree. path_to_root ( samples[ i - 1 ] ) . unwrap ( ) {
1210
+ for _ in tree. parents ( samples[ i - 1 ] ) . unwrap ( ) {
1178
1211
nsteps += 1 ;
1179
1212
}
1180
1213
assert_eq ! ( nsteps, 2 ) ;
0 commit comments