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
use crate::View;

/// Lower Than or Equal filter,
/// which only allows values lower than the specified clipping point through
pub struct LTE<V> {
    view: V,
    clipping_value: f64,
    out: f64,
}

impl<V> LTE<V>
where
    V: View,
{
    /// Create a new instance with a chained View
    #[inline(always)]
    pub fn new(view: V, clipping_value: f64) -> Self {
        Self {
            view,
            clipping_value,
            out: 0.0,
        }
    }
}

impl<V> View for LTE<V>
where
    V: View,
{
    #[inline]
    fn update(&mut self, val: f64) {
        self.view.update(val);
        let val = self.view.last();

        if val <= self.clipping_value {
            self.out = val;
        } else {
            self.out = self.clipping_value;
        }
    }

    #[inline(always)]
    fn last(&self) -> f64 {
        self.out
    }
}

#[cfg(test)]
mod tests {
    use crate::Echo;

    use super::*;

    #[test]
    fn lte() {
        let mut lte = LTE::new(Echo::new(), 1.0);
        lte.update(0.5);
        assert_eq!(lte.last(), 0.5);
        lte.update(1.5);
        assert_eq!(lte.last(), 1.0);
    }
}