1
- #![ cfg( feature="bindings" ) ]
2
-
3
1
// This is a rust implementation of the example
4
2
// found in tskit-c
5
3
@@ -12,10 +10,19 @@ use rand::distributions::Distribution;
12
10
use rand:: prelude:: * ;
13
11
use rand:: SeedableRng ;
14
12
13
+ #[ derive( Debug ) ]
14
+ struct Edge {
15
+ left : tskit:: Position ,
16
+ right : tskit:: Position ,
17
+ child : tskit:: NodeId ,
18
+ previous : Option < usize > ,
19
+ }
20
+
15
21
#[ derive( Default ) ]
16
22
struct EdgeBuffer {
17
23
parent : Vec < tskit:: NodeId > ,
18
- edges : HashMap < tskit:: NodeId , tskit:: OwningEdgeTable > ,
24
+ last : HashMap < tskit:: NodeId , usize > ,
25
+ edges : Vec < Edge > ,
19
26
}
20
27
21
28
impl EdgeBuffer {
@@ -26,16 +33,34 @@ impl EdgeBuffer {
26
33
parent : tskit:: NodeId ,
27
34
child : tskit:: NodeId ,
28
35
) -> 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 ;
31
46
} 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 ) ;
35
54
self . parent . push ( parent) ;
36
55
}
37
56
Ok ( ( ) )
38
57
}
58
+
59
+ fn clear ( & mut self ) {
60
+ self . parent . clear ( ) ;
61
+ self . last . clear ( ) ;
62
+ self . edges . clear ( ) ;
63
+ }
39
64
}
40
65
41
66
fn rotate_edges ( bookmark : & tskit:: types:: Bookmark , tables : & mut tskit:: TableCollection ) {
@@ -97,6 +122,7 @@ fn simulate(
97
122
98
123
let mut buffer = EdgeBuffer :: default ( ) ;
99
124
for birth_time in ( 0 ..num_generations) . rev ( ) {
125
+ println ! ( "bt = {birth_time:?}" ) ;
100
126
for c in children. iter_mut ( ) {
101
127
let bt = f64:: from ( birth_time) ;
102
128
let child = tables. add_node ( 0 , bt, -1 , -1 ) ?;
@@ -115,27 +141,19 @@ fn simulate(
115
141
if birth_time % simplify_interval == 0 {
116
142
//tables.sort(&bookmark, tskit::TableSortOptions::default())?;
117
143
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
+ }
136
155
}
137
- buffer. parent . clear ( ) ;
138
- buffer. edges . clear ( ) ;
156
+ buffer. clear ( ) ;
139
157
rotate_edges ( & bookmark, & mut tables) ;
140
158
if let Some ( idmap) =
141
159
tables. simplify ( children, tskit:: SimplificationOptions :: default ( ) , true ) ?
0 commit comments