use crate::integer_vector::IntegerVector;
use core::cmp::max;
use malachite_base::num::arithmetic::traits::{EntrywiseMax, EntrywiseMaxAssign};
fn assert_same_dimension(v: &IntegerVector, w: &IntegerVector) {
assert_eq!(
v.elements.len(),
w.elements.len(),
"cannot take the entrywise maximum of vectors of different dimensions"
);
}
impl EntrywiseMax<Self> for IntegerVector {
type Output = Self;
#[inline]
fn entrywise_max(mut self, other: Self) -> Self {
self.entrywise_max_assign(other);
self
}
}
impl EntrywiseMax<&Self> for IntegerVector {
type Output = Self;
#[inline]
fn entrywise_max(mut self, other: &Self) -> Self {
self.entrywise_max_assign(other);
self
}
}
impl EntrywiseMax<IntegerVector> for &IntegerVector {
type Output = IntegerVector;
#[inline]
fn entrywise_max(self, mut other: IntegerVector) -> IntegerVector {
other.entrywise_max_assign(self);
other
}
}
impl EntrywiseMax<&IntegerVector> for &IntegerVector {
type Output = IntegerVector;
fn entrywise_max(self, other: &IntegerVector) -> IntegerVector {
assert_same_dimension(self, other);
IntegerVector {
elements: self
.elements
.iter()
.zip(&other.elements)
.map(|(x, y)| max(x, y).clone())
.collect(),
}
}
}
impl EntrywiseMaxAssign<Self> for IntegerVector {
fn entrywise_max_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 EntrywiseMaxAssign<&Self> for IntegerVector {
fn entrywise_max_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);
}
}
}
}