Skip to content

feat: Implement row equality traits #400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,29 @@ macro_rules! row_lending_iterator_get {
};
}

macro_rules! optional_container_comparison {
($lhs: expr, $rhs: expr) => {
if let Some(value) = &$lhs {
if let Some(ovalue) = &$rhs {
if value.len() != ovalue.len() {
return false;
}
if value.iter().zip(ovalue.iter()).any(|(a, b)| a != b) {
false
} else {
true
}
} else {
false
}
} else if $rhs.is_some() {
false
} else {
true
}
};
}

#[cfg(test)]
mod test {
use crate::error::TskitError;
Expand Down
37 changes: 37 additions & 0 deletions src/edge_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl Iterator for EdgeTableIterator {
}

/// Row of an [`EdgeTable`]
#[derive(Debug)]
pub struct EdgeTableRowView<'a> {
table: &'a EdgeTable,
pub id: EdgeId,
Expand All @@ -89,6 +90,41 @@ impl<'a> EdgeTableRowView<'a> {
}
}

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;

Expand All @@ -110,6 +146,7 @@ impl<'a> streaming_iterator::StreamingIterator for EdgeTableRowView<'a> {
/// by types implementing [`std::ops::Deref`] to
/// [`crate::table_views::TableViews`]
#[repr(transparent)]
#[derive(Debug)]
pub struct EdgeTable {
pub(crate) table_: NonNull<ll_bindings::tsk_edge_table_t>,
}
Expand Down
53 changes: 35 additions & 18 deletions src/individual_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,11 @@ impl PartialEq for IndividualTableRow {
&& self.flags == other.flags
&& self.parents == other.parents
&& self.metadata == other.metadata
&& match &self.location {
None => other.location.is_none(),
Some(a) => match &other.location {
None => false,
Some(b) => {
if a.len() != b.len() {
false
} else {
for (i, j) in a.iter().enumerate() {
if !crate::util::partial_cmp_equal(&b[i], j) {
return false;
}
}
true
}
}
},
}
&& self.location == other.location
}
}

#[derive(Debug)]
pub struct IndividualTableRowView<'a> {
table: &'a IndividualTable,
pub id: IndividualId,
Expand All @@ -67,6 +51,38 @@ impl<'a> IndividualTableRowView<'a> {
}
}

impl<'a> PartialEq for IndividualTableRowView<'a> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
&& self.flags == other.flags
&& self.parents == other.parents
&& self.metadata == other.metadata
&& self.location == other.location
}
}

impl<'a> Eq for IndividualTableRowView<'a> {}

impl<'a> PartialEq<IndividualTableRow> for IndividualTableRowView<'a> {
fn eq(&self, other: &IndividualTableRow) -> bool {
self.id == other.id
&& self.flags == other.flags
&& optional_container_comparison!(self.parents, other.parents)
&& optional_container_comparison!(self.metadata, other.metadata)
&& optional_container_comparison!(self.location, other.location)
}
}

impl PartialEq<IndividualTableRowView<'_>> for IndividualTableRow {
fn eq(&self, other: &IndividualTableRowView) -> bool {
self.id == other.id
&& self.flags == other.flags
&& optional_container_comparison!(self.parents, other.parents)
&& optional_container_comparison!(self.metadata, other.metadata)
&& optional_container_comparison!(self.location, other.location)
}
}

impl<'a> streaming_iterator::StreamingIterator for IndividualTableRowView<'a> {
type Item = Self;

Expand All @@ -86,6 +102,7 @@ impl<'a> streaming_iterator::StreamingIterator for IndividualTableRowView<'a> {
/// These are not created directly but are accessed
/// by types implementing [`std::ops::Deref`] to
/// [`crate::table_views::TableViews`]
#[derive(Debug)]
pub struct IndividualTable {
table_: NonNull<ll_bindings::tsk_individual_table_t>,
}
Expand Down
43 changes: 43 additions & 0 deletions src/migration_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl Iterator for MigrationTableIterator {
}
}

#[derive(Debug)]
pub struct MigrationTableRowView<'a> {
table: &'a MigrationTable,
pub id: MigrationId,
Expand Down Expand Up @@ -101,6 +102,47 @@ impl<'a> MigrationTableRowView<'a> {
}
}

impl<'a> PartialEq for MigrationTableRowView<'a> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
&& self.node == other.node
&& self.source == other.source
&& self.dest == other.dest
&& crate::util::partial_cmp_equal(&self.left, &other.left)
&& crate::util::partial_cmp_equal(&self.right, &other.right)
&& crate::util::partial_cmp_equal(&self.time, &other.time)
&& self.metadata == other.metadata
}
}

impl<'a> Eq for MigrationTableRowView<'a> {}

impl<'a> PartialEq<MigrationTableRow> for MigrationTableRowView<'a> {
fn eq(&self, other: &MigrationTableRow) -> bool {
self.id == other.id
&& self.node == other.node
&& self.source == other.source
&& self.dest == other.dest
&& crate::util::partial_cmp_equal(&self.left, &other.left)
&& crate::util::partial_cmp_equal(&self.right, &other.right)
&& crate::util::partial_cmp_equal(&self.time, &other.time)
&& optional_container_comparison!(self.metadata, other.metadata)
}
}

impl PartialEq<MigrationTableRowView<'_>> for MigrationTableRow {
fn eq(&self, other: &MigrationTableRowView) -> bool {
self.id == other.id
&& self.node == other.node
&& self.source == other.source
&& self.dest == other.dest
&& crate::util::partial_cmp_equal(&self.left, &other.left)
&& crate::util::partial_cmp_equal(&self.right, &other.right)
&& crate::util::partial_cmp_equal(&self.time, &other.time)
&& optional_container_comparison!(self.metadata, other.metadata)
}
}

