1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#![allow(dead_code)]

use std::fmt;
use std::cmp::Ordering;
use std::ops::Sub;
use std::ops::Add;
use std::ops::Mul;
use std::ops::Div;

/// Result of comparison as `f64`
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct Dssim(f64);

impl Dssim {
    pub fn new(v: f64) -> Dssim {
        debug_assert!(v.is_finite());
        Dssim(v)
    }
}

impl fmt::Display for Dssim {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        write!(fmt, "{:.6}", self.0)
    }
}

impl Eq for Dssim {
    // Result never has NaN
}

impl PartialEq<Dssim> for f64 {
    fn eq(&self, other: &Dssim) -> bool {
        *self == other.0
    }

    fn ne(&self, other: &Dssim) -> bool {
        *self != other.0
    }
}

impl PartialEq<f64> for Dssim {
    fn eq(&self, other: &f64) -> bool {
        self.0 == *other
    }

    fn ne(&self, other: &f64) -> bool {
        self.0 != *other
    }
}

impl From<Dssim> for f64 {
    fn from(s: Dssim) -> f64 {
        s.0
    }
}

impl From<f64> for Dssim {
    fn from(s: f64) -> Dssim {
        Dssim(s)
    }
}

impl<RHS: Into<f64>> Sub<RHS> for Dssim {
    type Output = f64;
    fn sub(self, r: RHS) -> Self::Output {
        let rval = r.into();
        debug_assert!(rval.is_finite());
        self.0.sub(rval)
    }
}


impl Sub<Dssim> for f64 {
    type Output = f64;
    fn sub(self, r: Dssim) -> Self::Output {
        self.sub(r.0)
    }
}

impl<RHS: Into<f64>> Add<RHS> for Dssim {
    type Output = f64;
    fn add(self, r: RHS) -> Self::Output {
        let rval = r.into();
        debug_assert!(rval.is_finite());
        self.0.add(rval)
    }
}

impl Add<Dssim> for f64 {
    type Output = f64;
    fn add(self, r: Dssim) -> Self::Output {
        self.add(r.0)
    }
}

impl<RHS: Into<f64>> Mul<RHS> for Dssim {
    type Output = Dssim;
    fn mul(self, r: RHS) -> Self::Output {
        let rval = r.into();
        debug_assert!(rval.is_finite());
        self.0.mul(rval).into()
    }
}

impl Mul<Dssim> for f64 {
    type Output = Dssim;
    fn mul(self, r: Dssim) -> Self::Output {
        self.mul(r.0).into()
    }
}

impl<RHS: Into<f64>> Div<RHS> for Dssim {
    type Output = f64;
    fn div(self, r: RHS) -> Self::Output {
        let rval = r.into();
        debug_assert!(rval.is_finite() && rval != 0.);
        self.0.div(rval)
    }
}

impl Div<Dssim> for f64 {
    type Output = f64;
    fn div(self, r: Dssim) -> Self::Output {
        debug_assert!(r.0 != 0.);
        self.div(r.0)
    }
}

impl PartialOrd<f64> for Dssim {
    fn partial_cmp(&self, other: &f64) -> Option<Ordering> {
        self.0.partial_cmp(other)
    }

    fn lt(&self, other: &f64) -> bool { self.0.lt(other) }
    fn le(&self, other: &f64) -> bool { self.0.le(other) }
    fn gt(&self, other: &f64) -> bool { self.0.gt(other) }
    fn ge(&self, other: &f64) -> bool { self.0.ge(other) }
}

impl PartialOrd<Dssim> for f64 {
    fn partial_cmp(&self, other: &Dssim) -> Option<Ordering> {
        self.partial_cmp(&other.0)
    }

    fn lt(&self, other: &Dssim) -> bool { self.lt(&other.0) }
    fn le(&self, other: &Dssim) -> bool { self.le(&other.0) }
    fn gt(&self, other: &Dssim) -> bool { self.gt(&other.0) }
    fn ge(&self, other: &Dssim) -> bool { self.ge(&other.0) }
}