pub struct MultiZip<I: Iterator>(Vec<I>);
impl<I: Iterator> MultiZip<I> {
pub fn new(vec_of_iters: Vec<I>) -> Self {
Self(vec_of_iters)
}
}
impl<I: Iterator> Iterator for MultiZip<I> {
type Item = Vec<I::Item>;
fn next(&mut self) -> Option<Self::Item> {
self.0.iter_mut().map(Iterator::next).collect()
}
}
pub trait WrappingGet<T> {
fn wrapping_next(&self, idx: usize) -> &T;
fn wrapping_prev(&self, idx: usize) -> &T;
}
impl<T> WrappingGet<T> for &[T] {
fn wrapping_next(&self, idx: usize) -> &T {
&self[match idx {
_ if idx == self.len() - 1 => 0,
_ => idx + 1,
}]
}
fn wrapping_prev(&self, idx: usize) -> &T {
&self[match idx {
_ if idx == 0 => self.len() - 1,
_ => idx - 1,
}]
}
}
#[derive(Clone, Copy, Debug)]
pub struct FloatComparator {
rel_tol: f64,
abs_tol: f64,
}
impl FloatComparator {
pub fn new(rel_tol: f64, abs_tol: f64) -> Self {
Self { rel_tol, abs_tol }
}
#[inline]
pub fn isclose(self, a: f64, b: f64) -> bool {
if a == b {
return true;
}
if !a.is_finite() || !b.is_finite() {
return false;
}
let diff = (a - b).abs();
diff <= (self.rel_tol * b).abs() || diff <= (self.rel_tol * a).abs() || diff <= self.abs_tol
}
}
impl Default for FloatComparator {
fn default() -> Self {
Self::new(1e-9, 0.0)
}
}
pub fn isclose(a: f64, b: f64) -> bool {
FloatComparator::default().isclose(a, b)
}