abacus: bignum: add order comparisons
This commit is contained in:
parent
17bd51d197
commit
2600eb2ba7
|
@ -38,6 +38,19 @@ bool BigNum::equal(BigNum const& rhs) const {
|
|||
return digits_ == rhs.digits_;
|
||||
}
|
||||
|
||||
bool BigNum::less_than(BigNum const& rhs) const {
|
||||
assert(is_canonicalized());
|
||||
assert(rhs.is_canonicalized());
|
||||
|
||||
if (sign_ != rhs.sign_) {
|
||||
return sign_ < rhs.sign_;
|
||||
}
|
||||
|
||||
return std::lexicographical_compare(digits_.rbegin(), digits_.rend(),
|
||||
rhs.digits_.rbegin(),
|
||||
rhs.digits_.rend());
|
||||
}
|
||||
|
||||
void BigNum::canonicalize() {
|
||||
auto const it = std::find_if(digits_.rbegin(), digits_.rend(),
|
||||
[](auto v) { return v != 0; });
|
||||
|
|
|
@ -18,8 +18,25 @@ public:
|
|||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
friend bool operator<(BigNum const& lhs, BigNum const& rhs) {
|
||||
return lhs.less_than(rhs);
|
||||
}
|
||||
|
||||
friend bool operator>(BigNum const& lhs, BigNum const& rhs) {
|
||||
return rhs < lhs;
|
||||
}
|
||||
|
||||
friend bool operator<=(BigNum const& lhs, BigNum const& rhs) {
|
||||
return !(lhs > rhs);
|
||||
}
|
||||
|
||||
friend bool operator>=(BigNum const& lhs, BigNum const& rhs) {
|
||||
return !(lhs < rhs);
|
||||
}
|
||||
|
||||
private:
|
||||
bool equal(BigNum const& rhs) const;
|
||||
bool less_than(BigNum const& rhs) const;
|
||||
|
||||
void canonicalize();
|
||||
bool is_canonicalized() const;
|
||||
|
|
Loading…
Reference in a new issue