use crate::bindings as ll_bindings;
use crate::metadata;
use crate::sys;
use crate::tsk_id_t;
use crate::PopulationId;
use crate::SizeType;
use crate::TskitError;
#[derive(Eq, Debug)]
pub struct PopulationTableRow {
pub id: PopulationId,
pub metadata: Option<Vec<u8>>,
}
impl PartialEq for PopulationTableRow {
fn eq(&self, other: &Self) -> bool {
self.id == other.id && self.metadata == other.metadata
}
}
fn make_population_table_row(table: &PopulationTable, pos: tsk_id_t) -> Option<PopulationTableRow> {
let index = ll_bindings::tsk_size_t::try_from(pos).ok()?;
match index {
i if i < table.num_rows() => {
let metadata = table.raw_metadata(pos).map(|m| m.to_vec());
Some(PopulationTableRow {
id: pos.into(),
metadata,
})
}
_ => None,
}
}
pub(crate) type PopulationTableRefIterator<'a> =
crate::table_iterator::TableIterator<&'a PopulationTable>;
pub(crate) type PopulationTableIterator = crate::table_iterator::TableIterator<PopulationTable>;
impl<'a> Iterator for PopulationTableRefIterator<'a> {
type Item = PopulationTableRow;
fn next(&mut self) -> Option<Self::Item> {
let rv = make_population_table_row(self.table, self.pos);
self.pos += 1;
rv
}
}
impl Iterator for PopulationTableIterator {
type Item = PopulationTableRow;
fn next(&mut self) -> Option<Self::Item> {
let rv = make_population_table_row(&self.table, self.pos);
self.pos += 1;
rv
}
}
#[derive(Debug)]
pub struct PopulationTableRowView<'a> {
table: &'a PopulationTable,
pub id: PopulationId,
pub metadata: Option<&'a [u8]>,
}
impl<'a> PopulationTableRowView<'a> {
fn new(table: &'a PopulationTable) -> Self {
Self {
table,
id: PopulationId::NULL,
metadata: None,
}
}
}
impl<'a> PartialEq for PopulationTableRowView<'a> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id && self.metadata == other.metadata
}
}
impl<'a> Eq for PopulationTableRowView<'a> {}
impl<'a> PartialEq<PopulationTableRow> for PopulationTableRowView<'a> {
fn eq(&self, other: &PopulationTableRow) -> bool {
self.id == other.id && optional_container_comparison!(self.metadata, other.metadata)
}
}
impl PartialEq<PopulationTableRowView<'_>> for PopulationTableRow {
fn eq(&self, other: &PopulationTableRowView) -> bool {
self.id == other.id && optional_container_comparison!(self.metadata, other.metadata)
}
}
impl<'a> streaming_iterator::StreamingIterator for PopulationTableRowView<'a> {
type Item = Self;
row_lending_iterator_get!();
fn advance(&mut self) {
self.id = (i32::from(self.id) + 1).into();
self.metadata = self.table.raw_metadata(self.id);
}
}
#[repr(transparent)]
#[derive(Debug)]
pub struct PopulationTable {
table_: sys::LLPopulationTableRef,
}
impl PopulationTable {
pub(crate) fn new_from_table(
populations: *mut ll_bindings::tsk_population_table_t,
) -> Result<Self, TskitError> {
let table_ = sys::LLPopulationTableRef::new_from_table(populations)?;
Ok(PopulationTable { table_ })
}
pub(crate) fn as_ref(&self) -> &ll_bindings::tsk_population_table_t {
self.table_.as_ref()
}
raw_metadata_getter_for_tables!(PopulationId);
pub fn num_rows(&self) -> SizeType {
self.as_ref().num_rows.into()
}
pub fn metadata<T: metadata::PopulationMetadata>(
&self,
row: PopulationId,
) -> Option<Result<T, TskitError>> {
let buffer = self.raw_metadata(row)?;
Some(decode_metadata_row!(T, buffer).map_err(TskitError::from))
}
pub fn iter(&self) -> impl Iterator<Item = PopulationTableRow> + '_ {
crate::table_iterator::make_table_iterator::<&PopulationTable>(self)
}
pub fn lending_iter(&self) -> PopulationTableRowView {
PopulationTableRowView::new(self)
}
pub fn row<P: Into<PopulationId> + Copy>(&self, r: P) -> Option<PopulationTableRow> {
let ri = r.into().into();
table_row_access!(ri, self, make_population_table_row)
}
pub fn row_view<P: Into<PopulationId> + Copy>(&self, r: P) -> Option<PopulationTableRowView> {
match SizeType::try_from(r.into()).ok() {
Some(row) if row < self.num_rows() => {
let view = PopulationTableRowView {
table: self,
id: r.into(),
metadata: self.raw_metadata(r.into()),
};
Some(view)
}
_ => None,
}
}
}
build_owned_table_type!(
=> OwningPopulationTable,
PopulationTable,
crate::sys::LLOwningPopulationTable,
crate::bindings::tsk_population_table_t
);
impl OwningPopulationTable {
population_table_add_row!(=> add_row, self, self.as_mut_ptr());
population_table_add_row_with_metadata!(=> add_row_with_metadata, self, self.as_mut_ptr());
}