Skip to content

Make tree arrays public. Closes #70 #74

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
Apr 17, 2021
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
18 changes: 15 additions & 3 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ pub trait TskitTypeAccess<T> {
/// Indexable, iterable wrapper around C
/// arrays.
#[derive(Copy, Clone)]
pub(crate) struct WrappedTskArray<T> {
pub struct WrappedTskArray<T> {
array: *const T,
len_: crate::tsk_size_t,
}

pub(crate) struct WrappedTskArrayIter<'a, T: Copy + 'a> {
pub struct WrappedTskArrayIter<'a, T: Copy + 'a> {
inner: &'a WrappedTskArray<T>,
pos: crate::tsk_size_t,
}
Expand All @@ -36,14 +36,26 @@ impl<'a, T: Copy> Iterator for WrappedTskArrayIter<'a, T> {
}

impl<T: Copy> WrappedTskArray<T> {
pub fn new(array: *const T, len: crate::tsk_size_t) -> Self {
pub(crate) fn new(array: *const T, len: crate::tsk_size_t) -> Self {
Self { array, len_: len }
}

pub fn len(&self) -> crate::tsk_size_t {
self.len_
}

pub fn is_empty(&self) -> bool {
self.len_ == 0
}

/// # Safety
///
/// This function returns the raw C pointer,
/// and is thus unsafe.
pub unsafe fn as_ptr(&self) -> *const T {
self.array
}

pub fn iter(&self) -> WrappedTskArrayIter<T> {
WrappedTskArrayIter {
inner: self,
Expand Down
22 changes: 9 additions & 13 deletions src/trees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ impl Tree {
}
}

fn parent_array(&self) -> crate::ffi::TskIdArray {
pub fn parent_array(&self) -> crate::ffi::TskIdArray {
crate::ffi::TskIdArray::new(self.inner.parent, self.inner.num_nodes)
}

fn samples_array(&self) -> Result<crate::ffi::TskIdArray, TskitError> {
pub fn samples_array(&self) -> Result<crate::ffi::TskIdArray, TskitError> {
let num_samples =
unsafe { ll_bindings::tsk_treeseq_get_num_samples((*self.as_ptr()).tree_sequence) };
err_if_not_tracking_samples!(
Expand All @@ -109,44 +109,40 @@ impl Tree {
)
}

fn next_sample_array(&self) -> Result<crate::ffi::TskIdArray, TskitError> {
pub fn next_sample_array(&self) -> Result<crate::ffi::TskIdArray, TskitError> {
err_if_not_tracking_samples!(
self.flags,
crate::ffi::TskIdArray::new(self.inner.next_sample, self.inner.num_nodes)
)
}

#[allow(dead_code)]
fn left_sample_array(&self) -> Result<crate::ffi::TskIdArray, TskitError> {
pub fn left_sample_array(&self) -> Result<crate::ffi::TskIdArray, TskitError> {
err_if_not_tracking_samples!(
self.flags,
crate::ffi::TskIdArray::new(self.inner.left_sample, self.inner.num_nodes)
)
}

#[allow(dead_code)]
fn right_sample_array(&self) -> Result<crate::ffi::TskIdArray, TskitError> {
pub fn right_sample_array(&self) -> Result<crate::ffi::TskIdArray, TskitError> {
err_if_not_tracking_samples!(
self.flags,
crate::ffi::TskIdArray::new(self.inner.right_sample, self.inner.num_nodes)
)
}

#[allow(dead_code)]
fn left_sib_array(&self) -> crate::ffi::TskIdArray {
pub fn left_sib_array(&self) -> crate::ffi::TskIdArray {
crate::ffi::TskIdArray::new(self.inner.left_sib, self.inner.num_nodes)
}

fn right_sib_array(&self) -> crate::ffi::TskIdArray {
pub fn right_sib_array(&self) -> crate::ffi::TskIdArray {
crate::ffi::TskIdArray::new(self.inner.right_sib, self.inner.num_nodes)
}

fn left_child_array(&self) -> crate::ffi::TskIdArray {
pub fn left_child_array(&self) -> crate::ffi::TskIdArray {
crate::ffi::TskIdArray::new(self.inner.left_child, self.inner.num_nodes)
}

#[allow(dead_code)]
fn right_child_array(&self) -> crate::ffi::TskIdArray {
pub fn right_child_array(&self) -> crate::ffi::TskIdArray {
crate::ffi::TskIdArray::new(self.inner.right_child, self.inner.num_nodes)
}

Expand Down