Skip to content

Commit

Permalink
use bookmark for WF example program
Browse files Browse the repository at this point in the history
  • Loading branch information
molpopgen committed Jan 11, 2023
1 parent 58675ad commit 019f1c8
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion examples/haploid_wright_fisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ use proptest::prelude::*;
use rand::distributions::Distribution;
use rand::SeedableRng;

fn rotate_edge_table(rotation_point: usize, tables: &mut tskit::TableCollection) {
let num_edges = tables.edges().num_rows().as_usize();
// SAFETY: tables pointer is not null.
let left =
unsafe { std::slice::from_raw_parts_mut((*tables.as_mut_ptr()).edges.left, num_edges) };
let right =
unsafe { std::slice::from_raw_parts_mut((*tables.as_mut_ptr()).edges.right, num_edges) };
let parent =
unsafe { std::slice::from_raw_parts_mut((*tables.as_mut_ptr()).edges.parent, num_edges) };
let child =
unsafe { std::slice::from_raw_parts_mut((*tables.as_mut_ptr()).edges.child, num_edges) };
left.rotate_left(rotation_point);
right.rotate_left(rotation_point);
parent.rotate_left(rotation_point);
child.rotate_left(rotation_point);
}

// ANCHOR: haploid_wright_fisher
fn simulate(
seed: u64,
Expand Down Expand Up @@ -46,6 +63,7 @@ fn simulate(
let parent_picker = rand::distributions::Uniform::new(0, popsize);
let breakpoint_generator = rand::distributions::Uniform::new(0.0, 1.0);
let mut rng = rand::rngs::StdRng::seed_from_u64(seed);
let mut bookmark = tskit::types::Bookmark::new();

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

if birth_time % simplify_interval == 0 {
tables.full_sort(tskit::TableSortOptions::default())?;
tables.sort(&bookmark, 0)?;
if bookmark.edges() > 0 {
rotate_edge_table(bookmark.edges().as_usize(), &mut tables);
}
if let Some(idmap) =
tables.simplify(children, tskit::SimplificationOptions::default(), true)?
{
Expand All @@ -73,6 +94,7 @@ fn simulate(
*o = idmap[usize::try_from(*o)?];
}
}
bookmark.set_edges(tables.edges().num_rows());
}
std::mem::swap(&mut parents, &mut children);
}
Expand Down

0 comments on commit 019f1c8

Please sign in to comment.