abacus: bignum: add equality comparisons

This commit is contained in:
Bruno BELANYI 2021-08-20 16:51:23 +02:00
parent 2cff603a48
commit 17bd51d197
2 changed files with 21 additions and 0 deletions

View file

@ -27,6 +27,17 @@ BigNum::BigNum(std::int64_t number) {
assert(is_canonicalized()); assert(is_canonicalized());
} }
bool BigNum::equal(BigNum const& rhs) const {
assert(is_canonicalized());
assert(rhs.is_canonicalized());
if (sign_ != rhs.sign_) {
return false;
}
return digits_ == rhs.digits_;
}
void BigNum::canonicalize() { void BigNum::canonicalize() {
auto const it = std::find_if(digits_.rbegin(), digits_.rend(), auto const it = std::find_if(digits_.rbegin(), digits_.rend(),
[](auto v) { return v != 0; }); [](auto v) { return v != 0; });

View file

@ -10,7 +10,17 @@ class BigNum {
public: public:
explicit BigNum(std::int64_t number); explicit BigNum(std::int64_t number);
friend bool operator==(BigNum const& lhs, BigNum const& rhs) {
return lhs.equal(rhs);
}
friend bool operator!=(BigNum const& lhs, BigNum const& rhs) {
return !(lhs == rhs);
}
private: private:
bool equal(BigNum const& rhs) const;
void canonicalize(); void canonicalize();
bool is_canonicalized() const; bool is_canonicalized() const;