Skip to content

feat: add row_view() for tables #402

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 9, 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/edge_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,29 @@ impl EdgeTable {
pub fn row<E: Into<EdgeId> + Copy>(&self, r: E) -> Option<EdgeTableRow> {
table_row_access!(r.into().0, self, make_edge_table_row)
}

/// Return a view of row `r` of the table.
///
/// # Parameters
///
/// * `r`: the row id.
///
/// # Returns
///
/// * `Some(row_view)` if `r` is valid
/// * `None` otherwise
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_owned_table_type!(
Expand Down
22 changes: 22 additions & 0 deletions src/individual_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,28 @@ match tables.individuals().metadata::<MutationMetadata>(0.into())
let ri = r.into().0;
table_row_access!(ri, self, make_individual_table_row)
}

/// Return a view of `r` of the table.
///
/// # Parameters
///
/// * `r`: the row id.
///
/// # Returns
///
/// * `Some(row view)` if `r` is valid
/// * `None` otherwise
pub fn row_view<I: Into<IndividualId> + Copy>(&self, r: I) -> Option<IndividualTableRowView> {
let view = IndividualTableRowView {
table: self,
id: r.into(),
flags: self.flags(r)?,
location: self.location(r),
parents: self.parents(r),
metadata: self.raw_metadata(r.into()),
};
Some(view)
}
}

build_owned_table_type!(
Expand Down
25 changes: 25 additions & 0 deletions src/migration_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,31 @@ impl MigrationTable {
let ri = r.into().0;
table_row_access!(ri, self, make_migration_table_row)
}

/// Return a view of `r` of the table.
///
/// # Parameters
///
/// * `r`: the row id.
///
/// # Returns
///
/// * `Some(row view)` if `r` is valid
/// * `None` otherwise
pub fn row_view<M: Into<MigrationId> + Copy>(&self, r: M) -> Option<MigrationTableRowView> {
let view = MigrationTableRowView {
table: self,
id: r.into(),
left: self.left(r)?,
right: self.right(r)?,
node: self.node(r)?,
source: self.source(r)?,
dest: self.dest(r)?,
time: self.time(r)?,
metadata: self.raw_metadata(r.into()),
};
Some(view)
}
}

build_owned_table_type!(
Expand Down
24 changes: 24 additions & 0 deletions src/mutation_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,30 @@ impl MutationTable {
let ri = r.into().0;
table_row_access!(ri, self, make_mutation_table_row)
}

/// Return a view of row `r` of the table.
///
/// # Parameters
///
/// * `r`: the row id.
///
/// # Returns
///
/// * `Some(row view)` if `r` is valid
/// * `None` otherwise
pub fn row_view<M: Into<MutationId> + Copy>(&self, r: M) -> Option<MutationTableRowView> {
let view = MutationTableRowView {
table: self,
id: r.into(),
site: self.site(r)?,
node: self.node(r)?,
parent: self.parent(r)?,
time: self.time(r)?,
derived_state: self.derived_state(r),
metadata: self.raw_metadata(r.into()),
};
Some(view)
}
}

