Skip to content

refactor: TableCollection::simplify returns Option<&[NodeId]> #316

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

Merged
merged 1 commit into from
Aug 14, 2022
Merged
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
23 changes: 16 additions & 7 deletions src/table_collection.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::vec;

use crate::bindings as ll_bindings;
use crate::error::TskitError;
use crate::types::Bookmark;
Expand Down Expand Up @@ -60,6 +62,7 @@ use mbox::MBox;
///
pub struct TableCollection {
inner: MBox<ll_bindings::tsk_table_collection_t>,
idmap: Vec<NodeId>,
}

impl TskitTypeAccess<ll_bindings::tsk_table_collection_t> for TableCollection {
Expand Down Expand Up @@ -119,7 +122,10 @@ impl TableCollection {
if rv < 0 {
return Err(crate::error::TskitError::ErrorCode { code: rv });
}
let mut tables = Self { inner: mbox };
let mut tables = Self {
inner: mbox,
idmap: vec![],
};
unsafe {
(*tables.as_mut_ptr()).sequence_length = sequence_length.0;
}
Expand All @@ -134,7 +140,10 @@ impl TableCollection {
/// requiring an uninitialized table collection.
/// Consult the C API docs before using!
pub(crate) unsafe fn new_from_mbox(mbox: MBox<ll_bindings::tsk_table_collection_t>) -> Self {
Self { inner: mbox }
Self {
inner: mbox,
idmap: vec![],
}
}

pub(crate) fn into_raw(self) -> Result<*mut ll_bindings::tsk_table_collection_t, TskitError> {
Expand Down Expand Up @@ -762,10 +771,10 @@ impl TableCollection {
samples: &[N],
options: O,
idmap: bool,
) -> Result<Option<Vec<NodeId>>, TskitError> {
let mut output_node_map: Vec<NodeId> = vec![];
) -> Result<Option<&[NodeId]>, TskitError> {
if idmap {
output_node_map.resize(usize::try_from(self.nodes().num_rows())?, NodeId::NULL);
self.idmap
.resize(usize::try_from(self.nodes().num_rows())?, NodeId::NULL);
}
let rv = unsafe {
ll_bindings::tsk_table_collection_simplify(
Expand All @@ -774,15 +783,15 @@ impl TableCollection {
samples.len() as tsk_size_t,
options.into().bits(),
match idmap {
true => output_node_map.as_mut_ptr().cast::<tsk_id_t>(),
true => self.idmap.as_mut_ptr().cast::<tsk_id_t>(),
false => std::ptr::null_mut(),
},
)
};
handle_tsk_return_value!(
rv,
match idmap {
true => Some(output_node_map),
true => Some(&self.idmap),
false => None,
}
)
Expand Down