hist/
lib.rs

1pub struct Hist {
2
3    w:i32,
4    h:i32,
5    x:Vec<i32>,
6    y:Vec<i32>,
7
8}
9
10impl Hist {
11
12    // Function to initialize a Hist object
13    pub fn new(_w:i32, _h:i32, _x:&Vec<i32>, _y:&Vec<i32>) -> Hist {
14
15        assert!(_x.len() > 0 && _y.len() > 0, "Both len(x) and len(y) should be non-zero.");
16        assert!(_x.len() == _y.len(), "len(x) should be equal to len(y).");
17
18        Hist {
19            w:_w, 
20            h:_h,
21            x:_x.clone(),
22            y:_y.clone()
23        }
24
25    }
26
27    // Function to set the size (width, height) of the printed histogram
28    pub fn set_size(&mut self, _w:i32, _h:i32) {
29
30        self.w = _w;
31        self.h = _h;
32
33    }
34
35    // Function to set the width of the printed histogram
36    pub fn set_width(&mut self, _w:i32) {
37
38        self.w = _w;
39
40    }
41
42    // Function to set the height of the printed histogram
43    pub fn set_height(&mut self, _h:i32) {
44
45        self.h = _h;
46
47    }
48
49    // Function to get the size (width, height) of the printed histogram
50    pub fn get_size(&self) -> (i32, i32) {
51
52        (self.w, self.h)
53
54    }
55
56    // Function to get the width of the printed histogram
57    pub fn get_width(&self) -> i32 {
58
59        self.w
60
61    }
62
63    // Function to get the height of the printed histogram
64    pub fn get_height(&self) -> i32 {
65
66        self.h
67        
68    }
69
70    // Function to get the min data value
71    pub fn get_min(&self) -> i32 {
72
73        self.y.iter().min().unwrap().clone()
74
75    }
76
77    // Function to get the max data value
78    pub fn get_max(&self) -> i32 {
79
80        self.y.iter().max().unwrap().clone() 
81
82    }
83
84    pub fn get_mean(&self) -> i32 {
85
86        self.y.iter().fold(0i32, |sum, val| sum + val) / self.y.len() as i32
87
88    }
89
90    // Function to normalize the data
91    fn calc(&self) -> (Vec<i32>, Vec<i32>) {
92        
93        let norm_value = |val:i32, min:i32, max:i32, new_min:i32, new_max:i32| -> i32 {
94            new_min + ( (val - min) as f32 * ((new_max - new_min) as f32 / (max - min) as f32) as f32 ).ceil() as i32
95        };
96
97        let min_x = self.x.iter().min().unwrap().clone();
98        let max_x = self.x.iter().max().unwrap().clone();
99        let min_y = self.y.iter().min().unwrap().clone();
100        let max_y = self.y.iter().max().unwrap().clone();
101
102        (self.x.iter().map(|v| norm_value(*v, min_x, max_x, 0, self.w)).collect(),
103         self.y.iter().map(|v| norm_value(*v, min_y, max_y, 0, self.h)).collect()) as (Vec<i32>, Vec<i32>)
104
105    }
106
107    // Function to print the histogram on the console
108    pub fn display(&self) {
109
110        // Normalize Data
111
112        let (local_x, local_y) = self.calc();
113
114        // Display histogram
115
116        for row in 0..self.h + 1 {
117
118            for col in 0..self.w + 1 {
119
120                match local_x.binary_search(&col) {
121
122                    Ok(pos) => print!("{}", if (self.h - row) <= local_y[pos] {"*"} else {" "}),
123
124                    Err(_) => print!("{}", if row == self.h {"*"} else {" "}),
125
126                };
127
128            }
129
130            println!("");
131
132        }
133
134    }
135
136}