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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use num::{self, NumCast};
use std::error::Error;
use std::fmt;
use std::vec::Vec;
#[cfg(not(feature = "parallel"))]
pub trait Integer: NumCast + num::Integer + Copy {}
#[cfg(not(feature = "parallel"))]
impl<T> Integer for T where T: NumCast + num::Integer + Copy {}
#[cfg(feature = "parallel")]
pub trait Integer: NumCast + num::Integer + Copy + std::marker::Send + Sync {}
#[cfg(feature = "parallel")]
impl<T> Integer for T where T: NumCast + num::Integer + Copy + std::marker::Send + Sync {}
#[cfg(not(feature = "parallel"))]
pub trait Float: NumCast + num::Float + Copy {}
#[cfg(not(feature = "parallel"))]
impl<T> Float for T where T: NumCast + num::Float + Copy {}
#[cfg(feature = "parallel")]
pub trait Float: NumCast + num::Float + Copy + std::marker::Send + Sync {}
#[cfg(feature = "parallel")]
impl<T> Float for T where T: NumCast + num::Float + Copy + std::marker::Send + Sync {}
pub fn to_base_fixed<T: Integer>(num: T, base: T, degree: T) -> Vec<T>
where
{
let mut new_base = vec![T::from(0).unwrap(); degree.to_usize().unwrap()];
let mut new_num = num;
for i in 0..degree.to_usize().unwrap() {
let i = i as usize;
new_base[i] = new_num % base;
new_num = new_num / base;
}
new_base
}
pub fn poly_eval<T>(coeffs: &[T], base: T) -> T
where
T: Integer + NumCast + Copy,
{
let mut result: T = T::from(0).unwrap();
for coefficient in coeffs.iter().rev() {
result = (result * base) + *coefficient;
}
result
}
#[derive(Debug)]
pub enum ErrorKind {
InvalidParams,
RuntimeError,
}
#[derive(Debug)]
pub struct OarsError {
error_type: ErrorKind,
desc: String,
}
pub type OarsResult<T> = Result<T, OarsError>;
impl Error for OarsError {
fn description(&self) -> &str {
&self.desc
}
}
impl fmt::Display for OarsError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "OA Construction Error: {}", &self.desc)
}
}
impl OarsError {
pub fn new<T>(kind: ErrorKind, msg: T) -> Self
where
T: Into<String>,
{
OarsError {
error_type: kind,
desc: msg.into(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_to_base_fixed() {
let num = 5;
let base = 2;
let mut res = to_base_fixed(num, base, 3);
res.reverse();
assert!(res == vec![1, 0, 1]);
let num = 1;
let base = 2;
let mut res = to_base_fixed(num, base, 3);
res.reverse();
assert!(res == vec![0, 0, 1]);
let num = 9;
let base = 2;
let mut res = to_base_fixed(num, base, 5);
res.reverse();
assert!(res == vec![0, 1, 0, 0, 1]);
let num = 7;
let base = 3;
let mut res = to_base_fixed(num, base, 2);
res.reverse();
assert!(res == vec![2, 1]);
}
#[test]
fn test_poly_eval() {
let coeffs = vec![1, 1, 1, 1];
let base = 2;
let result = poly_eval(&coeffs, base);
assert!(result == 15);
let coeffs = vec![1, 2];
let base = 3;
let result = poly_eval(&coeffs, base);
assert!(result == 7);
let coeffs = vec![1, 0, 0, 1];
let base = 2;
let result = poly_eval(&coeffs, base);
assert!(result == 9);
let coeffs = vec![0, 0, 1];
let base = 3;
let result = poly_eval(&coeffs, base);
assert!(result == 9);
}
}