1- #![ cfg( feature="bindings" ) ]
2-
31// This is a rust implementation of the example
42// found in tskit-c
53
@@ -12,10 +10,19 @@ use rand::distributions::Distribution;
1210use rand:: prelude:: * ;
1311use rand:: SeedableRng ;
1412
13+ #[ derive( Debug ) ]
14+ struct Edge {
15+ left : tskit:: Position ,
16+ right : tskit:: Position ,
17+ child : tskit:: NodeId ,
18+ previous : Option < usize > ,
19+ }
20+
1521#[ derive( Default ) ]
1622struct EdgeBuffer {
1723 parent : Vec < tskit:: NodeId > ,
18- edges : HashMap < tskit:: NodeId , tskit:: OwningEdgeTable > ,
24+ last : HashMap < tskit:: NodeId , usize > ,
25+ edges : Vec < Edge > ,
1926}
2027
2128impl EdgeBuffer {
@@ -26,16 +33,34 @@ impl EdgeBuffer {
2633 parent : tskit:: NodeId ,
2734 child : tskit:: NodeId ,
2835 ) -> Result < ( ) > {
29- if let Some ( ref mut edges) = self . edges . get_mut ( & parent) {
30- edges. add_row ( left, right, parent, child) ?;
36+ if let Some ( last) = self . last . get_mut ( & parent) {
37+ let pchild = self . edges [ * last] . child ;
38+ assert ! ( child >= pchild) ;
39+ self . edges . push ( Edge {
40+ left,
41+ right,
42+ child,
43+ previous : Some ( * last) ,
44+ } ) ;
45+ * last = self . edges . len ( ) - 1 ;
3146 } else {
32- let mut edges = tskit:: OwningEdgeTable :: default ( ) ;
33- edges. add_row ( left, right, parent, child) ?;
34- self . edges . insert ( parent, edges) ;
47+ self . edges . push ( Edge {
48+ left,
49+ right,
50+ child,
51+ previous : None ,
52+ } ) ;
53+ self . last . insert ( parent, self . edges . len ( ) - 1 ) ;
3554 self . parent . push ( parent) ;
3655 }
3756 Ok ( ( ) )
3857 }
58+
59+ fn clear ( & mut self ) {
60+ self . parent . clear ( ) ;
61+ self . last . clear ( ) ;
62+ self . edges . clear ( ) ;
63+ }
3964}
4065
4166fn rotate_edges ( bookmark : & tskit:: types:: Bookmark , tables : & mut tskit:: TableCollection ) {
@@ -97,6 +122,7 @@ fn simulate(
97122
98123 let mut buffer = EdgeBuffer :: default ( ) ;
99124 for birth_time in ( 0 ..num_generations) . rev ( ) {
125+ println ! ( "bt = {birth_time:?}" ) ;
100126 for c in children. iter_mut ( ) {
101127 let bt = f64:: from ( birth_time) ;
102128 let child = tables. add_node ( 0 , bt, -1 , -1 ) ?;
@@ -115,27 +141,19 @@ fn simulate(
115141 if birth_time % simplify_interval == 0 {
116142 //tables.sort(&bookmark, tskit::TableSortOptions::default())?;
117143 for & parent in buffer. parent . iter ( ) . rev ( ) {
118- // remove the buffered edges from the internal hash
119- // NOTE: we unwrap here b/c our impl of EdgeBuffer guarantees that
120- // the object exists in the hash
121- let edges = buffer. edges . remove ( & parent) . unwrap ( ) ;
122- println ! ( "{:?}" , edges. num_rows( ) ) ;
123- let rv = unsafe {
124- tskit:: bindings:: tsk_edge_table_append_columns (
125- & mut ( * tables. as_mut_ptr ( ) ) . edges ,
126- edges. num_rows ( ) . into ( ) ,
127- ( * edges. as_ptr ( ) ) . left ,
128- ( * edges. as_ptr ( ) ) . right ,
129- ( * edges. as_ptr ( ) ) . parent ,
130- ( * edges. as_ptr ( ) ) . child ,
131- ( * edges. as_ptr ( ) ) . metadata ,
132- ( * edges. as_ptr ( ) ) . metadata_offset ,
133- )
134- } ;
135- assert ! ( rv >= 0 ) ;
144+ println ! ( "{parent:?}" ) ;
145+ let mut last = buffer. last . get ( & parent) . cloned ( ) ;
146+ while let Some ( previous) = last {
147+ let edge = & buffer. edges [ previous] ;
148+ tables. add_edge ( edge. left , edge. right , parent, edge. child ) ?;
149+ println ! ( "adding {edge:?}" ) ;
150+ tables. check_integrity (
151+ tskit:: TableIntegrityCheckFlags :: default ( ) . check_edge_ordering ( ) ,
152+ ) ?;
153+ last = edge. previous ;
154+ }
136155 }
137- buffer. parent . clear ( ) ;
138- buffer. edges . clear ( ) ;
156+ buffer. clear ( ) ;
139157 rotate_edges ( & bookmark, & mut tables) ;
140158 if let Some ( idmap) =
141159 tables. simplify ( children, tskit:: SimplificationOptions :: default ( ) , true ) ?
0 commit comments