use crate::metadata;
use crate::metadata::EdgeMetadata;
use crate::sys;
use crate::Position;
use crate::TskitError;
use crate::{EdgeId, NodeId};
use sys::bindings as ll_bindings;
#[repr(transparent)]
#[derive(Debug, Default)]
pub struct EdgeTable {
table_: sys::EdgeTable,
}
impl EdgeTable {
pub fn new() -> Result<Self, TskitError> {
let table_ = sys::EdgeTable::new(0)?;
Ok(Self { table_ })
}
pub(crate) unsafe fn new_from_table(
edges: *mut ll_bindings::tsk_edge_table_t,
) -> Result<Self, TskitError> {
let ptr = std::ptr::NonNull::new(edges).unwrap();
let table_ = unsafe { sys::EdgeTable::new_borrowed(ptr) };
Ok(EdgeTable { table_ })
}
pub(crate) fn as_ref(&self) -> &ll_bindings::tsk_edge_table_t {
self.table_.as_ref()
}
pub fn num_rows(&self) -> crate::SizeType {
self.as_ref().num_rows.into()
}
pub fn parent<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<NodeId> {
self.table_.parent(row.into())
}
pub fn child<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<NodeId> {
self.table_.child(row.into())
}
pub fn left<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<Position> {
self.table_.left(row.into())
}
pub fn right<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<Position> {
self.table_.right(row.into())
}
pub fn metadata<T: metadata::EdgeMetadata>(
&self,
row: impl Into<EdgeId>,
) -> Option<Result<T, TskitError>> {
let buffer = self.table_.raw_metadata(row)?;
Some(decode_metadata_row!(T, buffer).map_err(|e| e.into()))
}
pub fn iter(&self) -> impl Iterator<Item = crate::Edge<'_>> {
self.table_.iter()
}
pub fn row<E: Into<EdgeId> + Copy>(&self, r: E) -> Option<crate::Edge<'_>> {
self.table_.row(r.into())
}
build_table_column_slice_getter!(
=> left, left_slice, Position);
build_table_column_slice_getter!(
=> left, left_slice_raw, f64);
build_table_column_slice_getter!(
=> right, right_slice, Position);
build_table_column_slice_getter!(
=> right, right_slice_raw, f64);
build_table_column_slice_getter!(
=> parent, parent_slice, NodeId);
build_table_column_slice_getter!(
=> parent, parent_slice_raw, ll_bindings::tsk_id_t);
build_table_column_slice_getter!(
=> child, child_slice, NodeId);
build_table_column_slice_getter!(
=> child, child_slice_raw, ll_bindings::tsk_id_t);
pub fn parent_column(&self) -> impl crate::TableColumn<EdgeId, NodeId> + '_ {
crate::table_column::OpaqueTableColumn(self.parent_slice())
}
pub fn child_column(&self) -> impl crate::TableColumn<EdgeId, NodeId> + '_ {
crate::table_column::OpaqueTableColumn(self.child_slice())
}
pub fn left_column(&self) -> impl crate::TableColumn<EdgeId, Position> + '_ {
crate::table_column::OpaqueTableColumn(self.left_slice())
}
pub fn right_column(&self) -> impl crate::TableColumn<EdgeId, Position> + '_ {
crate::table_column::OpaqueTableColumn(self.right_slice())
}
pub fn clear(&mut self) -> Result<i32, TskitError> {
handle_tsk_return_value!(self.table_.clear())
}
pub fn add_row<L: Into<Position>, R: Into<Position>, P: Into<NodeId>, C: Into<NodeId>>(
&mut self,
left: L,
right: R,
parent: P,
child: C,
) -> Result<EdgeId, TskitError> {
let rv = self.table_.add_row(
left.into().into(),
right.into().into(),
parent.into().into(),
child.into().into(),
)?;
handle_tsk_return_value!(rv, rv.into())
}
pub fn add_row_with_metadata<
L: Into<Position>,
R: Into<Position>,
P: Into<NodeId>,
C: Into<NodeId>,
M: EdgeMetadata,
>(
&mut self,
left: L,
right: R,
parent: P,
child: C,
metadata: &M,
) -> Result<EdgeId, TskitError> {
let md = crate::metadata::EncodedMetadata::new(metadata)?;
let rv = self.table_.add_row_with_metadata(
left.into().into(),
right.into().into(),
parent.into().into(),
child.into().into(),
md.as_slice(),
)?;
handle_tsk_return_value!(rv, rv.into())
}
}