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
pub struct Hist {
w:i32,
h:i32,
x:Vec<i32>,
y:Vec<i32>,
}
impl Hist {
pub fn new(_w:i32, _h:i32, _x:&Vec<i32>, _y:&Vec<i32>) -> Hist {
assert!(_x.len() > 0 && _y.len() > 0, "Both len(x) and len(y) should be non-zero.");
assert!(_x.len() == _y.len(), "len(x) should be equal to len(y).");
Hist {
w:_w,
h:_h,
x:_x.clone(),
y:_y.clone()
}
}
pub fn set_size(&mut self, _w:i32, _h:i32) {
self.w = _w;
self.h = _h;
}
pub fn set_width(&mut self, _w:i32) {
self.w = _w;
}
pub fn set_height(&mut self, _h:i32) {
self.h = _h;
}
pub fn get_size(&self) -> (i32, i32) {
(self.w, self.h)
}
pub fn get_width(&self) -> i32 {
self.w
}
pub fn get_height(&self) -> i32 {
self.h
}
pub fn get_min(&self) -> i32 {
self.y.iter().min().unwrap().clone()
}
pub fn get_max(&self) -> i32 {
self.y.iter().max().unwrap().clone()
}
pub fn get_mean(&self) -> i32 {
self.y.iter().fold(0i32, |sum, val| sum + val) / self.y.len() as i32
}
fn calc(&self) -> (Vec<i32>, Vec<i32>) {
let norm_value = |val:i32, min:i32, max:i32, new_min:i32, new_max:i32| -> i32 {
new_min + ( (val - min) as f32 * ((new_max - new_min) as f32 / (max - min) as f32) as f32 ).ceil() as i32
};
let min_x = self.x.iter().min().unwrap().clone();
let max_x = self.x.iter().max().unwrap().clone();
let min_y = self.y.iter().min().unwrap().clone();
let max_y = self.y.iter().max().unwrap().clone();
(self.x.iter().map(|v| norm_value(*v, min_x, max_x, 0, self.w)).collect(),
self.y.iter().map(|v| norm_value(*v, min_y, max_y, 0, self.h)).collect()) as (Vec<i32>, Vec<i32>)
}
pub fn display(&self) {
let (local_x, local_y) = self.calc();
for row in 0..self.h + 1 {
for col in 0..self.w + 1 {
match local_x.binary_search(&col) {
Ok(pos) => print!("{}", if (self.h - row) <= local_y[pos] {"*"} else {" "}),
Err(_) => print!("{}", if row == self.h {"*"} else {" "}),
};
}
println!("");
}
}
}