From fbe79317a3d196a49ef878c5529ca46b412db329 Mon Sep 17 00:00:00 2001 From: Dev McElhinney Date: Wed, 6 Apr 2022 17:00:49 +0800 Subject: [PATCH] refactor guess time/freq res into guess interval --- src/context/helpers.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/context/helpers.rs b/src/context/helpers.rs index fe6c9527..2ae2fbb6 100644 --- a/src/context/helpers.rs +++ b/src/context/helpers.rs @@ -1,3 +1,5 @@ +use std::ops::Sub; + use mwa_hyperdrive_common::{ hifitime::{Duration, Epoch, Unit}, itertools::Itertools, @@ -8,20 +10,22 @@ lazy_static::lazy_static! { static ref DURATION_MAX: Duration = Duration::from_f64(f64::MAX, Unit::Second); } -pub fn guess_time_res(timestamps: Vec) -> Option { - timestamps +pub fn guess_interval(elements: &[T]) -> Option +where + T: Sub + Copy, + O: PartialOrd, +{ + elements .iter() .tuple_windows() - .fold(None, |result, (&past, &future)| { - Some(result.unwrap_or(*DURATION_MAX).min(future - past)) - }) + .map(|(&a, &b)| b.sub(a)) + .min_by(|a, b| a.partial_cmp(b).unwrap()) +} + +pub fn guess_time_res(timestamps: Vec) -> Option { + guess_interval(×tamps) } pub fn guess_freq_res(frequencies: Vec) -> Option { - frequencies - .iter() - .tuple_windows() - .fold(None, |result, (&low, &high)| { - Some(result.unwrap_or(f64::MAX).min(high - low)) - }) + guess_interval(&frequencies) }