argmin/core/math/
minmax_vec.rs

1// Copyright 2018-2020 argmin developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use crate::core::math::ArgminMinMax;
9
10impl<T> ArgminMinMax for Vec<T>
11where
12    T: std::cmp::PartialOrd + Clone,
13{
14    fn min(x: &Self, y: &Self) -> Vec<T> {
15        assert!(!x.is_empty());
16        assert_eq!(x.len(), y.len());
17
18        x.iter()
19            .zip(y.iter())
20            .map(|(a, b)| if a < b { a.clone() } else { b.clone() })
21            .collect()
22    }
23
24    fn max(x: &Self, y: &Self) -> Vec<T> {
25        assert!(!x.is_empty());
26        assert_eq!(x.len(), y.len());
27
28        x.iter()
29            .zip(y.iter())
30            .map(|(a, b)| if a > b { a.clone() } else { b.clone() })
31            .collect()
32    }
33}