Skip to content

Unify NULL concept for row id newtypes. #154

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
Aug 20, 2021
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
35 changes: 24 additions & 11 deletions examples/forward_simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,21 @@ fn births(
for p in parents {
// Register the two nodes for our offspring
let node0 = match tables.add_node(
0, // flags
birth_time as f64, // time
tskit::TSK_NULL, // population
0, // flags
birth_time as f64, // time
tskit::PopulationId::NULL, // population
// individual
tskit::TSK_NULL,
tskit::IndividualId::NULL,
) {
Ok(x) => x,
Err(e) => panic!("{}", e),
};
let node1 = match tables.add_node(0, birth_time as f64, tskit::TSK_NULL, tskit::TSK_NULL) {
let node1 = match tables.add_node(
0,
birth_time as f64,
tskit::PopulationId::NULL,
tskit::IndividualId::NULL,
) {
Ok(x) => x,
Err(e) => panic!("{}", e),
};
Expand Down Expand Up @@ -333,9 +338,9 @@ fn simplify(alive: &mut [Diploid], tables: &mut tskit::TableCollection) {
Some(idmap) => {
for a in alive.iter_mut() {
a.node0 = idmap[usize::from(a.node0)];
assert!(a.node0 != tskit::TSK_NULL);
assert!(a.node0 != tskit::NodeId::NULL);
a.node1 = idmap[usize::from(a.node1)];
assert!(a.node1 != tskit::TSK_NULL);
assert!(a.node1 != tskit::NodeId::NULL);
}
}
None => panic!("Unexpected None"),
Expand All @@ -354,13 +359,21 @@ fn runsim(params: &SimParams) -> tskit::TableCollection {

let mut alive: Vec<Diploid> = vec![];
for _ in 0..params.popsize {
let node0 = match tables.add_node(0, params.nsteps as f64, tskit::TSK_NULL, tskit::TSK_NULL)
{
let node0 = match tables.add_node(
0,
params.nsteps as f64,
tskit::PopulationId::NULL,
tskit::IndividualId::NULL,
) {
Ok(x) => x,
Err(e) => panic!("{}", e),
};
let node1 = match tables.add_node(0, params.nsteps as f64, tskit::TSK_NULL, tskit::TSK_NULL)
{
let node1 = match tables.add_node(
0,
params.nsteps as f64,
tskit::PopulationId::NULL,
tskit::IndividualId::NULL,
) {
Ok(x) => x,
Err(e) => panic!("{}", e),
};
Expand Down
2 changes: 1 addition & 1 deletion examples/tree_traversals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tskit::prelude::*;
fn traverse_upwards(tree: &tskit::Tree) {
for &s in tree.sample_nodes() {
let mut u = s;
while u != tskit::TSK_NULL {
while u != tskit::NodeId::NULL {
u = tree.parent(u).unwrap();
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ macro_rules! tree_array_slice {

macro_rules! impl_id_traits {
($idtype: ty) => {
impl $idtype {
/// NULL value for this type.
pub const NULL: $idtype = Self($crate::TSK_NULL);
}

impl From<$crate::tsk_id_t> for $idtype {
fn from(value: $crate::tsk_id_t) -> Self {
Self(value)
Expand All @@ -264,7 +269,7 @@ macro_rules! impl_id_traits {

impl $crate::IdIsNull for $idtype {
fn is_null(&self) -> bool {
self.0 == $crate::TSK_NULL
self.0 == Self::NULL
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,7 @@ impl_id_traits!(EdgeId);
// in a macro. bindgen thus misses it.
// See bindgen issue 316.
/// "Null" identifier value.
pub const TSK_NULL: tsk_id_t = -1;

/// "Null" identifier value for [``NodeId``]
pub const NULL_NODE_ID: NodeId = NodeId(-1);
pub(crate) const TSK_NULL: tsk_id_t = -1;

pub use edge_table::{EdgeTable, EdgeTableRow};
pub use error::TskitError;
Expand Down
2 changes: 1 addition & 1 deletion src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use thiserror::Error;
/// dominance: 0.25};
///
/// // Add table row with metadata.
/// tables.add_mutation_with_metadata(0, 0, tskit::TSK_NULL, 100., None,
/// tables.add_mutation_with_metadata(0, 0, tskit::MutationId::NULL, 100., None,
/// Some(&mutation)).unwrap();
///
/// // Decode the metadata
Expand Down
2 changes: 0 additions & 2 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ pub use crate::tsk_size_t;
pub use crate::NodeListGenerator;
pub use crate::TableAccess;
pub use crate::TskitTypeAccess;
pub use crate::NULL_NODE_ID;
pub use crate::TSK_NODE_IS_SAMPLE;
pub use crate::TSK_NULL;
pub use streaming_iterator::DoubleEndedStreamingIterator;
pub use streaming_iterator::StreamingIterator;
65 changes: 35 additions & 30 deletions src/table_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::TableSortOptions;
use crate::TreeSequenceFlags;
use crate::TskReturnValue;
use crate::TskitTypeAccess;
use crate::{tsk_flags_t, tsk_id_t, tsk_size_t, TSK_NULL};
use crate::{tsk_flags_t, tsk_id_t, tsk_size_t};
use crate::{EdgeId, IndividualId, MigrationId, MutationId, NodeId, PopulationId, SiteId};
use ll_bindings::tsk_table_collection_free;

Expand All @@ -42,7 +42,7 @@ use ll_bindings::tsk_table_collection_free;
///
/// // Add node:
///
/// let rv = tables.add_node(0, 3.2, tskit::TSK_NULL, tskit::TSK_NULL).unwrap();
/// let rv = tables.add_node(0, 3.2, tskit::PopulationId::NULL, tskit::IndividualId::NULL).unwrap();
///
/// // Get immutable reference to edge table
/// let edges = tables.edges();
Expand Down Expand Up @@ -656,7 +656,7 @@ impl TableCollection {
/// the behavior of simplification.
/// * `idmap`: if `true`, the return value contains a vector equal
/// in length to the input node table. For each input node,
/// this vector either contains the node's new index or [`TSK_NULL`]
/// this vector either contains the node's new index or [`NodeId::NULL`]
/// if the input node is not part of the simplified history.
pub fn simplify<N: Into<NodeId>>(
&mut self,
Expand All @@ -666,7 +666,7 @@ impl TableCollection {
) -> Result<Option<Vec<NodeId>>, TskitError> {
let mut output_node_map: Vec<NodeId> = vec![];
if idmap {
output_node_map.resize(self.nodes().num_rows() as usize, TSK_NULL.into());
output_node_map.resize(self.nodes().num_rows() as usize, NodeId::NULL);
}
let rv = unsafe {
ll_bindings::tsk_table_collection_simplify(
Expand Down Expand Up @@ -746,13 +746,18 @@ impl crate::provenance::Provenance for TableCollection {
#[cfg(test)]
mod test {
use super::*;
use crate::TSK_NULL;

fn make_small_table_collection() -> TableCollection {
let mut tables = TableCollection::new(1000.).unwrap();
tables.add_node(0, 1.0, TSK_NULL, TSK_NULL).unwrap();
tables.add_node(0, 0.0, TSK_NULL, TSK_NULL).unwrap();
tables.add_node(0, 0.0, TSK_NULL, TSK_NULL).unwrap();
tables
.add_node(0, 1.0, PopulationId::NULL, IndividualId::NULL)
.unwrap();
tables
.add_node(0, 0.0, PopulationId::NULL, IndividualId::NULL)
.unwrap();
tables
.add_node(0, 0.0, PopulationId::NULL, IndividualId::NULL)
.unwrap();
tables.add_edge(0., 1000., 0, 1).unwrap();
tables.add_edge(0., 1000., 0, 2).unwrap();
tables.build_index().unwrap();
Expand Down Expand Up @@ -997,13 +1002,13 @@ mod test {
let mut tables = TableCollection::new(1000.).unwrap();

tables
.add_mutation(0, 0, crate::TSK_NULL, 1.123, Some(b"pajamas"))
.add_mutation(0, 0, MutationId::NULL, 1.123, Some(b"pajamas"))
.unwrap();
tables
.add_mutation(1, 1, crate::TSK_NULL, 2.123, None)
.add_mutation(1, 1, MutationId::NULL, 2.123, None)
.unwrap();
tables
.add_mutation(2, 2, crate::TSK_NULL, 3.123, Some(b"more pajamas"))
.add_mutation(2, 2, MutationId::NULL, 3.123, Some(b"more pajamas"))
.unwrap();
let mutations = tables.mutations();
assert!(close_enough(mutations.time(0).unwrap(), 1.123));
Expand All @@ -1012,9 +1017,9 @@ mod test {
assert_eq!(mutations.node(0).unwrap(), 0);
assert_eq!(mutations.node(1).unwrap(), 1);
assert_eq!(mutations.node(2).unwrap(), 2);
assert_eq!(mutations.parent(0).unwrap(), crate::TSK_NULL);
assert_eq!(mutations.parent(1).unwrap(), crate::TSK_NULL);
assert_eq!(mutations.parent(2).unwrap(), crate::TSK_NULL);
assert_eq!(mutations.parent(0).unwrap(), MutationId::NULL);
assert_eq!(mutations.parent(1).unwrap(), MutationId::NULL);
assert_eq!(mutations.parent(2).unwrap(), MutationId::NULL);
assert_eq!(mutations.derived_state(0).unwrap().unwrap(), b"pajamas");

if mutations.derived_state(1).unwrap().is_some() {
Expand Down Expand Up @@ -1099,7 +1104,7 @@ mod test {
.add_mutation_with_metadata(
0,
0,
crate::TSK_NULL,
MutationId::NULL,
1.123,
None,
Some(&F { x: -3, y: 666 }),
Expand All @@ -1126,15 +1131,15 @@ mod test {
.add_mutation_with_metadata(
0,
0,
crate::TSK_NULL,
MutationId::NULL,
1.123,
None,
Some(&F { x: -3, y: 666 }),
)
.unwrap();

tables
.add_mutation_with_metadata(1, 2, crate::TSK_NULL, 2.0, None, None)
.add_mutation_with_metadata(1, 2, MutationId::NULL, 2.0, None, None)
.unwrap();

let mut num_with_metadata = 0;
Expand Down Expand Up @@ -1167,7 +1172,7 @@ mod test {
assert_eq!(tables.populations().num_rows(), 1);

tables
.add_node(crate::TSK_NODE_IS_SAMPLE, 0.0, pop_id, crate::TSK_NULL)
.add_node(crate::TSK_NODE_IS_SAMPLE, 0.0, pop_id, IndividualId::NULL)
.unwrap();

match tables.nodes().row(NodeId::from(0)) {
Expand All @@ -1185,10 +1190,10 @@ mod test {
let mut tables = TableCollection::new(1000.).unwrap();
let pop_id = tables.add_population().unwrap();
tables
.add_node(crate::TSK_NODE_IS_SAMPLE, 0.0, pop_id, crate::TSK_NULL)
.add_node(crate::TSK_NODE_IS_SAMPLE, 0.0, pop_id, IndividualId::NULL)
.unwrap();
tables
.add_node(crate::TSK_NODE_IS_SAMPLE, 1.0, pop_id, crate::TSK_NULL)
.add_node(crate::TSK_NODE_IS_SAMPLE, 1.0, pop_id, IndividualId::NULL)
.unwrap();
tables.add_edge(0., tables.sequence_length(), 1, 0).unwrap();
tables
Expand Down Expand Up @@ -1286,7 +1291,7 @@ mod test_bad_metadata {
let mut tables = TableCollection::new(1.).unwrap();
let md = F { x: 1, y: 11 };
tables
.add_mutation_with_metadata(0, 0, crate::TSK_NULL, 0.0, None, Some(&md))
.add_mutation_with_metadata(0, 0, MutationId::NULL, 0.0, None, Some(&md))
.unwrap();
if tables.mutations().metadata::<Ff>(0.into()).is_ok() {
panic!("expected an error!!");
Expand All @@ -1307,26 +1312,26 @@ mod test_adding_node {
fn test_adding_node_without_metadata() {
let mut tables = make_empty_table_collection(10.);

match tables.add_node(0, 0.0, TSK_NULL, TSK_NULL) {
match tables.add_node(0, 0.0, PopulationId::NULL, IndividualId::NULL) {
Ok(NodeId(0)) => (),
_ => panic!("Expected NodeId(0)"),
};

let row = tables.nodes().row(NodeId::from(0)).unwrap();

assert_eq!(row.id, NodeId::from(0));
assert_eq!(row.population, PopulationId::from(TSK_NULL));
assert_eq!(row.individual, IndividualId::from(TSK_NULL));
assert_eq!(row.population, PopulationId::NULL);
assert_eq!(row.individual, IndividualId::NULL);
assert!(row.metadata.is_none());

let row_id = tables
.add_node(0, 0.0, PopulationId::from(2), TSK_NULL)
.add_node(0, 0.0, PopulationId::from(2), IndividualId::NULL)
.unwrap();

assert_eq!(tables.nodes().population(row_id).unwrap(), PopulationId(2));
assert_eq!(
tables.nodes().individual(row_id).unwrap(),
IndividualId(TSK_NULL)
IndividualId::NULL,
);
assert!(tables
.nodes()
Expand All @@ -1343,12 +1348,12 @@ mod test_adding_node {
.is_ok());

let row_id = tables
.add_node(0, 0.0, TSK_NULL, IndividualId::from(17))
.add_node(0, 0.0, PopulationId::NULL, IndividualId::from(17))
.unwrap();

assert_eq!(
tables.nodes().population(row_id).unwrap(),
PopulationId(TSK_NULL)
PopulationId::NULL,
);
assert_eq!(tables.nodes().individual(row_id).unwrap(), IndividualId(17));

Expand Down Expand Up @@ -1391,7 +1396,7 @@ mod test_adding_individual {
#[test]
fn test_adding_individual_without_metadata() {
let mut tables = make_empty_table_collection(10.);
match tables.add_individual(0, &[0., 0., 0.], &[TSK_NULL, TSK_NULL]) {
match tables.add_individual(0, &[0., 0., 0.], &[IndividualId::NULL, IndividualId::NULL]) {
Ok(IndividualId(0)) => (),
_ => panic!("Expected NodeId(0)"),
};
Expand All @@ -1403,7 +1408,7 @@ mod test_adding_individual {

assert_eq!(
row.parents,
Some(vec![IndividualId(TSK_NULL), IndividualId(TSK_NULL),])
Some(vec![IndividualId::NULL, IndividualId::NULL,])
);

// Empty slices are a thing, causing None to be in the rows.
Expand Down
Loading