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
use basenum::{ BaseNum, BaseInt, BaseFloat, SignedNum, ApproxEq };
use std::ops::{ Add, Mul, Sub, Div, Rem, Not, BitAnd, BitOr, BitXor, Shl, Shr };
use rand::Rand;
use num::{ Float, One, Zero };
pub trait GenNum<E: BaseNum>
: Copy
+ Sized
+ Clone
+ One
+ Zero
+ Div<Self, Output = Self>
+ Rem<Self, Output = Self>
+ Add<E, Output = Self>
+ Mul<E, Output = Self>
+ Rand
{
fn from_s(x: E) -> Self;
fn map<F>(self, f: F) -> Self where F: Fn(E) -> E;
fn zip<F>(self, y: Self, f: F) -> Self where F: Fn(E, E) -> E;
fn split<F>(self, f: F) -> (Self, Self) where F: Fn(E) -> (E, E);
fn map2<F>(self, y: Self, f: F) -> (Self, Self) where F: Fn(E, E) -> (E, E);
}
macro_rules! impl_GenNum_for_scalar(
($t: ty) => {
impl GenNum<$t> for $t {
#[inline(always)]
fn from_s(x: $t) -> Self {
x
}
#[inline(always)]
fn map<F: Fn($t) -> $t>(self, f: F) -> $t {
f(self)
}
#[inline(always)]
fn zip<F: Fn($t, $t) -> $t>(self, y: $t, f: F) -> $t {
f(self, y)
}
#[inline(always)]
fn split<F: Fn($t) -> ($t, $t)>(self, f: F) -> ($t, $t) {
f(self)
}
#[inline(always)]
fn map2<F: Fn($t, $t) -> ($t, $t)>(self, y: $t, f: F) -> ($t, $t) {
f(self, y)
}
}
}
);
pub trait GenInt<I: BaseInt>
: GenNum<I>
+ Eq
+ Not<Output = Self>
+ BitAnd<Output = Self>
+ BitOr<Output = Self>
+ BitXor<Output = Self>
+ Shl<usize, Output = Self>
+ Shr<usize, Output = Self>
{}
pub trait GenIType: GenInt<i32> + SignedNum + Sub<i32, Output = Self> {}
impl_GenNum_for_scalar! { i32 }
impl GenInt<i32> for i32 {}
impl GenIType for i32 {}
pub trait GenUType: GenInt<u32> {}
impl_GenNum_for_scalar! { u32 }
impl GenInt<u32> for u32 {}
impl GenUType for u32 {}
pub trait GenFloat<F: BaseFloat>
: GenNum<F>
+ ApproxEq<BaseType = F>
+ SignedNum
+ Sub<F, Output = Self>
{
fn fma(&self, b: &Self, c: &Self) -> Self;
}
pub trait GenType: GenFloat<f32> {}
pub trait GenDType: GenFloat<f64> {}
macro_rules! impl_GenFloat_for_scalar(
($t: ty, $gt: path) => {
impl_GenNum_for_scalar! { $t }
impl GenFloat<$t> for $t {
fn fma(&self, b: &$t, c: &$t) -> $t {
Float::mul_add(*self, *b, *c)
}
}
impl $gt for $t {}
}
);
impl_GenFloat_for_scalar! { f32, GenType }
impl_GenFloat_for_scalar! { f64, GenDType }
pub trait GenBType: Eq {}
impl GenBType for bool {}