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
use dyn_clone::DynClone;
pub trait View: Send + DynClone {
fn update(&mut self, val: f64);
fn last(&self) -> f64;
}
dyn_clone::clone_trait_object!(View);
#[derive(Clone)]
pub struct SlidingWindow {
pub views: Vec<Box<dyn View>>,
}
impl SlidingWindow {
pub fn new() -> SlidingWindow {
return SlidingWindow { views: Vec::new() };
}
pub fn update(&mut self, val: f64) {
for i in 0..self.views.len() {
self.views[i].update(val);
}
}
pub fn last(&self) -> Vec<f64> {
let mut out: Vec<f64> = Vec::new();
for i in 0..self.views.len() {
let last = self.views[i].last();
out.push(last)
}
return out;
}
pub fn register_view(&mut self, view: Box<dyn View>) {
self.views.push(view);
}
}