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