impl<'a> streaming_iterator::StreamingIterator for MigrationTableRowView<'a> {
type Item = Self;

Expand All @@ -123,6 +165,7 @@ impl<'a> streaming_iterator::StreamingIterator for MigrationTableRowView<'a> {
/// These are not created directly but are accessed
/// by types implementing [`std::ops::Deref`] to
/// [`crate::table_views::TableViews`]
#[derive(Debug)]
pub struct MigrationTable {
table_: NonNull<ll_bindings::tsk_migration_table_t>,
}
Expand Down
40 changes: 40 additions & 0 deletions src/mutation_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ impl Iterator for MutationTableIterator {
}
}

#[derive(Debug)]
pub struct MutationTableRowView<'a> {
table: &'a MutationTable,
pub id: MutationId,
Expand All @@ -102,6 +103,44 @@ impl<'a> MutationTableRowView<'a> {
}
}

impl<'a> PartialEq for MutationTableRowView<'a> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
&& self.site == other.site
&& self.node == other.node
&& self.parent == other.parent
&& crate::util::partial_cmp_equal(&self.time, &other.time)
&& self.derived_state == other.derived_state
&& self.metadata == other.metadata
}
}

impl<'a> Eq for MutationTableRowView<'a> {}

impl<'a> PartialEq<MutationTableRow> for MutationTableRowView<'a> {
fn eq(&self, other: &MutationTableRow) -> bool {
self.id == other.id
&& self.site == other.site
&& self.node == other.node
&& self.parent == other.parent
&& crate::util::partial_cmp_equal(&self.time, &other.time)
&& optional_container_comparison!(self.derived_state, other.derived_state)
&& optional_container_comparison!(self.metadata, other.metadata)
}
}

impl PartialEq<MutationTableRowView<'_>> for MutationTableRow {
fn eq(&self, other: &MutationTableRowView) -> bool {
self.id == other.id
&& self.site == other.site
&& self.node == other.node
&& self.parent == other.parent
&& crate::util::partial_cmp_equal(&self.time, &other.time)
&& optional_container_comparison!(self.derived_state, other.derived_state)
&& optional_container_comparison!(self.metadata, other.metadata)
}
}

impl<'a> streaming_iterator::StreamingIterator for MutationTableRowView<'a> {
type Item = Self;

Expand All @@ -123,6 +162,7 @@ impl<'a> streaming_iterator::StreamingIterator for MutationTableRowView<'a> {
/// These are not created directly but are accessed
/// by types implementing [`std::ops::Deref`] to
/// [`crate::table_views::TableViews`]
#[derive(Debug)]
pub struct MutationTable {
table_: NonNull<ll_bindings::tsk_mutation_table_t>,
}
Expand Down
37 changes: 37 additions & 0 deletions src/node_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl Iterator for NodeTableIterator {
}
}

#[derive(Debug)]
pub struct NodeTableRowView<'a> {
table: &'a NodeTable,
pub id: NodeId,
Expand All @@ -90,6 +91,41 @@ impl<'a> NodeTableRowView<'a> {
}
}

impl<'a> PartialEq for NodeTableRowView<'a> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
&& self.flags == other.flags
&& self.population == other.population
&& self.individual == other.individual
&& crate::util::partial_cmp_equal(&self.time, &other.time)
&& self.metadata == other.metadata
}
}

impl<'a> Eq for NodeTableRowView<'a> {}

impl<'a> PartialEq<NodeTableRow> for NodeTableRowView<'a> {
fn eq(&self, other: &NodeTableRow) -> bool {
self.id == other.id
&& self.flags == other.flags
&& self.population == other.population
&& self.individual == other.individual
&& crate::util::partial_cmp_equal(&self.time, &other.time)
&& optional_container_comparison!(self.metadata, other.metadata)
}
}

impl PartialEq<NodeTableRowView<'_>> for NodeTableRow {
fn eq(&self, other: &NodeTableRowView) -> bool {
self.id == other.id
&& self.flags == other.flags
&& self.population == other.population
&& self.individual == other.individual
&& crate::util::partial_cmp_equal(&self.time, &other.time)
&& optional_container_comparison!(self.metadata, other.metadata)
}
}

impl<'a> streaming_iterator::StreamingIterator for NodeTableRowView<'a> {
type Item = Self;

Expand All @@ -110,6 +146,7 @@ impl<'a> streaming_iterator::StreamingIterator for NodeTableRowView<'a> {
/// These are not created directly but are accessed
/// by types implementing [`std::ops::Deref`] to
/// [`crate::table_views::TableViews`]
#[derive(Debug)]
pub struct NodeTable {
table_: NonNull<ll_bindings::tsk_node_table_t>,
}
Expand Down
22 changes: 22 additions & 0 deletions src/population_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl Iterator for PopulationTableIterator {
}
}

#[derive(Debug)]
pub struct PopulationTableRowView<'a> {
table: &'a PopulationTable,
pub id: PopulationId,
Expand All @@ -77,6 +78,26 @@ impl<'a> PopulationTableRowView<'a> {
}
}

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;

Expand All @@ -94,6 +115,7 @@ impl<'a> streaming_iterator::StreamingIterator for PopulationTableRowView<'a> {
/// by types implementing [`std::ops::Deref`] to
/// [`crate::table_views::TableViews`]
#[repr(transparent)]
#[derive(Debug)]
pub struct PopulationTable {
table_: NonNull<ll_bindings::tsk_population_table_t>,
}
Expand Down
Loading