abacus: bignum: factorize trim of leading zeros

This commit is contained in:
Bruno BELANYI 2021-08-21 02:14:59 +02:00
parent 4e3d53ecd4
commit 0436d2e513

View file

@ -20,6 +20,12 @@ bool do_less_than(digits_type const& lhs, digits_type const& rhs) {
rhs.rend()); rhs.rend());
} }
void do_trim_leading_zeros(digits_type& num) {
auto const it
= std::find_if(num.rbegin(), num.rend(), [](auto v) { return v != 0; });
num.erase(it.base(), num.end());
}
digits_type do_addition(digits_type const& lhs, digits_type const& rhs) { digits_type do_addition(digits_type const& lhs, digits_type const& rhs) {
int carry = 0; int carry = 0;
digits_type res; digits_type res;
@ -288,9 +294,7 @@ bool BigNum::less_than(BigNum const& rhs) const {
} }
void BigNum::canonicalize() { void BigNum::canonicalize() {
auto const it = std::find_if(digits_.rbegin(), digits_.rend(), do_trim_leading_zeros(digits_);
[](auto v) { return v != 0; });
digits_.erase(it.base(), digits_.end());
if (digits_.size() == 0) { if (digits_.size() == 0) {
sign_ = 0; sign_ = 0;