Skip to content

Commit

Permalink
refactor guess time/freq res into guess interval
Browse files Browse the repository at this point in the history
  • Loading branch information
Dev McElhinney authored and cjordan committed Apr 6, 2022
1 parent a0fb55f commit fbe7931
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/context/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ops::Sub;

use mwa_hyperdrive_common::{
hifitime::{Duration, Epoch, Unit},
itertools::Itertools,
Expand All @@ -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<Epoch>) -> Option<Duration> {
timestamps
pub fn guess_interval<T, O>(elements: &[T]) -> Option<O>
where
T: Sub<Output = O> + 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<Epoch>) -> Option<Duration> {
guess_interval(&timestamps)
}

pub fn guess_freq_res(frequencies: Vec<f64>) -> Option<f64> {
frequencies
.iter()
.tuple_windows()
.fold(None, |result, (&low, &high)| {
Some(result.unwrap_or(f64::MAX).min(high - low))
})
guess_interval(&frequencies)
}

0 comments on commit fbe7931

Please sign in to comment.