use crate::integer_vector::IntegerVector;
use core::cmp::min;
use malachite_base::num::arithmetic::traits::{EntrywiseMin, EntrywiseMinAssign};
fn assert_same_dimension(v: &IntegerVector, w: &IntegerVector) {
assert_eq!(
v.elements.len(),
w.elements.len(),
"cannot take the entrywise minimum of vectors of different dimensions"
);
}
impl EntrywiseMin<Self> for IntegerVector {
type Output = Self;
#[inline]
fn entrywise_min(mut self, other: Self) -> Self {
self.entrywise_min_assign(other);
self
}
}
impl EntrywiseMin<&Self> for IntegerVector {
type Output = Self;
#[inline]
fn entrywise_min(mut self, other: &Self) -> Self {
self.entrywise_min_assign(other);
self
}
}
impl EntrywiseMin<IntegerVector> for &IntegerVector {
type Output = IntegerVector;
#[inline]
fn entrywise_min(self, mut other: IntegerVector) -> IntegerVector {
other.entrywise_min_assign(self);
other
}
}
impl EntrywiseMin<&IntegerVector> for &IntegerVector {
type Output = IntegerVector;
fn entrywise_min(self, other: &IntegerVector) -> IntegerVector {
assert_same_dimension(self, other);
IntegerVector {
elements: self
.elements
.iter()
.zip(&other.elements)
.map(|(x, y)| min(x, y).clone())
.collect(),
}
}
}
impl EntrywiseMinAssign<Self> for IntegerVector {
fn entrywise_min_assign(&mut self, other: Self) {
assert_same_dimension(self, &other);
for (x, y) in self.elements.iter_mut().zip(other.elements) {
if y < *x {
*x = y;
}
}
}
}
impl EntrywiseMinAssign<&Self> for IntegerVector {
fn entrywise_min_assign(&mut self, other: &Self) {
assert_same_dimension(self, other);
for (x, y) in self.elements.iter_mut().zip(&other.elements) {
if y < x {
x.clone_from(y);
}
}
}
}