aryth/bound/vector/
mod.rs

1use veho::vector::iterate;
2
3use crate::types::Bound;
4
5pub fn bound<I>(it: I) -> Option<Bound<I::Item>> where
6    I: IntoIterator,
7    I::Item: Copy + PartialOrd,
8    I::IntoIter: Iterator<Item=I::Item>
9{
10    let iter = &mut it.into_iter();
11    iter.next().map(|ini|{
12        let (mut min, mut max) = (ini, ini);
13        iterate(iter, |x| { if x > max { max = x } else if x < min { min = x } });
14        Bound::new(min, max)
15    })
16}
17
18#[cfg(test)]
19mod tests {
20    use veho::entries::IntoHashmap;
21    use veho::hashmap::Mappers;
22
23    use crate::utils::option_to_string;
24
25    use super::*;
26
27    #[test]
28    fn test_ref_alpha() {
29        let vec = vec![4, 5, 9, 3, 7, 1];
30        let k = "some";
31        let bounded = bound(&vec);
32        println!("{} bound: {}", k, bounded.unwrap());
33        println!("{} original: {:?}", k, vec);
34    }
35
36    #[test]
37    fn test_ref_beta() {
38        let candidates = vec![
39            ("empty", vec![]),
40            ("sole", vec![1]),
41            ("some", vec![4, 5, 9, 3, 7, 1])
42        ].into_hashmap();
43        (&candidates).iterate(|k, vec| {
44            let bounded = bound(vec);
45            println!("{} bound: {}", k, option_to_string(&bounded));
46            println!("{} original: {:?}", k, vec);
47        });
48        println!("original candidates = {:?}", candidates);
49    }
50}