-
Notifications
You must be signed in to change notification settings - Fork 32
/
useful.rs
252 lines (234 loc) · 5.45 KB
/
useful.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//! Useful missing tools
use std::ops::Range;
// =============================================================================
// Fundamental Utils
// =============================================================================
/// Near equal
///
/// # Examples
/// ```
/// extern crate peroxide;
/// use peroxide::fuga::*;
///
/// assert!(nearly_eq(1.0/3.0 * 3.0, 1));
/// ```
pub fn nearly_eq<S, T>(x: S, y: T) -> bool
where
S: Into<f64>,
T: Into<f64>,
{
let mut b: bool = false;
let e = 1e-7;
let p: f64 = x.into().abs();
let q: f64 = y.into().abs();
if (p - q).abs() < e || (p - q).abs() / (p + q).min(f64::MAX) < e {
b = true;
}
b
}
#[allow(unused_comparisons)]
pub fn tab(s: &str, space: usize) -> String {
let l = s.len();
let mut m: String = String::new();
let fs = format!("{}{}", " ".repeat(space - l), s);
m.push_str(&fs);
m
}
pub fn quot_rem(x: usize, y: usize) -> (i32, i32) {
((x / y) as i32, (x % y) as i32)
}
// =============================================================================
// Choose Utils
// =============================================================================
pub fn choose_shorter_string(x1: String, x2: String) -> String {
if x1.len() > x2.len() {
x2
} else {
x1
}
}
pub fn choose_shorter_vec(x1: &Vec<f64>, x2: &Vec<f64>) -> Vec<f64> {
if x1.len() > x2.len() {
x2.clone()
} else {
x1.clone()
}
}
pub fn choose_longer_vec(x1: &Vec<f64>, x2: &Vec<f64>) -> Vec<f64> {
if x1.len() <= x2.len() {
x2.clone()
} else {
x1.clone()
}
}
pub fn max<T>(v: Vec<T>) -> T
where
T: PartialOrd + Copy + Clone,
{
let l = v.len();
if l == 1 {
v[0]
} else {
let mut t = if v[0] >= v[1] { v[0] } else { v[1] };
for i in 2..v.len() {
if v[i] > t {
t = v[i];
}
}
t
}
}
pub fn min<T>(v: Vec<T>) -> T
where
T: PartialOrd + Copy + Clone,
{
let l = v.len();
if l == 1 {
v[0]
} else {
let mut t = if v[0] <= v[1] { v[0] } else { v[1] };
for i in 2..v.len() {
if v[i] < t {
t = v[i];
}
}
t
}
}
/// Signum function
pub fn sgn(x: usize) -> f64 {
if x % 2 == 0 {
1f64
} else {
-1f64
}
}
/// Vector compare
pub fn eq_vec(x: &[f64], y: &[f64], tol: f64) -> bool {
x.iter().zip(y.iter()).all(|(x, y)| (x - y).abs() <= tol)
}
// =============================================================================
// Vec of Tuples
// =============================================================================
/// Auto-zip
///
/// # Examples
/// ```
/// extern crate peroxide;
/// use peroxide::fuga::*;
///
/// let a = vec![1, 2, 3];
/// let a_zipped = auto_zip(&a);
/// assert_eq!(a_zipped, vec![(1, 2), (2, 3)]);
/// ```
pub fn auto_zip<T: Clone>(x: &Vec<T>) -> Vec<(T, T)> {
let x_head = x[0..x.len() - 1].to_vec();
let x_tail = x[1..x.len()].to_vec();
x_head.into_iter().zip(x_tail).collect()
}
/// Find the index of interval of x
///
/// # Examples
/// ```
/// extern crate peroxide;
/// use peroxide::fuga::*;
///
/// let x = vec![
/// (0, 5),
/// (5, 7),
/// (7, 10),
/// (10, 15),
/// (15, 20),
/// (20, 30)
/// ];
///
/// assert_eq!(find_interval(&x, 11), 3);
/// ```
pub fn find_interval<T: PartialOrd + PartialEq>(sorted_intervals: &Vec<(T, T)>, x: T) -> usize {
let mut i = 0;
let mut j = sorted_intervals.len() - 1;
// Check range
assert!(
x >= sorted_intervals[0].0,
"x is smaller than the smallest interval"
);
assert!(
x <= sorted_intervals[sorted_intervals.len() - 1].1,
"x is larger than the largest interval"
);
while i <= j {
let mid = (i + j) / 2;
if x < sorted_intervals[mid].0 {
j = mid - 1;
} else if x > sorted_intervals[mid].1 {
i = mid + 1;
} else {
return mid;
}
}
i
}
/// Generate Range of Intervals
///
/// # Examples
/// ```
/// extern crate peroxide;
/// use peroxide::fuga::*;
/// use std::ops::Range;
///
/// let x = vec![1, 2, 3, 4];
/// let r = gen_range(&x);
///
/// let answer = vec![
/// Range { start: 1, end: 2 },
/// Range { start: 2, end: 3 },
/// Range { start: 3, end: 4 },
/// ];
///
/// assert_eq!(r, answer);
/// ```
pub fn gen_range<T: Clone + PartialOrd>(x: &[T]) -> Vec<Range<T>> {
let mut r = Vec::new();
for i in 0..x.len() - 1 {
r.push(Range {
start: x[i].clone(),
end: x[i + 1].clone(),
});
}
r
}
/// Generate and Zip Range of Intervals
///
/// # Examples
/// ```
/// extern crate peroxide;
/// use peroxide::fuga::*;
/// use std::ops::Range;
///
/// let x: Vec<i32> = vec![1, 2, 3, 4];
/// let y: Vec<i32> = x.iter().map(|&t| t.pow(2)).collect();
/// let r = zip_range(&x, &y);
///
/// let answer = vec![
/// (Range { start: 1, end: 2 }, 1),
/// (Range { start: 2, end: 3 }, 4),
/// (Range { start: 3, end: 4 }, 9),
/// ];
///
/// assert_eq!(r, answer);
/// ```
pub fn zip_range<T: Clone + PartialOrd, U: Clone>(x: &[T], y: &[U]) -> Vec<(Range<T>, U)> {
y[0..x.len() - 1]
.iter()
.enumerate()
.map(|(i, yi)| {
(
Range {
start: x[i].clone(),
end: x[i + 1].clone(),
},
yi.clone(),
)
})
.collect()
}