use crate::bindings as ll_bindings;
use crate::metadata;
use crate::{tsk_flags_t, tsk_id_t, TskitError};
use crate::{IndividualId, NodeId, PopulationId};
pub struct NodeTableRow {
pub id: NodeId,
pub time: f64,
pub flags: tsk_flags_t,
pub population: PopulationId,
pub individual: IndividualId,
pub metadata: Option<Vec<u8>>,
}
impl PartialEq for NodeTableRow {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
&& self.flags == other.flags
&& self.population == other.population
&& self.individual == other.individual
&& crate::util::f64_partial_cmp_equal(&self.time, &other.time)
&& self.metadata == other.metadata
}
}
fn make_node_table_row(table: &NodeTable, pos: tsk_id_t) -> Option<NodeTableRow> {
if pos < table.num_rows() as tsk_id_t {
Some(NodeTableRow {
id: pos.into(),
time: table.time(pos).unwrap(),
flags: table.flags(pos).unwrap(),
population: table.population(pos).unwrap(),
individual: table.individual(pos).unwrap(),
metadata: table_row_decode_metadata!(table, pos),
})
} else {
None
}
}
pub type NodeTableRefIterator<'a> = crate::table_iterator::TableIterator<&'a NodeTable<'a>>;
pub type NodeTableIterator<'a> = crate::table_iterator::TableIterator<NodeTable<'a>>;
impl<'a> Iterator for NodeTableRefIterator<'a> {
type Item = NodeTableRow;
fn next(&mut self) -> Option<Self::Item> {
let rv = make_node_table_row(self.table, self.pos);
self.pos += 1;
rv
}
}
impl<'a> Iterator for NodeTableIterator<'a> {
type Item = crate::node_table::NodeTableRow;
fn next(&mut self) -> Option<Self::Item> {
let rv = make_node_table_row(&self.table, self.pos);
self.pos += 1;
rv
}
}
pub struct NodeTable<'a> {
table_: &'a ll_bindings::tsk_node_table_t,
}
impl<'a> NodeTable<'a> {
pub(crate) fn new_from_table(nodes: &'a ll_bindings::tsk_node_table_t) -> Self {
NodeTable { table_: nodes }
}
pub fn num_rows(&'a self) -> ll_bindings::tsk_size_t {
self.table_.num_rows
}
pub fn time<N: Into<NodeId> + Copy>(&'a self, row: N) -> Result<f64, TskitError> {
unsafe_tsk_column_access!(row.into().0, 0, self.num_rows(), self.table_.time)
}
pub fn flags<N: Into<NodeId> + Copy>(&'a self, row: N) -> Result<tsk_flags_t, TskitError> {
unsafe_tsk_column_access!(row.into().0, 0, self.num_rows(), self.table_.flags)
}
pub fn flags_array_mut(&mut self) -> &mut [tsk_flags_t] {
unsafe { std::slice::from_raw_parts_mut(self.table_.flags, self.table_.num_rows as usize) }
}
pub fn time_array_mut(&mut self) -> &mut [f64] {
unsafe { std::slice::from_raw_parts_mut(self.table_.time, self.table_.num_rows as usize) }
}
pub fn population<N: Into<NodeId> + Copy>(
&'a self,
row: N,
) -> Result<PopulationId, TskitError> {
unsafe_tsk_column_access!(
row.into().0,
0,
self.num_rows(),
self.table_.population,
PopulationId
)
}
pub fn deme<N: Into<NodeId> + Copy>(&'a self, row: N) -> Result<PopulationId, TskitError> {
self.population(row)
}
pub fn individual<N: Into<NodeId> + Copy>(
&'a self,
row: N,
) -> Result<IndividualId, TskitError> {
unsafe_tsk_column_access!(
row.into().0,
0,
self.num_rows(),
self.table_.individual,
IndividualId
)
}
pub fn metadata<T: metadata::MetadataRoundtrip>(
&'a self,
row: NodeId,
) -> Result<Option<T>, TskitError> {
let buffer = metadata_to_vector!(self, row.0)?;
decode_metadata_row!(T, buffer)
}
pub fn iter(&self) -> NodeTableRefIterator {
crate::table_iterator::make_table_iterator::<&NodeTable<'a>>(self)
}
pub fn row<N: Into<NodeId> + Copy>(&self, r: N) -> Result<NodeTableRow, TskitError> {
table_row_access!(r.into().0, self, make_node_table_row)
}
pub fn samples_as_vector(&self) -> Vec<NodeId> {
let mut samples: Vec<NodeId> = vec![];
for row in self.iter() {
if row.flags & crate::TSK_NODE_IS_SAMPLE > 0 {
samples.push(row.id);
}
}
samples
}
pub fn create_node_id_vector(
&self,
mut f: impl FnMut(&crate::NodeTableRow) -> bool,
) -> Vec<NodeId> {
let mut samples: Vec<NodeId> = vec![];
for row in self.iter() {
if f(&row) {
samples.push(row.id);
}
}
samples
}
}