use crate::bindings as ll_bindings;
use crate::metadata;
use crate::sys;
use crate::tsk_id_t;
use crate::Position;
use crate::SiteId;
use crate::SizeType;
use crate::TskitError;
#[derive(Debug)]
pub struct SiteTableRow {
pub id: SiteId,
pub position: Position,
pub ancestral_state: Option<Vec<u8>>,
pub metadata: Option<Vec<u8>>,
}
impl PartialEq for SiteTableRow {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
&& crate::util::partial_cmp_equal(&self.position, &other.position)
&& self.ancestral_state == other.ancestral_state
&& self.metadata == other.metadata
}
}
fn make_site_table_row(table: &SiteTable, pos: tsk_id_t) -> Option<SiteTableRow> {
let ancestral_state = table.ancestral_state(pos).map(|s| s.to_vec());
Some(SiteTableRow {
id: pos.into(),
position: table.position(pos)?,
ancestral_state,
metadata: table.raw_metadata(pos).map(|m| m.to_vec()),
})
}
pub(crate) type SiteTableRefIterator<'a> = crate::table_iterator::TableIterator<&'a SiteTable>;
pub(crate) type SiteTableIterator = crate::table_iterator::TableIterator<SiteTable>;
impl<'a> Iterator for SiteTableRefIterator<'a> {
type Item = SiteTableRow;
fn next(&mut self) -> Option<Self::Item> {
let rv = make_site_table_row(self.table, self.pos);
self.pos += 1;
rv
}
}
impl Iterator for SiteTableIterator {
type Item = SiteTableRow;
fn next(&mut self) -> Option<Self::Item> {
let rv = make_site_table_row(&self.table, self.pos);
self.pos += 1;
rv
}
}
#[derive(Debug)]
pub struct SiteTableRowView<'a> {
table: &'a SiteTable,
pub id: SiteId,
pub position: Position,
pub ancestral_state: Option<&'a [u8]>,
pub metadata: Option<&'a [u8]>,
}
impl<'a> SiteTableRowView<'a> {
fn new(table: &'a SiteTable) -> Self {
Self {
table,
id: SiteId::NULL,
position: f64::NAN.into(),
ancestral_state: None,
metadata: None,
}
}
}
impl<'a> PartialEq for SiteTableRowView<'a> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
&& crate::util::partial_cmp_equal(&self.position, &other.position)
&& self.ancestral_state == other.ancestral_state
&& self.metadata == other.metadata
}
}
impl<'a> Eq for SiteTableRowView<'a> {}
impl<'a> PartialEq<SiteTableRow> for SiteTableRowView<'a> {
fn eq(&self, other: &SiteTableRow) -> bool {
self.id == other.id
&& crate::util::partial_cmp_equal(&self.position, &other.position)
&& optional_container_comparison!(self.ancestral_state, other.ancestral_state)
&& optional_container_comparison!(self.metadata, other.metadata)
}
}
impl PartialEq<SiteTableRowView<'_>> for SiteTableRow {
fn eq(&self, other: &SiteTableRowView) -> bool {
self.id == other.id
&& crate::util::partial_cmp_equal(&self.position, &other.position)
&& optional_container_comparison!(self.ancestral_state, other.ancestral_state)
&& optional_container_comparison!(self.metadata, other.metadata)
}
}
impl<'a> streaming_iterator::StreamingIterator for SiteTableRowView<'a> {
type Item = Self;
row_lending_iterator_get!();
fn advance(&mut self) {
self.id = (i32::from(self.id) + 1).into();
self.position = self
.table
.position(self.id)
.unwrap_or_else(|| f64::NAN.into());
self.ancestral_state = self.table.ancestral_state(self.id);
self.metadata = self.table.raw_metadata(self.id);
}
}
#[derive(Debug)]
#[repr(transparent)]
pub struct SiteTable {
table_: sys::LLSiteTableRef,
}
impl SiteTable {
pub(crate) fn new_from_table(
sites: *mut ll_bindings::tsk_site_table_t,
) -> Result<Self, TskitError> {
let table_ = sys::LLSiteTableRef::new_from_table(sites)?;
Ok(SiteTable { table_ })
}
pub(crate) fn as_ref(&self) -> &ll_bindings::tsk_site_table_t {
self.table_.as_ref()
}
raw_metadata_getter_for_tables!(SiteId);
pub fn num_rows(&self) -> SizeType {
self.as_ref().num_rows.into()
}
pub fn position<S: Into<SiteId> + Copy>(&self, row: S) -> Option<Position> {
sys::tsk_column_access::<Position, _, _, _>(
row.into(),
self.as_ref().position,
self.num_rows(),
)
}
pub fn ancestral_state<S: Into<SiteId>>(&self, row: S) -> Option<&[u8]> {
sys::tsk_ragged_column_access(
row.into(),
self.as_ref().ancestral_state,
self.num_rows(),
self.as_ref().ancestral_state_offset,
self.as_ref().ancestral_state_length,
)
}
pub fn metadata<T: metadata::SiteMetadata>(
&self,
row: SiteId,
) -> 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 = SiteTableRow> + '_ {
crate::table_iterator::make_table_iterator::<&SiteTable>(self)
}
pub fn lending_iter(&self) -> SiteTableRowView {
SiteTableRowView::new(self)
}
pub fn row<S: Into<SiteId> + Copy>(&self, r: S) -> Option<SiteTableRow> {
let ri = r.into().into();
table_row_access!(ri, self, make_site_table_row)
}
pub fn row_view<S: Into<SiteId> + Copy>(&self, r: S) -> Option<SiteTableRowView> {
let view = SiteTableRowView {
table: self,
id: r.into(),
position: self.position(r)?,
ancestral_state: self.ancestral_state(r),
metadata: self.raw_metadata(r.into()),
};
Some(view)
}
build_table_column_slice_getter!(
=> position, position_slice, Position);
build_table_column_slice_getter!(
=> position, position_slice_raw, f64);
}
build_owned_table_type!(
=> OwningSiteTable,
SiteTable,
crate::sys::LLOwningSiteTable,
crate::bindings::tsk_site_table_t
);
impl OwningSiteTable {
site_table_add_row!(=> add_row, self, self.as_mut_ptr());
site_table_add_row_with_metadata!(=> add_row_with_metadata, self, self.as_mut_ptr());
}