Skip to content

Commit 019f1c8

Browse files
committed
use bookmark for WF example program
1 parent 58675ad commit 019f1c8

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

examples/haploid_wright_fisher.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,23 @@ use proptest::prelude::*;
88
use rand::distributions::Distribution;
99
use rand::SeedableRng;
1010

11+
fn rotate_edge_table(rotation_point: usize, tables: &mut tskit::TableCollection) {
12+
let num_edges = tables.edges().num_rows().as_usize();
13+
// SAFETY: tables pointer is not null.
14+
let left =
15+
unsafe { std::slice::from_raw_parts_mut((*tables.as_mut_ptr()).edges.left, num_edges) };
16+
let right =
17+
unsafe { std::slice::from_raw_parts_mut((*tables.as_mut_ptr()).edges.right, num_edges) };
18+
let parent =
19+
unsafe { std::slice::from_raw_parts_mut((*tables.as_mut_ptr()).edges.parent, num_edges) };
20+
let child =
21+
unsafe { std::slice::from_raw_parts_mut((*tables.as_mut_ptr()).edges.child, num_edges) };
22+
left.rotate_left(rotation_point);
23+
right.rotate_left(rotation_point);
24+
parent.rotate_left(rotation_point);
25+
child.rotate_left(rotation_point);
26+
}
27+
1128
// ANCHOR: haploid_wright_fisher
1229
fn simulate(
1330
seed: u64,
@@ -46,6 +63,7 @@ fn simulate(
4663
let parent_picker = rand::distributions::Uniform::new(0, popsize);
4764
let breakpoint_generator = rand::distributions::Uniform::new(0.0, 1.0);
4865
let mut rng = rand::rngs::StdRng::seed_from_u64(seed);
66+
let mut bookmark = tskit::types::Bookmark::new();
4967

5068
for birth_time in (0..num_generations).rev() {
5169
for c in children.iter_mut() {
@@ -64,7 +82,10 @@ fn simulate(
6482
}
6583

6684
if birth_time % simplify_interval == 0 {
67-
tables.full_sort(tskit::TableSortOptions::default())?;
85+
tables.sort(&bookmark, 0)?;
86+
if bookmark.edges() > 0 {
87+
rotate_edge_table(bookmark.edges().as_usize(), &mut tables);
88+
}
6889
if let Some(idmap) =
6990
tables.simplify(children, tskit::SimplificationOptions::default(), true)?
7091
{
@@ -73,6 +94,7 @@ fn simulate(
7394
*o = idmap[usize::try_from(*o)?];
7495
}
7596
}
97+
bookmark.set_edges(tables.edges().num_rows());
7698
}
7799
std::mem::swap(&mut parents, &mut children);
78100
}

0 commit comments

Comments
 (0)