Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TreeSequence::tables #648

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/sys/table_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ impl TableCollection {
Self(TskBox::new_init_owning_from_ptr(tables))
}

pub unsafe fn new_borrowed(tables: std::ptr::NonNull<tsk_table_collection_t>) -> Self {
Self(TskBox::new_init_from_ptr(tables))
}

// # Safety
//
// The returned value is uninitialized.
Expand Down
39 changes: 37 additions & 2 deletions src/trees/treeseq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ use super::Tree;
/// ```
pub struct TreeSequence {
pub(crate) inner: sys::TreeSequence,
tables: crate::TableCollection,
views: crate::table_views::TableViews,
}

Expand Down Expand Up @@ -115,7 +116,16 @@ impl TreeSequence {
let raw_tables_ptr = tables.into_inner();
let mut inner = sys::TreeSequence::new(raw_tables_ptr, flags.into())?;
let views = crate::table_views::TableViews::new_from_tree_sequence(inner.as_mut())?;
Ok(Self { inner, views })
let tables = unsafe {
TableCollection::new_from_ll(sys::TableCollection::new_borrowed(
std::ptr::NonNull::new(inner.as_mut().tables).unwrap(),
))
}?;
Ok(Self {
inner,
tables,
views,
})
}

fn as_ref(&self) -> &ll_bindings::tsk_treeseq_t {
Expand Down Expand Up @@ -335,8 +345,17 @@ impl TreeSequence {
},
)?;
let views = crate::table_views::TableViews::new_from_tree_sequence(inner.as_mut())?;
let tables = unsafe {
TableCollection::new_from_ll(sys::TableCollection::new_borrowed(
std::ptr::NonNull::new(inner.as_mut().tables).unwrap(),
))
}?;
Ok((
Self { inner, views },
Self {
inner,
tables,
views,
},
match idmap {
true => Some(output_node_map),
false => None,
Expand Down Expand Up @@ -473,6 +492,22 @@ impl TreeSequence {
) -> Result<crate::edge_differences::EdgeDifferencesIterator, TskitError> {
crate::edge_differences::EdgeDifferencesIterator::new_from_treeseq(self, 0)
}

/// Reference to the underlying table collection.
///
/// # Examples
///
/// ```
/// let mut tables = tskit::TableCollection::new(1000.).unwrap();
/// tables.add_node(tskit::NodeFlags::default(),0.0, -1, -1).unwrap();
/// tables.build_index();
/// let tcopy = tables.deepcopy().unwrap();
/// let tree_sequence = tskit::TreeSequence::try_from(tcopy).unwrap();
/// assert_eq!(tables.equals(tree_sequence.tables(), 0), true);
/// ```
pub fn tables(&self) -> &TableCollection {
&self.tables
}
}

impl TryFrom<TableCollection> for TreeSequence {
Expand Down
Loading