Skip to content

Commit edc8ffc

Browse files
authored
feat: add TreeSequence::tables (#648)
1 parent 90603cf commit edc8ffc

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/sys/table_collection.rs

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ impl TableCollection {
3232
Self(TskBox::new_init_owning_from_ptr(tables))
3333
}
3434

35+
pub unsafe fn new_borrowed(tables: std::ptr::NonNull<tsk_table_collection_t>) -> Self {
36+
Self(TskBox::new_init_from_ptr(tables))
37+
}
38+
3539
// # Safety
3640
//
3741
// The returned value is uninitialized.

src/trees/treeseq.rs

+37-2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use super::Tree;
5858
/// ```
5959
pub struct TreeSequence {
6060
pub(crate) inner: sys::TreeSequence,
61+
tables: crate::TableCollection,
6162
views: crate::table_views::TableViews,
6263
}
6364

@@ -115,7 +116,16 @@ impl TreeSequence {
115116
let raw_tables_ptr = tables.into_inner();
116117
let mut inner = sys::TreeSequence::new(raw_tables_ptr, flags.into())?;
117118
let views = crate::table_views::TableViews::new_from_tree_sequence(inner.as_mut())?;
118-
Ok(Self { inner, views })
119+
let tables = unsafe {
120+
TableCollection::new_from_ll(sys::TableCollection::new_borrowed(
121+
std::ptr::NonNull::new(inner.as_mut().tables).unwrap(),
122+
))
123+
}?;
124+
Ok(Self {
125+
inner,
126+
tables,
127+
views,
128+
})
119129
}
120130

121131
fn as_ref(&self) -> &ll_bindings::tsk_treeseq_t {
@@ -335,8 +345,17 @@ impl TreeSequence {
335345
},
336346
)?;
337347
let views = crate::table_views::TableViews::new_from_tree_sequence(inner.as_mut())?;
348+
let tables = unsafe {
349+
TableCollection::new_from_ll(sys::TableCollection::new_borrowed(
350+
std::ptr::NonNull::new(inner.as_mut().tables).unwrap(),
351+
))
352+
}?;
338353
Ok((
339-
Self { inner, views },
354+
Self {
355+
inner,
356+
tables,
357+
views,
358+
},
340359
match idmap {
341360
true => Some(output_node_map),
342361
false => None,
@@ -473,6 +492,22 @@ impl TreeSequence {
473492
) -> Result<crate::edge_differences::EdgeDifferencesIterator, TskitError> {
474493
crate::edge_differences::EdgeDifferencesIterator::new_from_treeseq(self, 0)
475494
}
495+
496+
/// Reference to the underlying table collection.
497+
///
498+
/// # Examples
499+
///
500+
/// ```
501+
/// let mut tables = tskit::TableCollection::new(1000.).unwrap();
502+
/// tables.add_node(tskit::NodeFlags::default(),0.0, -1, -1).unwrap();
503+
/// tables.build_index();
504+
/// let tcopy = tables.deepcopy().unwrap();
505+
/// let tree_sequence = tskit::TreeSequence::try_from(tcopy).unwrap();
506+
/// assert_eq!(tables.equals(tree_sequence.tables(), 0), true);
507+
/// ```
508+
pub fn tables(&self) -> &TableCollection {
509+
&self.tables
510+
}
476511
}
477512

478513
impl TryFrom<TableCollection> for TreeSequence {

0 commit comments

Comments
 (0)