diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2020-11-12 12:23:41 +0200 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2020-11-12 19:14:41 +0100 |
commit | 8927bea9fafae63898f1d6099af7c305c063a067 (patch) | |
tree | 5c01fdf8b3f9e9be714af20b1df6ba025676fa29 | |
parent | 4a7e972ea2ddad4987934fb181fcc1b7e3d125f8 (diff) |
Simplify comparison operators
Change-Id: I9f1b386ddb4d7d5377151c54baee207b2444c7d9
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/105541
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
-rw-r--r-- | include/tools/bigint.hxx | 4 | ||||
-rw-r--r-- | tools/source/generic/bigint.cxx | 100 |
2 files changed, 24 insertions, 80 deletions
diff --git a/include/tools/bigint.hxx b/include/tools/bigint.hxx index a07ba53d7f92..6683f07e3121 100644 --- a/include/tools/bigint.hxx +++ b/include/tools/bigint.hxx @@ -114,7 +114,7 @@ public: TOOLS_DLLPUBLIC friend bool operator==( const BigInt& rVal1, const BigInt& rVal2 ); friend inline bool operator!=( const BigInt& rVal1, const BigInt& rVal2 ); TOOLS_DLLPUBLIC friend bool operator< ( const BigInt& rVal1, const BigInt& rVal2 ); - TOOLS_DLLPUBLIC friend bool operator> ( const BigInt& rVal1, const BigInt& rVal2 ); + friend inline bool operator> ( const BigInt& rVal1, const BigInt& rVal2 ); friend inline bool operator<=( const BigInt& rVal1, const BigInt& rVal2 ); friend inline bool operator>=( const BigInt& rVal1, const BigInt& rVal2 ); @@ -226,6 +226,8 @@ inline bool operator!=( const BigInt& rVal1, const BigInt& rVal2 ) return !(rVal1 == rVal2); } +inline bool operator>(const BigInt& rVal1, const BigInt& rVal2) { return rVal2 < rVal1; } + inline bool operator<=( const BigInt& rVal1, const BigInt& rVal2 ) { return !( rVal1 > rVal2); diff --git a/tools/source/generic/bigint.cxx b/tools/source/generic/bigint.cxx index 4a1ddfff391e..d90ac2447fa1 100644 --- a/tools/source/generic/bigint.cxx +++ b/tools/source/generic/bigint.cxx @@ -22,7 +22,7 @@ #include <osl/diagnose.h> #include <tools/bigint.hxx> - +#include <algorithm> #include <string.h> /** @@ -822,90 +822,32 @@ BigInt& BigInt::operator%=( const BigInt& rVal ) bool operator==( const BigInt& rVal1, const BigInt& rVal2 ) { - if ( rVal1.bIsBig || rVal2.bIsBig ) - { - BigInt nA, nB; - nA.MakeBigInt( rVal1 ); - nB.MakeBigInt( rVal2 ); - if ( nA.bIsNeg == nB.bIsNeg ) - { - if ( nA.nLen == nB.nLen ) - { - int i; - for ( i = nA.nLen - 1; i > 0 && nA.nNum[i] == nB.nNum[i]; i-- ) - { - } - - return nA.nNum[i] == nB.nNum[i]; - } - return false; - } - return false; - } - return rVal1.nVal == rVal2.nVal; + if (!rVal1.bIsBig && !rVal2.bIsBig) + return rVal1.nVal == rVal2.nVal; + + BigInt nA, nB; + nA.MakeBigInt(rVal1); + nB.MakeBigInt(rVal2); + return nA.bIsNeg == nB.bIsNeg && nA.nLen == nB.nLen + && std::equal(nA.nNum, nA.nNum + nA.nLen, nB.nNum); } bool operator<( const BigInt& rVal1, const BigInt& rVal2 ) { - if ( rVal1.bIsBig || rVal2.bIsBig ) - { - BigInt nA, nB; - nA.MakeBigInt( rVal1 ); - nB.MakeBigInt( rVal2 ); - if ( nA.bIsNeg == nB.bIsNeg ) - { - if ( nA.nLen == nB.nLen ) - { - int i; - for ( i = nA.nLen - 1; i > 0 && nA.nNum[i] == nB.nNum[i]; i-- ) - { - } + if (!rVal1.bIsBig && !rVal2.bIsBig) + return rVal1.nVal < rVal2.nVal; - if ( nA.bIsNeg ) - return nA.nNum[i] > nB.nNum[i]; - else - return nA.nNum[i] < nB.nNum[i]; - } - if ( nA.bIsNeg ) - return nA.nLen > nB.nLen; - else - return nA.nLen < nB.nLen; - } + BigInt nA, nB; + nA.MakeBigInt(rVal1); + nB.MakeBigInt(rVal2); + if (nA.bIsNeg != nB.bIsNeg) return !nB.bIsNeg; - } - return rVal1.nVal < rVal2.nVal; -} - -bool operator >(const BigInt& rVal1, const BigInt& rVal2 ) -{ - if ( rVal1.bIsBig || rVal2.bIsBig ) - { - BigInt nA, nB; - nA.MakeBigInt( rVal1 ); - nB.MakeBigInt( rVal2 ); - if ( nA.bIsNeg == nB.bIsNeg ) - { - if ( nA.nLen == nB.nLen ) - { - int i; - for ( i = nA.nLen - 1; i > 0 && nA.nNum[i] == nB.nNum[i]; i-- ) - { - } - - if ( nA.bIsNeg ) - return nA.nNum[i] < nB.nNum[i]; - else - return nA.nNum[i] > nB.nNum[i]; - } - if ( nA.bIsNeg ) - return nA.nLen < nB.nLen; - else - return nA.nLen > nB.nLen; - } - return !nA.bIsNeg; - } - - return rVal1.nVal > rVal2.nVal; + if (nA.nLen != nB.nLen) + return nA.bIsNeg ? (nA.nLen > nB.nLen) : (nA.nLen < nB.nLen); + int i = nA.nLen - 1; + while (i > 0 && nA.nNum[i] == nB.nNum[i]) + --i; + return nA.bIsNeg ? (nA.nNum[i] > nB.nNum[i]) : (nA.nNum[i] < nB.nNum[i]); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |