Implement BitXorAssign for BigUint

This commit is contained in:
Alice Ryhl 2017-09-02 23:40:51 +02:00
parent 8c3b2de11c
commit 2f6c0bf354
1 changed files with 15 additions and 8 deletions

View File

@ -1,7 +1,7 @@
use std::borrow::Cow; use std::borrow::Cow;
use std::default::Default; use std::default::Default;
use std::iter::repeat; use std::iter::repeat;
use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Rem, Shl, Shr, Sub, AddAssign, SubAssign, MulAssign, DivAssign, RemAssign, BitAndAssign, BitOrAssign}; use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Rem, Shl, Shr, Sub, AddAssign, SubAssign, MulAssign, DivAssign, RemAssign, BitAndAssign, BitOrAssign, BitXorAssign};
use std::str::{self, FromStr}; use std::str::{self, FromStr};
use std::fmt; use std::fmt;
use std::cmp; use std::cmp;
@ -314,20 +314,27 @@ impl<'a> BitOrAssign<&'a BigUint> for BigUint {
} }
forward_all_binop_to_val_ref_commutative!(impl BitXor for BigUint, bitxor); forward_all_binop_to_val_ref_commutative!(impl BitXor for BigUint, bitxor);
forward_val_assign!(impl BitXorAssign for BigUint, bitxor_assign);
impl<'a> BitXor<&'a BigUint> for BigUint { impl<'a> BitXor<&'a BigUint> for BigUint {
type Output = BigUint; type Output = BigUint;
fn bitxor(self, other: &BigUint) -> BigUint { fn bitxor(mut self, other: &BigUint) -> BigUint {
let mut data = self.data; self ^= other;
for (ai, &bi) in data.iter_mut().zip(other.data.iter()) { self
}
}
impl<'a> BitXorAssign<&'a BigUint> for BigUint {
#[inline]
fn bitxor_assign(&mut self, other: &BigUint) {
for (ai, &bi) in self.data.iter_mut().zip(other.data.iter()) {
*ai ^= bi; *ai ^= bi;
} }
if other.data.len() > data.len() { if other.data.len() > self.data.len() {
let extra = &other.data[data.len()..]; let extra = &other.data[self.data.len()..];
data.extend(extra.iter().cloned()); self.data.extend(extra.iter().cloned());
} }
BigUint::new(data) self.normalize();
} }
} }