From 9faebc885566c108c4933403513622bc3eaaca14 Mon Sep 17 00:00:00 2001 From: "darpan.ka" Date: Fri, 13 Dec 2013 12:06:04 +0530 Subject: [PATCH] BigInteger class implementation Change-Id: I03d28759af4efcd5578b0253c35b9a1820952d9f Signed-off-by: darpan.ka --- inc/FBaseBigInteger.h | 4 +- src/base/FBaseBigInteger.cpp | 418 +++++++++++++++++++++++++++ src/base/FBase_BigIntegerImpl.cpp | 433 ++++++++++++++++++++++++++++ src/base/FBase_BigIntegerImpl.h | 585 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 1438 insertions(+), 2 deletions(-) create mode 100644 src/base/FBaseBigInteger.cpp create mode 100644 src/base/FBase_BigIntegerImpl.cpp create mode 100644 src/base/FBase_BigIntegerImpl.h diff --git a/inc/FBaseBigInteger.h b/inc/FBaseBigInteger.h index d585300..05fe89d 100644 --- a/inc/FBaseBigInteger.h +++ b/inc/FBaseBigInteger.h @@ -700,7 +700,7 @@ public: * -GetLastResult() should be called to retrieve the last set exception * * @exception E_SUCCESS The method is successful. - * @exception E_OUT_OF_RANGE The specified shiftDistance is less than 0 or grater than bit length of number in binary representation. + * @exception E_OUT_OF_RANGE The specified shiftDistance is less than 0. */ BigInteger ShiftLeft(int shiftDistance) const; @@ -718,7 +718,7 @@ public: * -GetLastResult() should be called to retrieve the last set exception * * @exception E_SUCCESS The method is successful. - * @exception E_OUT_OF_RANGE The specified shiftDistance is less than 0 or grater than bit length of number in binary representation. + * @exception E_OUT_OF_RANGE The specified shiftDistance is less than 0. */ BigInteger ShiftRight(int shiftDistance) const; diff --git a/src/base/FBaseBigInteger.cpp b/src/base/FBaseBigInteger.cpp new file mode 100644 index 0000000..23ba230 --- /dev/null +++ b/src/base/FBaseBigInteger.cpp @@ -0,0 +1,418 @@ +// +// Copyright (c) 2013 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +/** + * @file FBaseBigInteger.cpp + * @brief This is the implementation file for BigInteger class. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "FBase_BigIntegerImpl.h" +#include "FBaseUtil_AtomicOperations.h" +#include "FBase_StringConverter.h" + +namespace Tizen { namespace Base +{ +BigInteger::BigInteger() + : __pImpl(null) +{ + +} + +BigInteger::~BigInteger() +{ + if (__pImpl->__refCount > 1) + { + Utility::_AtomicOperations::AtomicDec(&(__pImpl->__refCount)); + __pImpl = null; + } + else + { + delete __pImpl; + } +} + +BigInteger::BigInteger(const BigInteger& value) +{ + Utility::_AtomicOperations::AtomicInc(&(value.__pImpl->__refCount)); + __pImpl = value.__pImpl; +} + +BigInteger::BigInteger(int64_t value) + : __pImpl(new (std::nothrow) _BigIntegerImpl(value)) +{ + SysTryReturnVoidResult(NID_BASE, __pImpl != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); +} + +BigInteger::BigInteger(const char* pStr) +{ + __pImpl = new (std::nothrow) _BigIntegerImpl(pStr, 10); + SysTryReturnVoidResult(NID_BASE, __pImpl != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); +} + +BigInteger::BigInteger(const char* pStr, int radix) +{ + SysTryReturnVoidResult(NID_BASE, (radix >= 2) && (radix <= 36), E_OUT_OF_RANGE, + "[%s] Radix should be in between 2 and 36.", GetErrorMessage(E_OUT_OF_RANGE)); + __pImpl = new (std::nothrow) _BigIntegerImpl(pStr, radix); + SysTryReturnVoidResult(NID_BASE, __pImpl != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); +} + +BigInteger::BigInteger(const Tizen::Base::ImmutableString& str) +{ + std::unique_ptr< char[] > pStr(_StringConverter::CopyToCharArrayN(str.GetPointer())); + SysTryReturnVoidResult(NID_BASE, pStr != null, GetLastResult(), "[%s] Memory allocation failed.", GetErrorMessage(GetLastResult())); + __pImpl = new (std::nothrow) _BigIntegerImpl(pStr.get(), 10); + SysTryReturnVoidResult(NID_BASE, __pImpl != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); +} + +BigInteger::BigInteger(const Tizen::Base::ImmutableString& str, int radix) +{ + SysTryReturnVoidResult(NID_BASE, (radix > 0) && (radix <= 36), E_OUT_OF_RANGE, + "[%s] Radix should be in between 2 and 36.", GetErrorMessage(E_OUT_OF_RANGE)); + std::unique_ptr< char[] > pStr(_StringConverter::CopyToCharArrayN(str.GetPointer())); + SysTryReturnVoidResult(NID_BASE, pStr != null, GetLastResult(), "[%s] Memory allocation failed.", GetErrorMessage(GetLastResult())); + __pImpl = new (std::nothrow) _BigIntegerImpl(pStr.get(), radix); + SysTryReturnVoidResult(NID_BASE, __pImpl != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); +} + +BigInteger::BigInteger(_BigIntegerImpl* pImpl) + : __pImpl(pImpl) +{ + SysTryReturnVoidResult(NID_BASE, __pImpl != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); +} + +BigInteger +BigInteger::Absolute(void) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return BigInteger(__pImpl->Absolute()); +} + +BigInteger +BigInteger::Add(const BigInteger& rhs) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return BigInteger(__pImpl->Add(rhs.__pImpl)); +} + +BigInteger +BigInteger::And(const BigInteger& rhs) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return BigInteger(__pImpl->And(rhs.__pImpl)); +} + +BigInteger +BigInteger::AndNot(const BigInteger& rhs) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return BigInteger(__pImpl->AndNot(rhs.__pImpl)); +} + +BigInteger +BigInteger::ClearBit(int bitIndex) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + int bitLen = __pImpl->GetBitLength(); + SysTryReturn(NID_BASE, (bitIndex >= 0) && (bitIndex < bitLen), BigInteger(), E_OUT_OF_RANGE, + "[%s] Bit index should be grater than 0 and less than bit length.", GetErrorMessage(E_OUT_OF_RANGE)); + return BigInteger(__pImpl->ClearBit(bitIndex)); +} + +int +BigInteger::CompareTo(const BigInteger& value) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return __pImpl->CompareTo(value.__pImpl); +} + +BigInteger +BigInteger::Divide(const BigInteger& rhs) const +{ + int num = rhs.__pImpl->ToInt64(); + SysTryReturn(NID_BASE, num > 0, BigInteger(), E_INVALID_ARG, "[%s] Value should be grater than 0.", GetErrorMessage(E_INVALID_ARG)); + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return BigInteger(__pImpl->Divide(rhs.__pImpl)); +} + +BigInteger::QuotientAndRemainder +BigInteger::DivideAndRemainderN(const BigInteger& rhs) const +{ + int num = rhs.__pImpl->ToInt64(); + SysTryReturn(NID_BASE, num > 0, QuotientAndRemainder(), E_INVALID_ARG, "[%s] Value should be grater than 0.", GetErrorMessage(E_INVALID_ARG)); + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return __pImpl->DivideAndRemainderN(rhs.__pImpl); +} + +bool +BigInteger::Equals(const Object& obj) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + const BigInteger* pOther = dynamic_cast< const BigInteger* >(&obj); + if (pOther == null) + { + return false; + } + + return (__pImpl->CompareTo(pOther->__pImpl) == 0) ? true : false; +} + +BigInteger +BigInteger::FlipBit(int bitIndex) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + int bitLen = __pImpl->GetBitLength(); + SysTryReturn(NID_BASE, (bitIndex >= 0) && (bitIndex < bitLen), BigInteger(), E_OUT_OF_RANGE, + "[%s] Bit index should be grater than 0 and less than bit length.", GetErrorMessage(E_OUT_OF_RANGE)); + return BigInteger(__pImpl->FlipBit(bitIndex)); +} + +int +BigInteger::GetSetBitCount(void) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return __pImpl->GetSetBitCount(); +} + +int +BigInteger::GetBitLength(void) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return __pImpl->GetBitLength(); +} + +int +BigInteger::GetHashCode(void) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + Tizen::Base::ImmutableString str = ToString(); + return str.GetHashCode(); +} + +int +BigInteger::GetLowestSetBit(void) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return __pImpl->GetLowestSetBit(); +} + +int +BigInteger::GetSign(void) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return __pImpl->GetSign(); +} + +BigInteger +BigInteger::GreaterCommonDivisor(const BigInteger& value) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return BigInteger(__pImpl->GreaterCommonDivisor(value.__pImpl)); +} + +bool +BigInteger::IsBitSet(int bitIndex) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + int bitLen = __pImpl->GetBitLength(); + SysTryReturn(NID_BASE, (bitIndex >= 0) && (bitIndex < bitLen), false, E_OUT_OF_RANGE, + "[%s] Bit index should be grater than 0 and less than bit length.", GetErrorMessage(E_OUT_OF_RANGE)); + return __pImpl->IsBitSet(bitIndex); +} + +BigInteger::PrimeNumberResult +BigInteger::IsProbablePrimeNumber(void) const +{ + return __pImpl->IsProbablePrimeNumber(); +} + +BigInteger +BigInteger::Maximum(const BigInteger& rhs) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return BigInteger(__pImpl->Maximum(rhs.__pImpl)); +} + +BigInteger +BigInteger::Minimum(const BigInteger& rhs) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return BigInteger(__pImpl->Minimum(rhs.__pImpl)); +} + +BigInteger +BigInteger::Modulus(const BigInteger& value) const +{ + int sign = value.__pImpl->GetSign(); + SysTryReturn(NID_BASE, sign > 0, BigInteger(), E_INVALID_ARG, "[%s] Value should be grater than 0.", GetErrorMessage(E_INVALID_ARG)); + + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return BigInteger(__pImpl->Modulus(value.__pImpl)); +} + +BigInteger +BigInteger::ModulusInverse(const BigInteger& value) const +{ + int sign = value.__pImpl->GetSign(); + SysTryReturn(NID_BASE, sign > 0, BigInteger(), E_INVALID_ARG, "[%s] Value should be grater than 0.", GetErrorMessage(E_INVALID_ARG)); + + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return BigInteger(__pImpl->ModulusInverse(value.__pImpl)); +} + +BigInteger +BigInteger::ModulusPower(const BigInteger& exponent, const BigInteger& modulo) const +{ + int sign = modulo.__pImpl->GetSign(); + SysTryReturn(NID_BASE, sign > 0, BigInteger(), E_INVALID_ARG, "[%s] Value should be grater than 0.", GetErrorMessage(E_INVALID_ARG)); + + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return BigInteger(__pImpl->ModulusPower(exponent.__pImpl, modulo.__pImpl)); +} + +BigInteger +BigInteger::Multiply(const BigInteger& rhs) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return BigInteger(__pImpl->Multiply(rhs.__pImpl)); +} + +BigInteger +BigInteger::Negate(void) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return BigInteger(__pImpl->Negate()); +} + +BigInteger +BigInteger::NextProbablePrimeNumber(void) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return BigInteger(__pImpl->NextProbablePrimeNumber()); +} + +BigInteger +BigInteger::Not(void) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return BigInteger(__pImpl->Not()); +} + +BigInteger +BigInteger::Or(const BigInteger& rhs) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return BigInteger(__pImpl->Or(rhs.__pImpl)); +} + +BigInteger +BigInteger::Power(int exponent) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + SysTryReturn(NID_BASE, exponent > 0, BigInteger(), E_INVALID_ARG, "[%s] Exponent should be grater than 0.", GetErrorMessage(E_INVALID_ARG)); + return BigInteger(__pImpl->Power(exponent)); +} + +BigInteger +BigInteger::Remainder(const BigInteger& divisor) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return BigInteger(__pImpl->Remainder(divisor.__pImpl)); +} + +BigInteger +BigInteger::SetBit(int bitIndex) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + int bitLen = __pImpl->GetBitLength(); + SysTryReturn(NID_BASE, (bitIndex >= 0) && (bitIndex < bitLen), BigInteger(), E_OUT_OF_RANGE, + "[%s] Bit index should be grater than 0 and less than bit length.", GetErrorMessage(E_OUT_OF_RANGE)); + return BigInteger(__pImpl->SetBit(bitIndex)); +} + +BigInteger +BigInteger::ShiftLeft(int shiftDistance) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + SysTryReturn(NID_BASE, shiftDistance >= 0, BigInteger(), E_OUT_OF_RANGE, + "[%s] shiftDistance should be grater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE)); + return BigInteger(__pImpl->ShiftLeft(shiftDistance)); +} + +BigInteger +BigInteger::ShiftRight(int shiftDistance) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + SysTryReturn(NID_BASE, shiftDistance >= 0, BigInteger(), E_OUT_OF_RANGE, + "[%s] shiftDistance should be grater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE)); + return BigInteger(__pImpl->ShiftRight(shiftDistance)); +} + +BigInteger +BigInteger::Subtract(const BigInteger& rhs) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return BigInteger(__pImpl->Subtract(rhs.__pImpl)); +} + +double +BigInteger::ToDouble() const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + double ret = __pImpl->ToDouble(); + SysTryReturn(NID_BASE, (ret != HUGE_VAL && ret != -HUGE_VAL), std::numeric_limits< double >::quiet_NaN(), + E_OUT_OF_RANGE, "[%s] Value cannot fit into a Double.", GetErrorMessage(E_OUT_OF_RANGE)); +} + +int64_t +BigInteger::ToInt64() const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return __pImpl->ToInt64(); +} + +Tizen::Base::ImmutableString +BigInteger::ToString() const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return __pImpl->ToString(10); +} + +Tizen::Base::ImmutableString +BigInteger::ToString(int radix) const +{ + SysTryReturn(NID_BASE, (radix > 0) && (radix <= 36), String(""), E_OUT_OF_RANGE, + "[%s] Radix should be in between 2 and 36.", GetErrorMessage(E_OUT_OF_RANGE)); + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return __pImpl->ToString(radix); +} + +BigInteger +BigInteger::Xor(const BigInteger& rhs) const +{ + SysAssertf(__pImpl != null, "Not yet constructed! Construct the object before use"); + return BigInteger(__pImpl->Xor(rhs.__pImpl)); +} +}} // Tizen::Base diff --git a/src/base/FBase_BigIntegerImpl.cpp b/src/base/FBase_BigIntegerImpl.cpp new file mode 100644 index 0000000..9582a61 --- /dev/null +++ b/src/base/FBase_BigIntegerImpl.cpp @@ -0,0 +1,433 @@ +// +// Copyright (c) 2013 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +/** + * @file FBase_BigIntegerImpl.cpp + * @brief This is the implementation file for _BigIntegerImpl class. + */ + +#include +#include +#include +#include +#include +#include "FBase_BigIntegerImpl.h" + +namespace Tizen { namespace Base +{ +//Constructors +_BigIntegerImpl::_BigIntegerImpl() + : __refCount(1) +{ + mpz_init(__arbInt); +} + +_BigIntegerImpl::~_BigIntegerImpl() +{ + mpz_clear(__arbInt); +} + +_BigIntegerImpl::_BigIntegerImpl(int64_t value) + : __refCount(1) +{ + // No need to call mpz_init() as mpz_set_si() does both intialisation and assignment + mpz_init_set_si(__arbInt, static_cast< int32_t >(value >> 32)); + mpz_mul_2exp(__arbInt, __arbInt, 32); + mpz_add_ui(__arbInt, __arbInt, static_cast< uint32_t >(value & 0xFFFFFFFF)); +} + +_BigIntegerImpl::_BigIntegerImpl(const char* pStr, int radix) + : __refCount(1) +{ + // No need to call mpz_init() as mpz_set_str() does both intialisation and assignment + int ret = mpz_init_set_str(__arbInt, pStr, radix); + if (ret != 0) + { + mpz_clear(__arbInt); + SysTryReturnVoidResult(NID_BASE, ret != 0, E_NUM_FORMAT, + "[%s] string does not represent valid number.", GetErrorMessage(E_NUM_FORMAT)); + } +} + +_BigIntegerImpl* +_BigIntegerImpl::Absolute(void) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_abs(pTemp->__arbInt, __arbInt); + return pTemp; +} + + +_BigIntegerImpl* +_BigIntegerImpl::Add(_BigIntegerImpl* rhs) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_add(pTemp->__arbInt, __arbInt, rhs->__arbInt); + return pTemp; +} + +_BigIntegerImpl* +_BigIntegerImpl::And(_BigIntegerImpl* rhs) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_and(pTemp->__arbInt, __arbInt, rhs->__arbInt); + return pTemp; +} + +_BigIntegerImpl* +_BigIntegerImpl::AndNot(_BigIntegerImpl* rhs) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_com(pTemp->__arbInt, rhs->__arbInt); + mpz_and(pTemp->__arbInt, __arbInt, pTemp->__arbInt); + return pTemp; +} + +_BigIntegerImpl* +_BigIntegerImpl::ClearBit(int bitIndex) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_set(pTemp->__arbInt, __arbInt); + mpz_clrbit(pTemp->__arbInt, bitIndex); + return pTemp; +} + +int +_BigIntegerImpl::CompareTo(_BigIntegerImpl* value) const +{ + return mpz_cmp(__arbInt, value->__arbInt); +} + +_BigIntegerImpl* +_BigIntegerImpl::Divide(_BigIntegerImpl* rhs) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_div(pTemp->__arbInt, __arbInt, rhs->__arbInt); + return pTemp; +} + +BigInteger::QuotientAndRemainder +_BigIntegerImpl::DivideAndRemainderN(_BigIntegerImpl* rhs) const +{ + BigInteger::QuotientAndRemainder quoRem; + _BigIntegerImpl* pQuotient = new (std::nothrow) _BigIntegerImpl(); + _BigIntegerImpl* pRemainer = new (std::nothrow) _BigIntegerImpl(); + + mpz_fdiv_qr(pQuotient->__arbInt, pRemainer->__arbInt, __arbInt, rhs->__arbInt); + quoRem.quotient = new (std::nothrow) BigInteger(pQuotient); + quoRem.remainder = new (std::nothrow) BigInteger(pRemainer); + return quoRem; +} + +_BigIntegerImpl* +_BigIntegerImpl::FlipBit(int index) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_set(pTemp->__arbInt, __arbInt); + mpz_combit(pTemp->__arbInt, index); + return pTemp; +} + +int +_BigIntegerImpl::GetSetBitCount(void) const +{ + + char* binaryRep = mpz_get_str(0, 2, __arbInt); + int len = strlen(binaryRep); + int count = 0; + //find out the number of bits set to 1 + for(int i = 0; i < len; ++i) + { + if(binaryRep[i] == '1') + { + ++count; + } + } + return count; +} + +int +_BigIntegerImpl::GetBitLength(void) const +{ + + char* binaryRep = mpz_get_str(0, 2, __arbInt); + return strlen(binaryRep); +} + +int +_BigIntegerImpl::GetHashCode(void) const +{ + String str(mpz_get_str(0, 10, __arbInt)); + return str.GetHashCode(); +} + +int +_BigIntegerImpl::GetLowestSetBit(void) const +{ + return mpz_scan1(__arbInt, 0); +} + +int +_BigIntegerImpl::GetSign(void) const +{ + return mpz_sgn(__arbInt); +} + +_BigIntegerImpl* +_BigIntegerImpl::GreaterCommonDivisor(_BigIntegerImpl* value) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_gcd(pTemp->__arbInt, __arbInt, value->__arbInt); + return pTemp; +} + +bool +_BigIntegerImpl::IsBitSet(int bitIndex) const +{ + if (mpz_tstbit(__arbInt, bitIndex) == 1) + { + return true; + } + return false; +} + +BigInteger::PrimeNumberResult +_BigIntegerImpl::IsProbablePrimeNumber(void) const +{ + int ret = mpz_probab_prime_p(__arbInt, 25); + if (ret == 0) + { + return BigInteger::DEFINITELY_COMPOSITE; + } + else if (ret == 1) + { + return BigInteger::PROBABLY_PRIME; + } + else if (ret == 2) + { + return BigInteger::DEFINITELY_PRIME; + } +} + +_BigIntegerImpl* +_BigIntegerImpl::Maximum(_BigIntegerImpl* rhs) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + if (mpz_cmp(__arbInt, rhs->__arbInt) > 0) + { + mpz_set(pTemp->__arbInt, __arbInt); + } + else + { + mpz_set(pTemp->__arbInt, rhs->__arbInt); + } + return pTemp; +} + +_BigIntegerImpl* +_BigIntegerImpl::Minimum(_BigIntegerImpl* rhs) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + if (mpz_cmp(__arbInt, rhs->__arbInt) < 0) + { + mpz_set(pTemp->__arbInt, __arbInt); + } + else + { + mpz_set(pTemp->__arbInt, rhs->__arbInt); + } + return pTemp; +} + +_BigIntegerImpl* +_BigIntegerImpl::Modulus(_BigIntegerImpl* value) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_mod(pTemp->__arbInt, __arbInt, value->__arbInt); + return pTemp; +} + +_BigIntegerImpl* +_BigIntegerImpl::ModulusInverse(_BigIntegerImpl* value) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_invert(pTemp->__arbInt, __arbInt, value->__arbInt); + return pTemp; +} + +_BigIntegerImpl* +_BigIntegerImpl::ModulusPower(_BigIntegerImpl* exponent, _BigIntegerImpl* modulo) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_powm(pTemp->__arbInt, __arbInt, exponent->__arbInt, modulo->__arbInt); + return pTemp; +} + +_BigIntegerImpl* +_BigIntegerImpl::Multiply(_BigIntegerImpl* rhs) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_mul(pTemp->__arbInt, __arbInt, rhs->__arbInt); + return pTemp; +} + +_BigIntegerImpl* +_BigIntegerImpl::Negate(void) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_neg(pTemp->__arbInt, __arbInt); + return pTemp; +} + +_BigIntegerImpl* +_BigIntegerImpl::NextProbablePrimeNumber(void) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_nextprime(pTemp->__arbInt, __arbInt); + return pTemp; +} + +_BigIntegerImpl* +_BigIntegerImpl::Not(void) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_com(pTemp->__arbInt, __arbInt); + return pTemp; +} + +_BigIntegerImpl* +_BigIntegerImpl::Or(_BigIntegerImpl* rhs) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_ior(pTemp->__arbInt, __arbInt, rhs->__arbInt); + return pTemp; +} + +_BigIntegerImpl* +_BigIntegerImpl::Power(int exponent) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_pow_ui(pTemp->__arbInt, __arbInt, exponent); + return pTemp; +} + +_BigIntegerImpl* +_BigIntegerImpl::Remainder(_BigIntegerImpl* divisor) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_cdiv_r(pTemp->__arbInt, __arbInt, divisor->__arbInt); + return pTemp; +} + +_BigIntegerImpl* +_BigIntegerImpl::SetBit(int bitIndex) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_set(pTemp->__arbInt, __arbInt); + mpz_setbit(pTemp->__arbInt, bitIndex); + return pTemp; +} + +_BigIntegerImpl* +_BigIntegerImpl::ShiftLeft(int shiftDistance) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_mul_2exp(pTemp->__arbInt, __arbInt, shiftDistance); + return pTemp; +} + +_BigIntegerImpl* +_BigIntegerImpl::ShiftRight(int shiftDistance) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_fdiv_q_2exp(pTemp->__arbInt, __arbInt, shiftDistance); + return pTemp; +} + +_BigIntegerImpl* +_BigIntegerImpl::Subtract(_BigIntegerImpl* rhs) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_sub(pTemp->__arbInt, __arbInt, rhs->__arbInt); + return pTemp; +} + +double +_BigIntegerImpl::ToDouble() const +{ + return mpz_get_d(__arbInt); +} + +int64_t +_BigIntegerImpl::ToInt64() const +{ + int32_t low, high; + mpz_t tmp; + mpz_init(tmp); + //Get lower 64 bits of __arbInt + mpz_mod_2exp( tmp, __arbInt, 64 ); + // tmp & 0xffffffff + low = mpz_get_si( tmp ); + // right shift tmp by 32 bits i.e tmp >>= 32 + mpz_div_2exp( tmp, tmp, 32 ); + //Get higher bits i.e hi = tmp & 0xffffffff + high = mpz_get_si( tmp ); + + mpz_clear( tmp ); + //Add higher 32 bits and lower 32 bits + return static_cast< int64_t >((static_cast < int64_t >(high) << 32) | low); +} + +Tizen::Base::ImmutableString +_BigIntegerImpl::ToString(int radix) const +{ + char* str = mpz_get_str(0, radix, __arbInt); + return String(str); +} + +_BigIntegerImpl* +_BigIntegerImpl::Xor(_BigIntegerImpl* rhs) const +{ + _BigIntegerImpl* pTemp = new (std::nothrow) _BigIntegerImpl(); + SysTryReturn(NID_BASE, pTemp != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); + mpz_xor(pTemp->__arbInt, __arbInt, rhs->__arbInt); + return pTemp; +} +}} // Tizen::Base diff --git a/src/base/FBase_BigIntegerImpl.h b/src/base/FBase_BigIntegerImpl.h new file mode 100644 index 0000000..48a20ea --- /dev/null +++ b/src/base/FBase_BigIntegerImpl.h @@ -0,0 +1,585 @@ +// +// Copyright (c) 2013 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +/** + * @file FBase_BigIntegerImpl.h + * @brief This is the header file for the %_BigIntegerImpl class. + * + * This header file contains the declarations of the %_BigIntegerImpl class. + */ + +#ifndef _FBASE_INTERNAL_BIG_INTEGER_IMPL_H_ +#define _FBASE_INTERNAL_BIG_INTEGER_IMPL_H_ + +#include +#include +#include +#include +#include + +using namespace Tizen::Base::Collection; +namespace Tizen { namespace Base +{ +class _BigIntegerImpl +{ +public: + + /* + * This is the default constructor for this class. @n + * + * @since 3.0 + */ + _BigIntegerImpl(); + + /** + * Initializes %_BigIntegerImpl instance with a random value in the range 0 to 2^numBits - 1. + * + * @since 3.0 + * + * @param[in] numBits Number of bits + * + */ + _BigIntegerImpl(int numBits); + + /** + * Initializes %_BigIntegerImpl instance with @c int64_t value. + * + * @since 3.0 + * + * @param[in] value 64-bit signed integral value + * + */ + _BigIntegerImpl(int64_t value); + + + /** + * Initializes %_BigIntegerImpl instance with null-terminated C string with given radix + * + * @since 3.0 + * + * @param[in] value Null-terminated C string representing a numeric value. + * @param[in] radix The radix of the string representing a numeric value @n + * Radix value range is from 2 to 36. + * + * @remarks -This method accepts only string representing integer value. Decimal separators are not allowed in the input string. + * -The behavior of this constructor is not dependent on the system default locale setting. + * + */ + _BigIntegerImpl(const char* pStr, int radix); + + /** + * Default destructor. + * + * @since 3.0 + * + * @remarks The internally allocated memory block is freed when the instance is destroyed. + */ + ~_BigIntegerImpl(); + + /** + * Calculates absolute value of the calling %_BigIntegerImpl instance and returns a new %_BigIntegerImpl instance containing absolute value. + * + * @since 3.0 + * + * @return New %_BigIntegerImpl instance. + * + */ + _BigIntegerImpl* Absolute(void) const; + + /** + * Adds value of the calling %_BigIntegerImpl instance with value of specified %_BigIntegerImpl rhs instance + * and returns a new %_BigIntegerImpl instance whose value is this + rhs. + * + * @since 3.0 + * + * @param[in] rhs Value to add with calling %_BigIntegerImpl instance. + * + * @return A new %_BigIntegerImpl instance containing this + rhs value. + * + */ + _BigIntegerImpl* Add(_BigIntegerImpl* rhs) const; + + /** + * Performs binary And of value of the calling %_BigIntegerImpl instance with value of specified %_BigIntegerImpl rhs instance + * and returns a new %_BigIntegerImpl instance whose value is this & rhs. + * + * @since 3.0 + * + * @param[in] rhs Value to and with calling %_BigIntegerImpl instance. + * + * @return A new %_BigIntegerImpl instance containing this & rhs value. + * + */ + _BigIntegerImpl* And(_BigIntegerImpl* rhs) const; + + /** + * Performs binary complement of value of specified %_BigIntegerImpl rhs instance and And'ed with value of calling %_BigIntegerImpl instance + * and returns a new %_BigIntegerImpl instance whose value is this & ~rhs. + * + * @since 3.0 + * + * @param[in] rhs Value is complemented and AND with calling %_BigIntegerImpl instance. + * + * @return A new %_BigIntegerImpl instance containing this & ~rhs value. + * + */ + _BigIntegerImpl* AndNot(_BigIntegerImpl* rhs) const; + + /** + * Clears bit at a specified position in the calling %_BigIntegerImpl instance and returns a new %_BigIntegerImpl instance. + * + * @since 3.0 + * + * @param[in] bitIndex Position where the bit has to be cleared. + * + * @return A new %_BigIntegerImpl instance. + * + */ + _BigIntegerImpl* ClearBit(int bitIndex) const; + + /** + * Compares value of calling %_BigIntegerImpl instance with value of specified %_BigIntegerImpl instance. + * + * @since 3.0 + * + * @param[in] value Value to compare with calling %_BigIntegerImpl instance.. + * + * @return Comparision value. + * @remarks + * 1 -If the calling %_BigIntegerImpl instance value is greater than value + * 0 -If the calling %_BigIntegerImpl instance value is equal to value + * -1 -If the calling %_BigIntegerImpl instance value is less than value + * + */ + int CompareTo(_BigIntegerImpl* value) const; + + /** + * Divides value of the calling %_BigIntegerImpl instance with value of specified %_BigIntegerImpl rhs instance + * and returns a new %_BigIntegerImpl instance whose value is this / rhs. + * + * @since 3.0 + * + * @param[in] rhs Divisor,value which divides the calling %_BigIntegerImpl instance. + * + * @return A new %_BigIntegerImpl instance containing this / rhs value. + * + * + */ + _BigIntegerImpl* Divide(_BigIntegerImpl* rhs) const; + + /** + * Divides value of the calling %_BigIntegerImpl instance with value of specified %_BigIntegerImpl rhs instance + * and returns a struct QuotientAndRemainder containing the quotient and remainder. + * + * @since 3.0 + * + * @param[in] rhs Divisor,value which divides the calling %_BigIntegerImpl instance. + * + * @return QuotientAndRemainder containing quotient and reminder. + * + * + */ + BigInteger::QuotientAndRemainder DivideAndRemainderN(_BigIntegerImpl* rhs) const; + + /** + * Compares whether the value of the calling %_BigIntegerImpl instance is equal to the specified object. + * + * @since 3.0 + * + * @param[in] obj An instance of object to compare. + * + * @return @c true if the values are equal else @c false + * + */ + bool Equals(const Object& obj) const; + + /** + * Flips bit at a specified position in the calling %_BigIntegerImpl instance and returns a new %_BigIntegerImpl instance. + * + * @since 3.0 + * + * @param[in] bitIndex Position where the bit is flipped. + * + * @return A new %_BigIntegerImpl instance. + * + */ + _BigIntegerImpl* FlipBit(int bitIndex) const; + + /** + * Gets the number of bits set to 1 in the value of the calling %_BigIntegerImpl instance. + * + * @since 3.0 + * + * @return Number of bits set to 1. + */ + int GetSetBitCount(void) const; + + /** + * Gets the number of bits in the binary representation value of the calling %_BigIntegerImpl instance. + * + * @since 3.0 + * + * @return Number of bits in the binary representation value of %_BigIntegerImpl instance. + */ + int GetBitLength(void) const; + + /** + * Gets the hash value of the calling %_BigIntegerImpl instance. + * + * @since 3.0 + * + * @return The hash value of the current instance. + */ + int GetHashCode(void) const; + + /** + * Gets the lowest bit set value of the calling %_BigIntegerImpl instance. + * + * @since 3.0 + * + * @return The position of the lowest bit set. + */ + int GetLowestSetBit(void) const; + + /** + * Gets the sign of the value of the calling %_BigIntegerImpl instance. + * + * @since 3.0 + * + * @return The sign of the value. + * + * @remarks + * -1 -If the value < 0 + * 0 -If the value == 0 + * 1 -If the value > 0 + */ + int GetSign(void) const; + + /** + * Gets the greater common divisor of the value of calling %_BigIntegerImpl instance and value of the specified %_BigIntegerImpl instance + * and returns a new %_BigIntegerImpl instance containing value of the greater common divisor. + * + * @since 3.0 + * + * @param[in] value Value with which the greater common divisor is computed + * + * @return A new %_BigIntegerImpl instance containing value of greater common divisor. + * + * @remarks + * Zero is returned if the value of calling %_BigIntegerImpl instance is 0 or value of the specified %_BigIntegerImpl instance is 0 + * + */ + _BigIntegerImpl* GreaterCommonDivisor(_BigIntegerImpl* value) const; + + /** + * Checks whether the bit at specified index in the value of calling %_BigIntegerImpl instance is set. + * + * @since 3.0 + * + * @param[in] bitIndex Position of the bit whose value is checked whether it is set. + * + * @return @c true if the bit is set @c false + * + */ + bool IsBitSet(int bitIndex) const; + + /** + * Checks whether the value of calling %_BigIntegerImpl instance is probably prime. + * + * @since 3.0 + * + */ + BigInteger::PrimeNumberResult IsProbablePrimeNumber(void) const; + + /** + * Finds the maximum between value of the calling %_BigIntegerImpl instance with value of specified %_BigIntegerImpl rhs instance + * and returns new %_BigIntegerImpl instance containing maximum value. + * + * @since 3.0 + * + * @param[in] rhs Value to compute the maximum with calling %_BigIntegerImpl instance. + * + * @return A new %_BigIntegerImpl instance containing maximum value. + * + */ + _BigIntegerImpl* Maximum(_BigIntegerImpl* rhs) const; + + /** + * Finds the minimum between value of the calling %_BigIntegerImpl instance with value of specified %_BigIntegerImpl rhs instance + * and returns new %_BigIntegerImpl instance containing minimum value. + * + * @since 3.0 + * + * @param[in] rhs Value to compute the minimum with calling %_BigIntegerImpl instance. + * + * @return A new %_BigIntegerImpl instance containing minimum value. + * + */ + _BigIntegerImpl* Minimum(_BigIntegerImpl* rhs) const; + + /** + * Performs modulus of value of the calling %_BigIntegerImpl instance and value of specified %_BigIntegerImpl instance + * and returns a new %_BigIntegerImpl instance whose value is this mod value. + * + * @since 3.0 + * + * @param[in] value The modulas. + * + * @return A new %_BigIntegerImpl instance containing this mod value. + * + * The modulus value must be positive. + * The result is always positive. + * + */ + _BigIntegerImpl* Modulus(_BigIntegerImpl* value) const; + + /** + * Performs 1 divided by of value of the calling %_BigIntegerImpl instance and modulus with value of specified %_BigIntegerImpl instance + * and returns a new %_BigIntegerImpl instance whose value is 1/this mod value. + * + * @since 3.0 + * + * @param[in] value The modulus. + * + * @return A new %_BigIntegerImpl instance containing 1/this mod value. + * + * The modulas value must be positive. + * The result is always positive. + * + */ + _BigIntegerImpl* ModulusInverse(_BigIntegerImpl* value) const; + + /** + * Calculates power of value of the calling %_BigIntegerImpl instance and exponent specified in value of specified %_BigIntegerImpl instance + * and the result is moded with value specified in the modulo %_BigIntegerImpl instance. + * + * @since 3.0 + * + * @param[in] exponent The exponent. + * @param[in] modulo The modulas. + * + * @return A new %_BigIntegerImpl instance containing pow(this, exponent) mod modulo. + * + */ + _BigIntegerImpl* ModulusPower(_BigIntegerImpl* exponent, _BigIntegerImpl* modulo) const; + + + /** + * Multiplies value of the calling %_BigIntegerImpl instance with value of specified %_BigIntegerImpl rhs instance + * and returns a new %_BigIntegerImpl instance whose value is this * rhs. + * + * @since 3.0 + * + * @param[in] rhs Value to multiply with calling %_BigIntegerImpl instance. + * + * @return A new %_BigIntegerImpl instance. + * + */ + + _BigIntegerImpl* Multiply(_BigIntegerImpl* rhs) const; + + /** + * Negates value of the calling %_BigIntegerImpl instance and returns a new %_BigIntegerImpl instance whose value is -this. + * + * @since 3.0 + * + * @return A new %_BigIntegerImpl instance. + * + */ + _BigIntegerImpl* Negate(void) const; + + /** + * Gets the next probable prime number from the calling %_BigIntegerImpl instance + * and returns a new %_BigIntegerImpl instance which contains next probable prime number value is -this. + * + * @since 3.0 + * + * @return A new %_BigIntegerImpl instance containing next probable prime number. + * + */ + _BigIntegerImpl* NextProbablePrimeNumber(void) const; + + + /** + * Complements, performs logical negation on each bit in the value of calling %_BigIntegerImpl instance + * and returns a new %_BigIntegerImpl instance which contains complement value. + * + * @since 3.0 + * + * @return A new %_BigIntegerImpl instance containing complement value. + * + */ + _BigIntegerImpl* Not(void) const; + + /** + * Performs binary Or of value of the calling %_BigIntegerImpl instance with value of specified %_BigIntegerImpl rhs instance + * and returns a new %_BigIntegerImpl instance whose value is this | rhs. + * + * @since 3.0 + * + * @param[in] rhs Value to Or with calling %_BigIntegerImpl instance. + * + * @return A new %_BigIntegerImpl instance containing this | rhs. + * + */ + _BigIntegerImpl* Or(_BigIntegerImpl* rhs) const; + + /** + * Raises value of the calling %_BigIntegerImpl instance to the power of specified %_BigIntegerImpl rhs instance + * and returns a new %_BigIntegerImpl instance whose value is pow(this, exponent). + * + * @since 3.0 + * + * @param[in] exponent Exponent value. + * + * @return A new %_BigIntegerImpl instance containing pow(this, exponent) value. + * + */ + _BigIntegerImpl* Power(int exponent) const; + + /** + * Divides value of the calling %_BigIntegerImpl instance with value of specified %_BigIntegerImpl rhs instance + * and returns a new %_BigIntegerImpl instance whose value is this % rhs. + * + * @since 3.0 + * + * @param[in] rhs Divisor,value which divides the calling %_BigIntegerImpl instance. + * + * @return A new %_BigIntegerImpl instance containing this % rhs value. + * + */ + _BigIntegerImpl* Remainder(_BigIntegerImpl* divisor) const; + + /** + * Sets bit at a specified position in the calling %_BigIntegerImpl instance and returns a new %_BigIntegerImpl instance. + * + * @since 3.0 + * + * @param[in] bitIndex Position where the bit has to be set. + * + * @return A new %_BigIntegerImpl instance. + * + */ + _BigIntegerImpl* SetBit(int bitIndex) const; + + /** + * Performs Shift left of value of the calling %_BigIntegerImpl instance with specified shift distance + * and returns a new %_BigIntegerImpl instance whose value is this << shiftDistance. + * + * @since 3.0 + * + * @param[in] shiftDistance Shift distance. + * + * @return A new %_BigIntegerImpl instance. + * + */ + _BigIntegerImpl* ShiftLeft(int shiftDistance) const; + + /** + * Performs Shift right of value of the calling %_BigIntegerImpl instance with specified shift distance + * and returns a new %_BigIntegerImpl instance whose value is this >> shiftDistance. + * + * @since 3.0 + * + * @param[in] shiftDistance Shift distance. + * + * @return A new %_BigIntegerImpl instance. + * + */ + _BigIntegerImpl* ShiftRight(int shiftDistance) const; + + /** + * Subtracts value of the calling %_BigIntegerImpl instance with value of specified %_BigIntegerImpl rhs instance + * and returns a new %_BigIntegerImpl instance whose value is this - rhs. + * + * @since 3.0 + * + * @param[in] rhs Value to subtract with calling %_BigIntegerImpl instance. + * + * @return A new %_BigIntegerImpl instance. + * + */ + _BigIntegerImpl* Subtract(_BigIntegerImpl* rhs) const; + + /** + * Gets the @c double equivalent of the current instance of %BigInteger. + * + * @since 3.0 + * + * @return The @c double equivalent of the current instance + * + */ + double ToDouble(void) const; + + /** + * Gets the @c int64_t equivalent of the current instance of %BigInteger. + * + * @since 3.0 + * + * @return The @c int64_t equivalent of the current instance + * + */ + int64_t ToInt64(void) const; + + /** + * Converts value of the calling %_BigIntegerImpl instance to string representation in specified radix. + * + * @since 3.0 + * + * @param[in] radix Radix used for the string representation. + * + * @return String containing the value in specified radix. + * + */ + ImmutableString ToString(int radix) const; + + /** + * Performs binary Xor of value of the calling %_BigIntegerImpl instance with value of specified %_BigIntegerImpl rhs instance + * and returns a new %_BigIntegerImpl instance whose value is this ^ rhs. + * + * @since 3.0 + * + * @param[in] rhs Value to Xor with calling %_BigIntegerImpl instance. + * + * @return A new %_BigIntegerImpl instance containing this ^ rhs value. + * + */ + _BigIntegerImpl* Xor(_BigIntegerImpl* rhs) const; + + +private: + // + // The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects. + // + // @param[in] bigInt An instance of %_BigIntegerImpl class to copy from + // + _BigIntegerImpl(const _BigIntegerImpl& bigInt); + + // + // The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects. + // + // @param[in] list An instance of %_BigIntegerImpl + // + _BigIntegerImpl& operator =(const _BigIntegerImpl& bigInt); + +public: + volatile int __refCount; + +private: + mpz_t __arbInt; +}; +}} +#endif //_FBASE_INTERNAL_BIG_INTEGER_IMPL_H_ -- 2.7.4