@@ -5,7 +5,6 @@ use crate::EdgeTable;
5
5
use crate :: IndividualTable ;
6
6
use crate :: MigrationTable ;
7
7
use crate :: MutationTable ;
8
- use crate :: NodeIterator ;
9
8
use crate :: NodeTable ;
10
9
use crate :: PopulationTable ;
11
10
use crate :: SimplificationOptions ;
@@ -30,7 +29,11 @@ pub struct Tree {
30
29
flags : TreeFlags ,
31
30
}
32
31
33
- pub type BoxedNodeIterator = Box < dyn NodeIterator > ;
32
+ // Trait defining iteration over nodes.
33
+ trait NodeIterator {
34
+ fn next_node ( & mut self ) ;
35
+ fn current_node ( & mut self ) -> Option < tsk_id_t > ;
36
+ }
34
37
35
38
drop_for_tskit_type ! ( Tree , tsk_tree_free) ;
36
39
tskit_type_access ! ( Tree , ll_bindings:: tsk_tree_t) ;
@@ -203,26 +206,27 @@ impl Tree {
203
206
rv
204
207
}
205
208
206
- /// Return a [`NodeIterator `] from the node `u` to the root of the tree.
209
+ /// Return an [`Iterator `] from the node `u` to the root of the tree.
207
210
///
208
211
/// # Errors
209
212
///
210
213
/// [`TskitError::IndexError`] if `u` is out of range.
211
- pub fn path_to_root ( & self , u : tsk_id_t ) -> Result < BoxedNodeIterator , TskitError > {
212
- let iter = PathToRootIterator :: new ( self , u) ?;
213
- Ok ( Box :: new ( iter) )
214
+ pub fn path_to_root (
215
+ & self ,
216
+ u : tsk_id_t ,
217
+ ) -> Result < impl Iterator < Item = tsk_id_t > + ' _ , TskitError > {
218
+ PathToRootIterator :: new ( self , u)
214
219
}
215
220
216
- /// Return a [`NodeIterator `] over the children of node `u`.
221
+ /// Return an [`Iterator `] over the children of node `u`.
217
222
///
218
223
/// # Errors
219
224
///
220
225
/// [`TskitError::IndexError`] if `u` is out of range.
221
- pub fn children ( & self , u : tsk_id_t ) -> Result < BoxedNodeIterator , TskitError > {
222
- let iter = ChildIterator :: new ( self , u) ?;
223
- Ok ( Box :: new ( iter) )
226
+ pub fn children ( & self , u : tsk_id_t ) -> Result < impl Iterator < Item = tsk_id_t > + ' _ , TskitError > {
227
+ ChildIterator :: new ( & self , u)
224
228
}
225
- /// Return a [`NodeIterator `] over the sample nodes descending from node `u`.
229
+ /// Return an [`Iterator `] over the sample nodes descending from node `u`.
226
230
///
227
231
/// # Note
228
232
///
@@ -234,19 +238,18 @@ impl Tree {
234
238
///
235
239
/// [`TskitError::NotTrackingSamples`] if [`TreeFlags::SAMPLE_LISTS`] was not used
236
240
/// to initialize `self`.
237
- pub fn samples ( & self , u : tsk_id_t ) -> Result < BoxedNodeIterator , TskitError > {
238
- let iter = SamplesIterator :: new ( self , u) ?;
239
- Ok ( Box :: new ( iter) )
241
+ pub fn samples ( & self , u : tsk_id_t ) -> Result < impl Iterator < Item = tsk_id_t > + ' _ , TskitError > {
242
+ SamplesIterator :: new ( self , u)
240
243
}
241
244
242
- /// Return a [`NodeIterator `] over the roots of the tree.
245
+ /// Return an [`Iterator `] over the roots of the tree.
243
246
///
244
247
/// # Note
245
248
///
246
249
/// For a tree with multiple roots, the iteration starts
247
250
/// at the left root.
248
- pub fn roots ( & self ) -> BoxedNodeIterator {
249
- Box :: new ( RootIterator :: new ( self ) )
251
+ pub fn roots ( & self ) -> impl Iterator < Item = tsk_id_t > + ' _ {
252
+ RootIterator :: new ( self )
250
253
}
251
254
252
255
/// Return all roots as a vector.
@@ -260,15 +263,15 @@ impl Tree {
260
263
v
261
264
}
262
265
263
- /// Return a [`NodeIterator `] over all nodes in the tree.
266
+ /// Return an [`Iterator `] over all nodes in the tree.
264
267
///
265
268
/// # Parameters
266
269
///
267
270
/// * `order`: A value from [`NodeTraversalOrder`] specifying the
268
271
/// iteration order.
269
- pub fn traverse_nodes ( & self , order : NodeTraversalOrder ) -> BoxedNodeIterator {
272
+ pub fn traverse_nodes ( & self , order : NodeTraversalOrder ) -> impl Iterator < Item = tsk_id_t > + ' _ {
270
273
match order {
271
- NodeTraversalOrder :: Preorder => Box :: new ( PreorderNodeIterator :: new ( & self ) ) ,
274
+ NodeTraversalOrder :: Preorder => PreorderNodeIterator :: new ( & self ) ,
272
275
}
273
276
}
274
277
@@ -396,21 +399,19 @@ pub enum NodeTraversalOrder {
396
399
Preorder ,
397
400
}
398
401
399
- struct PreorderNodeIterator {
402
+ struct PreorderNodeIterator < ' a > {
400
403
root_stack : Vec < i32 > ,
401
404
node_stack : Vec < i32 > ,
402
- left_child : crate :: ffi:: TskIdArray ,
403
- right_sib : crate :: ffi:: TskIdArray ,
405
+ tree : & ' a Tree ,
404
406
current_node_ : Option < tsk_id_t > ,
405
407
}
406
408
407
- impl PreorderNodeIterator {
408
- fn new ( tree : & Tree ) -> Self {
409
+ impl < ' a > PreorderNodeIterator < ' a > {
410
+ fn new ( tree : & ' a Tree ) -> Self {
409
411
let mut rv = PreorderNodeIterator {
410
412
root_stack : tree. roots_to_vec ( ) ,
411
413
node_stack : vec ! [ ] ,
412
- left_child : tree. left_child_array ( ) ,
413
- right_sib : tree. right_sib_array ( ) ,
414
+ tree,
414
415
current_node_ : None ,
415
416
} ;
416
417
rv. root_stack . reverse ( ) ;
@@ -421,15 +422,15 @@ impl PreorderNodeIterator {
421
422
}
422
423
}
423
424
424
- impl NodeIterator for PreorderNodeIterator {
425
+ impl NodeIterator for PreorderNodeIterator < ' _ > {
425
426
fn next_node ( & mut self ) {
426
427
self . current_node_ = self . node_stack . pop ( ) ;
427
428
match self . current_node_ {
428
429
Some ( u) => {
429
- let mut c = self . left_child [ u ] ;
430
+ let mut c = self . tree . left_child ( u ) . unwrap ( ) ;
430
431
while c != TSK_NULL {
431
432
self . node_stack . push ( c) ;
432
- c = self . right_sib [ c ] ;
433
+ c = self . tree . right_sib ( c ) . unwrap ( ) ;
433
434
}
434
435
}
435
436
None => {
@@ -445,30 +446,32 @@ impl NodeIterator for PreorderNodeIterator {
445
446
}
446
447
}
447
448
448
- struct RootIterator {
449
+ iterator_for_nodeiterator ! ( PreorderNodeIterator <' _>) ;
450
+
451
+ struct RootIterator < ' a > {
449
452
current_root : Option < tsk_id_t > ,
450
453
next_root : tsk_id_t ,
451
- right_sib : crate :: ffi :: TskIdArray ,
454
+ tree : & ' a Tree ,
452
455
}
453
456
454
- impl RootIterator {
455
- fn new ( tree : & Tree ) -> Self {
457
+ impl < ' a > RootIterator < ' a > {
458
+ fn new ( tree : & ' a Tree ) -> Self {
456
459
RootIterator {
457
460
current_root : None ,
458
461
next_root : tree. inner . left_root ,
459
- right_sib : tree. right_sib_array ( ) ,
462
+ tree,
460
463
}
461
464
}
462
465
}
463
466
464
- impl NodeIterator for RootIterator {
467
+ impl NodeIterator for RootIterator < ' _ > {
465
468
fn next_node ( & mut self ) {
466
469
self . current_root = match self . next_root {
467
470
TSK_NULL => None ,
468
471
r => {
469
472
assert ! ( r >= 0 ) ;
470
473
let cr = Some ( r) ;
471
- self . next_root = self . right_sib [ r ] ;
474
+ self . next_root = self . tree . right_sib ( r ) . unwrap ( ) ;
472
475
cr
473
476
}
474
477
} ;
@@ -479,32 +482,34 @@ impl NodeIterator for RootIterator {
479
482
}
480
483
}
481
484
482
- struct ChildIterator {
485
+ iterator_for_nodeiterator ! ( RootIterator <' _>) ;
486
+
487
+ struct ChildIterator < ' a > {
483
488
current_child : Option < tsk_id_t > ,
484
489
next_child : tsk_id_t ,
485
- right_sib : crate :: ffi :: TskIdArray ,
490
+ tree : & ' a Tree ,
486
491
}
487
492
488
- impl ChildIterator {
489
- fn new ( tree : & Tree , u : tsk_id_t ) -> Result < Self , TskitError > {
493
+ impl < ' a > ChildIterator < ' a > {
494
+ fn new ( tree : & ' a Tree , u : tsk_id_t ) -> Result < Self , TskitError > {
490
495
let c = tree. left_child ( u) ?;
491
496
492
497
Ok ( ChildIterator {
493
498
current_child : None ,
494
499
next_child : c,
495
- right_sib : tree. right_sib_array ( ) ,
500
+ tree,
496
501
} )
497
502
}
498
503
}
499
504
500
- impl NodeIterator for ChildIterator {
505
+ impl NodeIterator for ChildIterator < ' _ > {
501
506
fn next_node ( & mut self ) {
502
507
self . current_child = match self . next_child {
503
508
TSK_NULL => None ,
504
509
r => {
505
510
assert ! ( r >= 0 ) ;
506
511
let cr = Some ( r) ;
507
- self . next_child = self . right_sib [ r ] ;
512
+ self . next_child = self . tree . right_sib ( r ) . unwrap ( ) ;
508
513
cr
509
514
}
510
515
} ;
@@ -515,33 +520,35 @@ impl NodeIterator for ChildIterator {
515
520
}
516
521
}
517
522
518
- struct PathToRootIterator {
523
+ iterator_for_nodeiterator ! ( ChildIterator <' _>) ;
524
+
525
+ struct PathToRootIterator < ' a > {
519
526
current_node : Option < tsk_id_t > ,
520
527
next_node : tsk_id_t ,
521
- parent : crate :: ffi :: TskIdArray ,
528
+ tree : & ' a Tree ,
522
529
}
523
530
524
- impl PathToRootIterator {
525
- fn new ( tree : & Tree , u : tsk_id_t ) -> Result < Self , TskitError > {
531
+ impl < ' a > PathToRootIterator < ' a > {
532
+ fn new ( tree : & ' a Tree , u : tsk_id_t ) -> Result < Self , TskitError > {
526
533
match u >= tree. num_nodes as tsk_id_t {
527
534
true => Err ( TskitError :: IndexError ) ,
528
535
false => Ok ( PathToRootIterator {
529
536
current_node : None ,
530
537
next_node : u,
531
- parent : tree. parent_array ( ) ,
538
+ tree,
532
539
} ) ,
533
540
}
534
541
}
535
542
}
536
543
537
- impl NodeIterator for PathToRootIterator {
544
+ impl NodeIterator for PathToRootIterator < ' _ > {
538
545
fn next_node ( & mut self ) {
539
546
self . current_node = match self . next_node {
540
547
TSK_NULL => None ,
541
548
r => {
542
549
assert ! ( r >= 0 ) ;
543
550
let cr = Some ( r) ;
544
- self . next_node = self . parent [ r ] ;
551
+ self . next_node = self . tree . parent ( r ) . unwrap ( ) ;
545
552
cr
546
553
}
547
554
} ;
@@ -552,41 +559,47 @@ impl NodeIterator for PathToRootIterator {
552
559
}
553
560
}
554
561
555
- struct SamplesIterator {
562
+ iterator_for_nodeiterator ! ( PathToRootIterator <' _>) ;
563
+
564
+ struct SamplesIterator < ' a > {
556
565
current_node : Option < tsk_id_t > ,
557
566
next_sample_index : tsk_id_t ,
558
567
last_sample_index : tsk_id_t ,
559
- next_sample : crate :: ffi:: TskIdArray ,
560
- samples : crate :: ffi:: TskIdArray ,
568
+ tree : & ' a Tree ,
569
+ //next_sample: crate::ffi::TskIdArray,
570
+ //samples: crate::ffi::TskIdArray,
561
571
}
562
572
563
- impl SamplesIterator {
564
- fn new ( tree : & Tree , u : tsk_id_t ) -> Result < Self , TskitError > {
573
+ impl < ' a > SamplesIterator < ' a > {
574
+ fn new ( tree : & ' a Tree , u : tsk_id_t ) -> Result < Self , TskitError > {
565
575
let rv = SamplesIterator {
566
576
current_node : None ,
567
577
next_sample_index : tree. left_sample ( u) ?,
568
578
last_sample_index : tree. right_sample ( u) ?,
569
- next_sample : tree. next_sample_array ( ) ?,
570
- samples : tree. samples_array ( ) ?,
579
+ tree,
571
580
} ;
572
581
573
582
Ok ( rv)
574
583
}
575
584
}
576
585
577
- impl NodeIterator for SamplesIterator {
586
+ impl NodeIterator for SamplesIterator < ' _ > {
578
587
fn next_node ( & mut self ) {
579
588
self . current_node = match self . next_sample_index {
580
589
TSK_NULL => None ,
581
590
r => {
582
591
if r == self . last_sample_index {
583
- let cr = Some ( self . samples [ r] ) ;
592
+ //let cr = Some(self.tree.samples(r).unwrap());
593
+ let cr = Some ( unsafe { * ( * self . tree . as_ptr ( ) ) . samples . offset ( r as isize ) } ) ;
584
594
self . next_sample_index = TSK_NULL ;
585
595
cr
586
596
} else {
587
597
assert ! ( r >= 0 ) ;
588
- let cr = Some ( self . samples [ r] ) ;
589
- self . next_sample_index = self . next_sample [ r] ;
598
+ //let cr = Some(self.tree.samples(r).unwrap());
599
+ let cr = Some ( unsafe { * ( * self . tree . as_ptr ( ) ) . samples . offset ( r as isize ) } ) ;
600
+ //self.next_sample_index = self.next_sample[r];
601
+ self . next_sample_index =
602
+ unsafe { * ( * self . tree . as_ptr ( ) ) . next_sample . offset ( r as isize ) } ;
590
603
cr
591
604
}
592
605
}
@@ -598,6 +611,8 @@ impl NodeIterator for SamplesIterator {
598
611
}
599
612
}
600
613
614
+ iterator_for_nodeiterator ! ( SamplesIterator <' _>) ;
615
+
601
616
/// A tree sequence.
602
617
///
603
618
/// This is a thin wrapper around the C type `tsk_treeseq_t`.
0 commit comments