From 17bd51d1975c45fd68479a41dfee6cf6331846fc Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 20 Aug 2021 16:51:23 +0200 Subject: [PATCH] abacus: bignum: add equality comparisons --- src/bignum/bignum.cc | 11 +++++++++++ src/bignum/bignum.hh | 10 ++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/bignum/bignum.cc b/src/bignum/bignum.cc index e47c9b8..4f0c810 100644 --- a/src/bignum/bignum.cc +++ b/src/bignum/bignum.cc @@ -27,6 +27,17 @@ BigNum::BigNum(std::int64_t number) { 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() { auto const it = std::find_if(digits_.rbegin(), digits_.rend(), [](auto v) { return v != 0; }); diff --git a/src/bignum/bignum.hh b/src/bignum/bignum.hh index c814e75..e1b13c4 100644 --- a/src/bignum/bignum.hh +++ b/src/bignum/bignum.hh @@ -10,7 +10,17 @@ class BigNum { public: 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: + bool equal(BigNum const& rhs) const; + void canonicalize(); bool is_canonicalized() const;