use crate::integer::Integer;
use crate::integer_vector::IntegerVector;
use crate::natural::Natural;
use alloc::vec::Vec;
use core::ops::{Mul, MulAssign};
use malachite_base::num::arithmetic::traits::NegAssign;
use malachite_base::num::basic::traits::{One, Zero};
#[doc(hidden)]
pub fn integers_mul_scalar_assign(xs: &mut [Integer], c: &Integer) {
match *c {
integer_zero!() => xs.fill(Integer::ZERO),
integer_one!() => {}
integer_negative_one!() => {
for x in xs {
x.neg_assign();
}
}
_ => {
for x in xs {
*x *= c;
}
}
}
}
crate_test_fn! {
#[doc(hidden)]
integers_mul_scalar(xs: &[Integer], c: &Integer) -> Vec<Integer> {
match *c {
integer_zero!() => alloc::vec![Integer::ZERO; xs.len()],
integer_one!() => xs.to_vec(),
integer_negative_one!() => xs.iter().map(|x| -x).collect(),
_ => xs.iter().map(|x| x * c).collect(),
}
}
}
crate_test_fn! {
#[doc(hidden)]
integers_mul_scalar_to_out(out: &mut [Integer], xs: &[Integer], c: &Integer) {
let xs = &xs[..out.len()];
match *c {
integer_zero!() => out.fill(Integer::ZERO),
integer_one!() => out.clone_from_slice(xs),
integer_negative_one!() => {
for (o, x) in out.iter_mut().zip(xs) {
*o = -x;
}
}
_ => {
for (o, x) in out.iter_mut().zip(xs) {
*o = x * c;
}
}
}
}}
impl Mul<Integer> for IntegerVector {
type Output = Self;
#[inline]
fn mul(mut self, c: Integer) -> Self {
self *= &c;
self
}
}
impl Mul<&Integer> for IntegerVector {
type Output = Self;
#[inline]
fn mul(mut self, c: &Integer) -> Self {
self *= c;
self
}
}
impl Mul<Integer> for &IntegerVector {
type Output = IntegerVector;
#[inline]
fn mul(self, c: Integer) -> IntegerVector {
self * &c
}
}
impl Mul<&Integer> for &IntegerVector {
type Output = IntegerVector;
fn mul(self, c: &Integer) -> IntegerVector {
IntegerVector {
elements: integers_mul_scalar(&self.elements, c),
}
}
}
impl Mul<IntegerVector> for Integer {
type Output = IntegerVector;
#[inline]
fn mul(self, v: IntegerVector) -> IntegerVector {
v * self
}
}
impl Mul<&IntegerVector> for Integer {
type Output = IntegerVector;
#[inline]
fn mul(self, v: &IntegerVector) -> IntegerVector {
v * self
}
}
impl Mul<IntegerVector> for &Integer {
type Output = IntegerVector;
#[inline]
fn mul(self, v: IntegerVector) -> IntegerVector {
v * self
}
}
impl Mul<&IntegerVector> for &Integer {
type Output = IntegerVector;
#[inline]
fn mul(self, v: &IntegerVector) -> IntegerVector {
v * self
}
}
impl MulAssign<Integer> for IntegerVector {
#[inline]
fn mul_assign(&mut self, c: Integer) {
*self *= &c;
}
}
impl MulAssign<&Integer> for IntegerVector {
fn mul_assign(&mut self, c: &Integer) {
integers_mul_scalar_assign(&mut self.elements, c);
}
}