Add is_zero() to trait WeightNum

This commit is contained in:
Michael Neumann 2016-02-27 22:36:04 +01:00
parent ed7c417013
commit ba128c0c93
2 changed files with 20 additions and 5 deletions

View File

@ -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<Output=Self> + Add<Output=Self> + Zero { }
pub trait WeightNum: PartialOrd + Copy + Sub<Output=Self> + Add<Output=Self> + Zero {
#[inline(always)]
fn is_zero(&self) -> bool {
self.partial_cmp(&Self::zero()) == Some(Ordering::Equal)
}
}
impl<T> WeightNum for T
where T: PartialOrd + Copy + Sub<Output=T> + Add<Output=T> + 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;

View File

@ -1,4 +1,3 @@
use std::cmp::Ordering;
use super::{WeightNum, Weights};
use super::square_matrix::SquareMatrix;
@ -22,7 +21,7 @@ impl<T: WeightNum> Weights for WeightMatrix<T> {
#[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