use crate::bindings as ll_bindings;
use crate::metadata;
use crate::sys;
use crate::Position;
use crate::{tsk_id_t, TskitError};
use crate::{EdgeId, NodeId};
#[derive(Debug)]
pub struct EdgeTableRow {
pub id: EdgeId,
pub left: Position,
pub right: Position,
pub parent: NodeId,
pub child: NodeId,
pub metadata: Option<Vec<u8>>,
}
impl PartialEq for EdgeTableRow {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
&& self.parent == other.parent
&& self.child == other.child
&& crate::util::partial_cmp_equal(&self.left, &other.left)
&& crate::util::partial_cmp_equal(&self.right, &other.right)
&& self.metadata == other.metadata
}
}
fn make_edge_table_row(table: &EdgeTable, pos: tsk_id_t) -> Option<EdgeTableRow> {
Some(EdgeTableRow {
id: pos.into(),
left: table.left(pos)?,
right: table.right(pos)?,
parent: table.parent(pos)?,
child: table.child(pos)?,
metadata: table.raw_metadata(pos).map(|m| m.to_vec()),
})
}
pub(crate) type EdgeTableRefIterator<'a> = crate::table_iterator::TableIterator<&'a EdgeTable>;
pub(crate) type EdgeTableIterator = crate::table_iterator::TableIterator<EdgeTable>;
impl<'a> Iterator for EdgeTableRefIterator<'a> {
type Item = EdgeTableRow;
fn next(&mut self) -> Option<Self::Item> {
let rv = make_edge_table_row(self.table, self.pos);
self.pos += 1;
rv
}
}
impl Iterator for EdgeTableIterator {
type Item = EdgeTableRow;
fn next(&mut self) -> Option<Self::Item> {
let rv = make_edge_table_row(&self.table, self.pos);
self.pos += 1;
rv
}
}
#[derive(Debug)]
pub struct EdgeTableRowView<'a> {
table: &'a EdgeTable,
pub id: EdgeId,
pub left: Position,
pub right: Position,
pub parent: NodeId,
pub child: NodeId,
pub metadata: Option<&'a [u8]>,
}
impl<'a> EdgeTableRowView<'a> {
fn new(table: &'a EdgeTable) -> Self {
Self {
table,
id: (-1).into(),
left: f64::NAN.into(),
right: f64::NAN.into(),
parent: NodeId::NULL,
child: NodeId::NULL,
metadata: None,
}
}
}
impl<'a> PartialEq for EdgeTableRowView<'a> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
&& self.parent == other.parent
&& self.child == other.child
&& crate::util::partial_cmp_equal(&self.left, &other.left)
&& crate::util::partial_cmp_equal(&self.right, &other.right)
&& self.metadata == other.metadata
}
}
impl<'a> Eq for EdgeTableRowView<'a> {}
impl<'a> PartialEq<EdgeTableRow> for EdgeTableRowView<'a> {
fn eq(&self, other: &EdgeTableRow) -> bool {
self.id == other.id
&& self.parent == other.parent
&& self.child == other.child
&& crate::util::partial_cmp_equal(&self.left, &other.left)
&& crate::util::partial_cmp_equal(&self.right, &other.right)
&& optional_container_comparison!(self.metadata, other.metadata)
}
}
impl PartialEq<EdgeTableRowView<'_>> for EdgeTableRow {
fn eq(&self, other: &EdgeTableRowView) -> bool {
self.id == other.id
&& self.parent == other.parent
&& self.child == other.child
&& crate::util::partial_cmp_equal(&self.left, &other.left)
&& crate::util::partial_cmp_equal(&self.right, &other.right)
&& optional_container_comparison!(self.metadata, other.metadata)
}
}
impl<'a> streaming_iterator::StreamingIterator for EdgeTableRowView<'a> {
type Item = Self;
row_lending_iterator_get!();
fn advance(&mut self) {
self.id = (i32::from(self.id) + 1).into();
self.left = self.table.left(self.id).unwrap_or_else(|| f64::NAN.into());
self.right = self.table.right(self.id).unwrap_or_else(|| f64::NAN.into());
self.parent = self.table.parent(self.id).unwrap_or(NodeId::NULL);
self.child = self.table.child(self.id).unwrap_or(NodeId::NULL);
self.metadata = self.table.raw_metadata(self.id);
}
}
#[repr(transparent)]
#[derive(Debug)]
pub struct EdgeTable {
table_: sys::LLEdgeTableRef,
}
impl EdgeTable {
pub(crate) fn new_from_table(
edges: *mut ll_bindings::tsk_edge_table_t,
) -> Result<Self, TskitError> {
let table_ = sys::LLEdgeTableRef::new_from_table(edges)?;
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()
}
raw_metadata_getter_for_tables!(EdgeId);
pub fn parent<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<NodeId> {
sys::tsk_column_access::<NodeId, _, _, _>(row.into(), self.as_ref().parent, self.num_rows())
}
pub fn child<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<NodeId> {
sys::tsk_column_access::<NodeId, _, _, _>(row.into(), self.as_ref().child, self.num_rows())
}
pub fn left<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<Position> {
sys::tsk_column_access::<Position, _, _, _>(row.into(), self.as_ref().left, self.num_rows())
}
pub fn right<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<Position> {
sys::tsk_column_access::<Position, _, _, _>(
row.into(),
self.as_ref().right,
self.num_rows(),
)
}
pub fn metadata<T: metadata::EdgeMetadata>(
&self,
row: EdgeId,
) -> Option<Result<T, TskitError>> {
let buffer = self.raw_metadata(row)?;
Some(decode_metadata_row!(T, buffer).map_err(|e| e.into()))
}
pub fn iter(&self) -> impl Iterator<Item = EdgeTableRow> + '_ {
crate::table_iterator::make_table_iterator::<&EdgeTable>(self)
}
pub fn lending_iter(&self) -> EdgeTableRowView {
EdgeTableRowView::new(self)
}
pub fn row<E: Into<EdgeId> + Copy>(&self, r: E) -> Option<EdgeTableRow> {
table_row_access!(r.into().into(), self, make_edge_table_row)
}
pub fn row_view<E: Into<EdgeId> + Copy>(&self, r: E) -> Option<EdgeTableRowView> {
let view = EdgeTableRowView {
table: self,
id: r.into(),
left: self.left(r)?,
right: self.right(r)?,
parent: self.parent(r)?,
child: self.child(r)?,
metadata: self.raw_metadata(r.into()),
};
Some(view)
}
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);
}
build_owned_table_type!(
=> OwningEdgeTable,
EdgeTable,
crate::sys::LLOwningEdgeTable,
crate::bindings::tsk_edge_table_t
);
impl OwningEdgeTable {
edge_table_add_row!(=> add_row, self, self.as_mut_ptr());
edge_table_add_row_with_metadata!(=> add_row_with_metadata, self, self.as_mut_ptr());
}