Loading...
Searching...
No Matches
juce_BigInteger.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
class JUCE_API BigInteger
{
public:
//==============================================================================
BigInteger();
BigInteger (uint32 value);
BigInteger (int32 value);
BigInteger (int64 value);
BigInteger (const BigInteger&);
BigInteger (BigInteger&&) noexcept;
BigInteger& operator= (BigInteger&&) noexcept;
~BigInteger();
//==============================================================================
BigInteger& operator= (const BigInteger&);
void swapWith (BigInteger&) noexcept;
//==============================================================================
bool operator[] (int bit) const noexcept;
bool isZero() const noexcept;
bool isOne() const noexcept;
int toInteger() const noexcept;
int64 toInt64() const noexcept;
//==============================================================================
BigInteger& clear() noexcept;
BigInteger& clearBit (int bitNumber) noexcept;
BigInteger& setBit (int bitNumber);
BigInteger& setBit (int bitNumber, bool shouldBeSet);
BigInteger& setRange (int startBit, int numBits, bool shouldBeSet);
BigInteger& insertBit (int bitNumber, bool shouldBeSet);
BigInteger getBitRange (int startBit, int numBits) const;
uint32 getBitRangeAsInt (int startBit, int numBits) const noexcept;
BigInteger& setBitRangeAsInt (int startBit, int numBits, uint32 valueToSet);
BigInteger& shiftBits (int howManyBitsLeft, int startBit);
int countNumberOfSetBits() const noexcept;
int findNextSetBit (int startIndex) const noexcept;
int findNextClearBit (int startIndex) const noexcept;
int getHighestBit() const noexcept;
//==============================================================================
bool isNegative() const noexcept;
void setNegative (bool shouldBeNegative) noexcept;
void negate() noexcept;
//==============================================================================
// All the standard arithmetic ops...
BigInteger& operator+= (const BigInteger&);
BigInteger& operator-= (const BigInteger&);
BigInteger& operator*= (const BigInteger&);
BigInteger& operator/= (const BigInteger&);
BigInteger& operator|= (const BigInteger&);
BigInteger& operator&= (const BigInteger&);
BigInteger& operator^= (const BigInteger&);
BigInteger& operator%= (const BigInteger&);
BigInteger& operator<<= (int numBitsToShift);
BigInteger& operator>>= (int numBitsToShift);
BigInteger& operator++();
BigInteger& operator--();
BigInteger operator++ (int);
BigInteger operator-- (int);
BigInteger operator-() const;
BigInteger operator+ (const BigInteger&) const;
BigInteger operator- (const BigInteger&) const;
BigInteger operator* (const BigInteger&) const;
BigInteger operator/ (const BigInteger&) const;
BigInteger operator| (const BigInteger&) const;
BigInteger operator& (const BigInteger&) const;
BigInteger operator^ (const BigInteger&) const;
BigInteger operator% (const BigInteger&) const;
BigInteger operator<< (int numBitsToShift) const;
BigInteger operator>> (int numBitsToShift) const;
bool operator== (const BigInteger&) const noexcept;
bool operator!= (const BigInteger&) const noexcept;
bool operator< (const BigInteger&) const noexcept;
bool operator<= (const BigInteger&) const noexcept;
bool operator> (const BigInteger&) const noexcept;
bool operator>= (const BigInteger&) const noexcept;
//==============================================================================
int compare (const BigInteger& other) const noexcept;
int compareAbsolute (const BigInteger& other) const noexcept;
//==============================================================================
void divideBy (const BigInteger& divisor, BigInteger& remainder);
BigInteger findGreatestCommonDivisor (BigInteger other) const;
void exponentModulo (const BigInteger& exponent, const BigInteger& modulus);
void inverseModulo (const BigInteger& modulus);
void montgomeryMultiplication (const BigInteger& other, const BigInteger& modulus,
const BigInteger& modulusp, int k);
void extendedEuclidean (const BigInteger& a, const BigInteger& b,
BigInteger& xOut, BigInteger& yOut);
//==============================================================================
String toString (int base, int minimumNumCharacters = 1) const;
void parseString (StringRef text, int base);
//==============================================================================
MemoryBlock toMemoryBlock() const;
void loadFromMemoryBlock (const MemoryBlock& data);
private:
//==============================================================================
enum { numPreallocatedInts = 4 };
HeapBlock<uint32> heapAllocation;
uint32 preallocated[numPreallocatedInts];
size_t allocatedSize;
int highestBit = -1;
bool negative = false;
uint32* getValues() const noexcept;
uint32* ensureSize (size_t);
void shiftLeft (int bits, int startBit);
void shiftRight (int bits, int startBit);
JUCE_LEAK_DETECTOR (BigInteger)
};
OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const BigInteger& value);
} // namespace juce
juce::BigInteger
Definition juce_BigInteger.h:39
juce::BigInteger::clear
BigInteger & clear() noexcept
Definition juce_BigInteger.cpp:281
juce::BigInteger::toMemoryBlock
MemoryBlock toMemoryBlock() const
Definition juce_BigInteger.cpp:1193
juce::BigInteger::isOne
bool isOne() const noexcept
Definition juce_BigInteger.cpp:356
juce::BigInteger::getBitRange
BigInteger getBitRange(int startBit, int numBits) const
Definition juce_BigInteger.cpp:219
juce::BigInteger::parseString
void parseString(StringRef text, int base)
Definition juce_BigInteger.cpp:1145
juce::BigInteger::toInt64
int64 toInt64() const noexcept
Definition juce_BigInteger.cpp:212
juce::BigInteger::exponentModulo
void exponentModulo(const BigInteger &exponent, const BigInteger &modulus)
Definition juce_BigInteger.cpp:919
juce::BigInteger::setNegative
void setNegative(bool shouldBeNegative) noexcept
Definition juce_BigInteger.cpp:366
juce::BigInteger::~BigInteger
~BigInteger()
juce::BigInteger::getBitRangeAsInt
uint32 getBitRangeAsInt(int startBit, int numBits) const noexcept
Definition juce_BigInteger.cpp:237
juce::BigInteger::setRange
BigInteger & setRange(int startBit, int numBits, bool shouldBeSet)
Definition juce_BigInteger.cpp:333
juce::BigInteger::findGreatestCommonDivisor
BigInteger findGreatestCommonDivisor(BigInteger other) const
Definition juce_BigInteger.cpp:900
juce::BigInteger::extendedEuclidean
void extendedEuclidean(const BigInteger &a, const BigInteger &b, BigInteger &xOut, BigInteger &yOut)
Definition juce_BigInteger.cpp:1005
juce::BigInteger::getHighestBit
int getHighestBit() const noexcept
Definition juce_BigInteger.cpp:391
juce::BigInteger::divideBy
void divideBy(const BigInteger &divisor, BigInteger &remainder)
Definition juce_BigInteger.cpp:578
juce::BigInteger::toString
String toString(int base, int minimumNumCharacters=1) const
Definition juce_BigInteger.cpp:1098
juce::BigInteger::shiftBits
BigInteger & shiftBits(int howManyBitsLeft, int startBit)
Definition juce_BigInteger.cpp:873
juce::BigInteger::findNextClearBit
int findNextClearBit(int startIndex) const noexcept
Definition juce_BigInteger.cpp:413
juce::BigInteger::compare
int compare(const BigInteger &other) const noexcept
Definition juce_BigInteger.cpp:743
juce::BigInteger::insertBit
BigInteger & insertBit(int bitNumber, bool shouldBeSet)
Definition juce_BigInteger.cpp:341
juce::BigInteger::setBitRangeAsInt
BigInteger & setBitRangeAsInt(int startBit, int numBits, uint32 valueToSet)
Definition juce_BigInteger.cpp:263
juce::BigInteger::clearBit
BigInteger & clearBit(int bitNumber) noexcept
Definition juce_BigInteger.cpp:320
juce::BigInteger::BigInteger
BigInteger()
Definition juce_BigInteger.cpp:54
juce::BigInteger::negate
void negate() noexcept
Definition juce_BigInteger.cpp:371
juce::BigInteger::findNextSetBit
int findNextSetBit(int startIndex) const noexcept
Definition juce_BigInteger.cpp:402
juce::BigInteger::loadFromMemoryBlock
void loadFromMemoryBlock(const MemoryBlock &data)
Definition juce_BigInteger.cpp:1205
juce::BigInteger::isZero
bool isZero() const noexcept
Definition juce_BigInteger.cpp:351
juce::BigInteger::countNumberOfSetBits
int countNumberOfSetBits() const noexcept
Definition juce_BigInteger.cpp:380
juce::BigInteger::swapWith
void swapWith(BigInteger &) noexcept
Definition juce_BigInteger.cpp:133
juce::BigInteger::montgomeryMultiplication
void montgomeryMultiplication(const BigInteger &other, const BigInteger &modulus, const BigInteger &modulusp, int k)
Definition juce_BigInteger.cpp:985
juce::BigInteger::isNegative
bool isNegative() const noexcept
Definition juce_BigInteger.cpp:361
juce::BigInteger::compareAbsolute
int compareAbsolute(const BigInteger &other) const noexcept
Definition juce_BigInteger.cpp:756
juce::BigInteger::setBit
BigInteger & setBit(int bitNumber)
Definition juce_BigInteger.cpp:294
juce::BigInteger::toInteger
int toInteger() const noexcept
Definition juce_BigInteger.cpp:206
juce::BigInteger::inverseModulo
void inverseModulo(const BigInteger &modulus)
Definition juce_BigInteger.cpp:1042
juce::HeapBlock
Definition juce_HeapBlock.h:87
juce::MemoryBlock
Definition juce_MemoryBlock.h:33
juce::OutputStream
Definition juce_OutputStream.h:38
juce::StringRef
Definition juce_StringRef.h:62
juce::String
Definition juce_String.h:53