-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwheel.rs
159 lines (144 loc) · 4.05 KB
/
wheel.rs
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
150
151
152
153
154
155
156
157
158
159
pub mod wheel {
#[derive(Debug, Clone, Copy)]
pub enum Wheel<T> {
NaN,
Inf,
NegInf,
Num(T),
}
use Wheel::*;
impl<T> Wheel<T> {
pub fn sign(&self) -> Option<Ordering> where T: Neg<Output = T> + Clone + PartialOrd {
self.clone().partial_cmp(&self.clone().neg())
}
pub fn num(self) -> Option<T> {
match self {
NaN | Inf | NegInf => None,
Num(x) => Some(x),
}
}
}
impl<'a, T> Into<Option<&'a T>> for &'a Wheel<T> {
fn into(self) -> Option<&'a T> {
match self {
NaN | Inf | NegInf => None,
Num(x) => Some(x),
}
}
}
impl<T: Display> Display for Wheel<T> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
NaN => write!(f, "NaN"),
Inf => write!(f, "Inf"),
NegInf => write!(f, "-Inf"),
Num(x) => x.fmt(f),
}
}
}
impl<T: PartialEq> PartialEq for Wheel<T> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Inf, Inf) | (NegInf, NegInf) => true,
(Num(x), Num(y)) => x.eq(y),
_ => false,
}
}
}
impl<T: PartialEq + Eq> Eq for Wheel<T> {}
impl<T: PartialOrd> PartialOrd for Wheel<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match (self, other) {
(NaN, _) | (_, NaN) => None,
(Inf, Inf) | (NegInf, NegInf) => Some(Equal),
(Inf, _) | (_, NegInf) => Some(Greater),
(_, Inf) | (NegInf, _) => Some(Less),
(Num(x), Num(y)) => x.partial_cmp(y),
}
}
}
impl<T: PartialOrd + Ord> Ord for Wheel<T> {
/// Panics if either self or other is NaN
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).unwrap()
}
}
impl<T: Neg<Output = T>> Neg for Wheel<T> {
type Output = Self;
fn neg(self) -> Self {
match self {
NaN => NaN,
Inf => NegInf,
NegInf => Inf,
Num(x) => Num(-x),
}
}
}
impl<T: Add<Output = T>> Add for Wheel<T> {
type Output = Self;
fn add(self, other: Self) -> Self {
match (self, other) {
(NaN, _) | (_, NaN) | (Inf, NegInf) | (NegInf, Inf) => NaN,
(Inf, _) | (_, Inf) => Inf,
(NegInf, _) | (_, NegInf) => NegInf,
(Num(x), Num(y)) => Num(x + y),
}
}
}
impl<T: Sub<Output = T>> Sub for Wheel<T> {
type Output = Self;
fn sub(self, other: Self) -> Self {
match (self, other) {
(NaN, _) | (_, NaN) | (Inf, Inf) | (NegInf, NegInf) => NaN,
(Inf, _) | (_, NegInf) => Inf,
(NegInf, _) | (_, Inf) => NegInf,
(Num(x), Num(y)) => Num(x - y),
}
}
}
impl<T: Clone + Neg<Output = T> + PartialOrd + Mul<Output = T>> Mul for Wheel<T> {
type Output = Self;
fn mul(self, other: Self) -> Self {
match (self, other) {
(NaN, _) | (_, NaN) => NaN,
(x @ Inf, y) | (x @ NegInf, y) => {
match y.sign() {
Some(Greater) => x,
Some(Less) => -x,
_ => NaN,
}
},
(Num(x), Num(y)) => Num(x * y),
(x, y) => y * x,
}
}
}
impl<T: Clone + Neg<Output = T> + Sub<Output = T> + PartialOrd + Div<Output = T>> Div for Wheel<T> {
type Output = Self;
fn div(self, other: Self) -> Self {
match (self, other) {
(NaN, _) | (_, NaN) | (Inf, Inf) | (Inf, NegInf) | (NegInf, Inf) | (NegInf, NegInf) => NaN,
(x @ Num(_), Inf) | (x @ Num(_), NegInf) => x.clone() - x,
(x @ Inf, y @ Num(_)) | (x @ NegInf, y @ Num(_)) => match y.sign() {
Some(Less) => -x,
_ => x,
},
(Num(x), Num(y)) => Num(x / y),
}
}
}
impl<T: Rem<Output = T> + Neg<Output = T>> Rem for Wheel<T> {
type Output = Self;
fn rem(self, other: Self) -> Self {
match (self, other) {
(NaN, _) | (_, NaN) | (Inf, _) | (NegInf, _) => NaN,
(x, Inf) => x,
(x, NegInf) => -x,
(Num(x), Num(y)) => Num(x % y),
}
}
}
use std::fmt::{self, Debug, Display, Formatter};
use std::cmp::{*, Ordering::*};
use std::ops::*;
}