build_owned_table_type!(
Expand Down
22 changes: 22 additions & 0 deletions src/node_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,28 @@ impl NodeTable {
table_row_access!(ri, self, make_node_table_row)
}

/// Return a view of row `r` of the table.
///
/// # Parameters
///
/// * `r`: the row id.
///
/// # Returns
///
/// * `Some(row view)` if `r` is valid
/// * `None` otherwise
pub fn row_view<N: Into<NodeId> + Copy>(&self, r: N) -> Option<NodeTableRowView> {
let view = NodeTableRowView {
table: self,
id: r.into(),
time: self.time(r)?,
flags: self.flags(r)?,
population: self.population(r)?,
individual: self.individual(r)?,
metadata: self.raw_metadata(r.into()),
};
Some(view)
}
/// Obtain a vector containing the indexes ("ids")
/// of all nodes for which [`crate::TSK_NODE_IS_SAMPLE`]
/// is `true`.
Expand Down
24 changes: 24 additions & 0 deletions src/population_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,30 @@ impl PopulationTable {
let ri = r.into().0;
table_row_access!(ri, self, make_population_table_row)
}

/// Return a view of row `r` of the table.
///
/// # Parameters
///
/// * `r`: the row id.
///
/// # Returns
///
/// * `Some(row view)` if `r` is valid
/// * `None` otherwise
pub fn row_view<P: Into<PopulationId> + Copy>(&self, r: P) -> Option<PopulationTableRowView> {
match SizeType::try_from(r.into().0).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!(
Expand Down
47 changes: 47 additions & 0 deletions src/provenance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ impl<'a> streaming_iterator::StreamingIterator for ProvenanceTableRowView<'a> {

row_lending_iterator_get!();

// FIXME: bad duplication
fn advance(&mut self) {
self.id = (i32::from(self.id) + 1).into();
let record_slice = unsafe_tsk_ragged_char_column_access_to_slice_u8!(
Expand Down Expand Up @@ -262,6 +263,52 @@ impl ProvenanceTable {
make_provenance_row(self, row.into().0)
}

/// Obtain a [`ProvenanceTableRowView`] for row `row`.
///
/// # Returns
///
/// * `Some(row view)` if `r` is valid
/// * `None` otherwise
pub fn row_view<P: Into<ProvenanceId> + Copy>(&self, row: P) -> Option<ProvenanceTableRowView> {
match u64::try_from(row.into().0).ok() {
// FIXME: bad duplication
Some(x) if x < self.num_rows() => {
let record_slice = unsafe_tsk_ragged_char_column_access_to_slice_u8!(
row.into().0,
0,
self.num_rows(),
self.as_ref(),
record,
record_offset,
record_length
);
let timestamp_slice = unsafe_tsk_ragged_char_column_access_to_slice_u8!(
row.into().0,
0,
self.num_rows(),
self.as_ref(),
timestamp,
timestamp_offset,
timestamp_length
);
let view = ProvenanceTableRowView {
table: self,
id: row.into(),
record: match record_slice {
Some(r) => std::str::from_utf8(r).unwrap(),
None => "",
},
timestamp: match timestamp_slice {
Some(t) => std::str::from_utf8(t).unwrap(),
None => "",
},
};
Some(view)
}
_ => None,
}
}

/// Return an iterator over rows of the table.
/// The value of the iterator is [`ProvenanceTableRow`].
pub fn iter(&self) -> impl Iterator<Item = ProvenanceTableRow> + '_ {
Expand Down
21 changes: 21 additions & 0 deletions src/site_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,27 @@ impl SiteTable {
let ri = r.into().0;
table_row_access!(ri, self, make_site_table_row)
}

/// Return a view of row `r` of the table.
///
/// # Parameters
///
/// * `r`: the row id.
///
/// # Returns
///
/// * `Some(row view)` if `r` is valid
/// * `None` otherwise
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_owned_table_type!(
Expand Down
13 changes: 12 additions & 1 deletion tests/test_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ fn test_empty_table_collection() {
macro_rules! validate_empty_tables {
($tables: ident, $table: ident, $table_iter: ident, $row: expr) => {
assert!($tables.$table().row($row).is_none());
assert!($tables.$table().row_view($row).is_none());
assert_eq!($tables.$table().num_rows(), 0);
assert_eq!($tables.$table().iter().count(), 0);
assert_eq!($tables.$table_iter().count(), 0);
Expand Down Expand Up @@ -43,6 +44,10 @@ mod test_adding_rows_without_metadata {
// are held in an Option.
match tables.$table().row(id) {
Some(row) => {
match tables.$table().row_view(id) {
Some(view) => assert_eq!(view, row),
None => panic!("if there is a row, there must be a row view")
}
assert!(row.metadata.is_none());

// A row equals itself
Expand Down Expand Up @@ -273,7 +278,13 @@ mod test_metadata_round_trips {
assert_eq!($tables.$table().num_rows(), 1);

match $tables.$table().row(id) {
Some(row) => assert!(row.metadata.is_some()),
Some(row) => {
assert!(row.metadata.is_some());
match $tables.$table().row_view(id) {
Some(view) => assert_eq!(row, view),
None => panic!("if there is a row, there must be a view!"),
}
}
None => panic!("Expected Some(row) from {} table", stringify!(table)),
}

Expand Down