Skip to content

Commit

Permalink
Removed accumulator and put its code in add_assign.
Browse files Browse the repository at this point in the history
  • Loading branch information
tfpf committed Feb 23, 2024
1 parent f31c8b0 commit 4ca4f4c
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,23 +364,6 @@ impl Long {
base = &base * &base;
}
}
fn accumulate(accumulator: &mut Vec<i32>, operand: &[i32]) {
let (alen, olen) = (accumulator.len(), operand.len());
let mut carry = false;
for i in 0..std::cmp::max(alen, olen) {
let oi = if i < olen { operand[i] } else { 0 };
if i < alen {
(accumulator[i], carry) = Long::adc(accumulator[i], oi, carry);
} else {
let (sum, carry_) = Long::adc(0, oi, carry);
accumulator.push(sum);
carry = carry_;
}
}
if carry {
accumulator.push(1);
}
}
fn adc(a: i32, b: i32, carry: bool) -> (i32, bool) {
let sum = a + b + carry as i32;
if sum >= 1_000_000_000 {
Expand All @@ -396,7 +379,21 @@ impl Long {
}
impl std::ops::AddAssign<&Long> for Long {
fn add_assign(&mut self, other: &Long) {
Long::accumulate(&mut self.digits, &other.digits);
let (slen, olen) = (self.digits.len(), other.digits.len());
let mut carry = false;
for i in 0..std::cmp::max(slen, olen) {
let oi = if i < olen { other.digits[i] } else { 0 };
if i < slen {
(self.digits[i], carry) = Long::adc(self.digits[i], oi, carry);
} else {
let (sum, carry_) = Long::adc(0, oi, carry);
self.digits.push(sum);
carry = carry_;
}
}
if carry {
self.digits.push(1);
}
}
}
impl std::ops::Add<&Long> for &Long {
Expand Down

0 comments on commit 4ca4f4c

Please sign in to comment.