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