From 991afee8250f4fd371861826cb40909408c2179f Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Thu, 20 Oct 2022 14:45:41 -0700 Subject: [PATCH] feat: add Deme::most_recent_deme_start_time --- src/specification.rs | 17 +++++++++++++++++ tests/test_graph.rs | 1 + 2 files changed, 18 insertions(+) diff --git a/src/specification.rs b/src/specification.rs index 7982ef10..5d28bf8f 100644 --- a/src/specification.rs +++ b/src/specification.rs @@ -2609,6 +2609,23 @@ impl Graph { Err(e) => Err(e.into()), } } + + /// Return the most recent end time of any deme + /// in the Graph. + /// + /// This function is useful to check if the most + /// recent end time is greater than zero, meaning + /// that the model ends at a time point ancestral to + /// "now". + pub fn most_recent_deme_end_time(&self) -> Time { + let init = self.demes[0].end_time(); + self.demes + .iter() + .skip(1) + .fold(init, |current_min, current_deme| { + std::cmp::min(current_min, current_deme.end_time()) + }) + } } #[cfg(test)] diff --git a/tests/test_graph.rs b/tests/test_graph.rs index 1bbbea25..a5152999 100644 --- a/tests/test_graph.rs +++ b/tests/test_graph.rs @@ -578,6 +578,7 @@ demes: let graph = demes::loads(yaml).unwrap(); let end_time = graph.deme(0).epochs()[0].end_time(); assert_eq!(100.0, f64::from(end_time)); + assert_eq!(graph.most_recent_deme_end_time(), 100.0); } // from demes-spec/test-cases/valid