diff --git a/src/lib.rs b/src/lib.rs index 8aa6c3c..5a9fa53 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,16 +27,32 @@ use std::num::Zero; use coverage::Coverage; use mark_matrix::MarkMatrix; pub use weight_matrix::WeightMatrix; +use std::cmp::Ordering; pub mod square_matrix; mod coverage; mod mark_matrix; pub mod weight_matrix; -pub trait WeightNum: PartialOrd + Copy + Sub + Add + Zero { } +pub trait WeightNum: PartialOrd + Copy + Sub + Add + Zero { + #[inline(always)] + fn is_zero(&self) -> bool { + self.partial_cmp(&Self::zero()) == Some(Ordering::Equal) + } +} -impl WeightNum for T -where T: PartialOrd + Copy + Sub + Add + Zero { } +impl WeightNum for usize { #[inline(always)] fn is_zero(&self) -> bool { *self == 0 } } +impl WeightNum for isize { #[inline(always)] fn is_zero(&self) -> bool { *self == 0 } } +impl WeightNum for u64 { #[inline(always)] fn is_zero(&self) -> bool { *self == 0 } } +impl WeightNum for i64 { #[inline(always)] fn is_zero(&self) -> bool { *self == 0 } } +impl WeightNum for u32 { #[inline(always)] fn is_zero(&self) -> bool { *self == 0 } } +impl WeightNum for i32 { #[inline(always)] fn is_zero(&self) -> bool { *self == 0 } } +impl WeightNum for u16 { #[inline(always)] fn is_zero(&self) -> bool { *self == 0 } } +impl WeightNum for i16 { #[inline(always)] fn is_zero(&self) -> bool { *self == 0 } } +impl WeightNum for u8 { #[inline(always)] fn is_zero(&self) -> bool { *self == 0 } } +impl WeightNum for i8 { #[inline(always)] fn is_zero(&self) -> bool { *self == 0 } } +impl WeightNum for f64 { #[inline(always)] fn is_zero(&self) -> bool { *self == 0.0 } } +impl WeightNum for f32 { #[inline(always)] fn is_zero(&self) -> bool { *self == 0.0 } } pub trait Weights { type T: WeightNum; diff --git a/src/weight_matrix.rs b/src/weight_matrix.rs index 8544655..13fc4e6 100644 --- a/src/weight_matrix.rs +++ b/src/weight_matrix.rs @@ -1,4 +1,3 @@ -use std::cmp::Ordering; use super::{WeightNum, Weights}; use super::square_matrix::SquareMatrix; @@ -22,7 +21,7 @@ impl Weights for WeightMatrix { #[inline] fn is_element_zero(&self, pos: (usize, usize)) -> bool { - self.c[pos].partial_cmp(&T::zero()) == Some(Ordering::Equal) + self.c[pos].is_zero() } // for each row, subtracts the minimum of that row from each other value in the