diff options
author | Pieter Wuille <pieter.wuille@gmail.com> | 2014-04-21 08:28:43 +0200 |
---|---|---|
committer | Pieter Wuille <pieter.wuille@gmail.com> | 2014-05-09 16:45:00 +0200 |
commit | 397668ea63e148a92f68e9fae578595585616770 (patch) | |
tree | bae5d6b5f9aa30309fb65a6e03fbedff3de3f1cf /src | |
parent | df9eb5e14fa8072bc8a82b59e712c2ba36f13f4c (diff) |
Deduplicate uint* comparison operator logic
Diffstat (limited to 'src')
-rw-r--r-- | src/uint256.h | 89 |
1 files changed, 20 insertions, 69 deletions
diff --git a/src/uint256.h b/src/uint256.h index 10c6657c76..1acedd14bf 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -308,85 +308,28 @@ public: return ret; } - - friend inline bool operator<(const base_uint& a, const base_uint& b) - { - for (int i = base_uint::WIDTH-1; i >= 0; i--) - { - if (a.pn[i] < b.pn[i]) - return true; - else if (a.pn[i] > b.pn[i]) - return false; - } - return false; - } - - friend inline bool operator<=(const base_uint& a, const base_uint& b) - { - for (int i = base_uint::WIDTH-1; i >= 0; i--) - { - if (a.pn[i] < b.pn[i]) - return true; - else if (a.pn[i] > b.pn[i]) - return false; + int CompareTo(const base_uint& b) const { + for (int i = base_uint::WIDTH-1; i >= 0; i--) { + if (pn[i] < b.pn[i]) + return -1; + if (pn[i] > b.pn[i]) + return 1; } - return true; - } - - friend inline bool operator>(const base_uint& a, const base_uint& b) - { - for (int i = base_uint::WIDTH-1; i >= 0; i--) - { - if (a.pn[i] > b.pn[i]) - return true; - else if (a.pn[i] < b.pn[i]) - return false; - } - return false; + return 0; } - friend inline bool operator>=(const base_uint& a, const base_uint& b) - { - for (int i = base_uint::WIDTH-1; i >= 0; i--) - { - if (a.pn[i] > b.pn[i]) - return true; - else if (a.pn[i] < b.pn[i]) + bool EqualTo(uint64_t b) const { + for (int i = base_uint::WIDTH-1; i >= 2; i--) { + if (pn[i]) return false; } - return true; - } - - friend inline bool operator==(const base_uint& a, const base_uint& b) - { - for (int i = 0; i < base_uint::WIDTH; i++) - if (a.pn[i] != b.pn[i]) - return false; - return true; - } - - friend inline bool operator==(const base_uint& a, uint64_t b) - { - if (a.pn[0] != (unsigned int)b) + if (pn[1] != (b >> 32)) return false; - if (a.pn[1] != (unsigned int)(b >> 32)) + if (pn[0] != (b & 0xfffffffful)) return false; - for (int i = 2; i < base_uint::WIDTH; i++) - if (a.pn[i] != 0) - return false; return true; } - friend inline bool operator!=(const base_uint& a, const base_uint& b) - { - return (!(a == b)); - } - - friend inline bool operator!=(const base_uint& a, uint64_t b) - { - return (!(a == b)); - } - friend inline const base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; } friend inline const base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; } friend inline const base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; } @@ -397,6 +340,14 @@ public: friend inline const base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; } friend inline const base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; } friend inline const base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; } + friend inline bool operator==(const base_uint& a, const base_uint& b) { return a.CompareTo(b) == 0; } + friend inline bool operator!=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) != 0; } + friend inline bool operator>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) > 0; } + friend inline bool operator<(const base_uint& a, const base_uint& b) { return a.CompareTo(b) < 0; } + friend inline bool operator>=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) >= 0; } + friend inline bool operator<=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <= 0; } + friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); } + friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); } std::string GetHex() const { |