mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-10 23:44:24 +00:00
BigInteger: Mutating methods now return self references to facilitate easy concatenating of several mutating operations
This commit is contained in:
parent
fa37d47ebb
commit
d048fdbc82
2 changed files with 31 additions and 16 deletions
|
|
@ -258,7 +258,7 @@ uint32 BigInteger::getBitRangeAsInt (const int startBit, int numBits) const noex
|
|||
return n & (((uint32) 0xffffffff) >> endSpace);
|
||||
}
|
||||
|
||||
void BigInteger::setBitRangeAsInt (const int startBit, int numBits, uint32 valueToSet)
|
||||
BigInteger& BigInteger::setBitRangeAsInt (const int startBit, int numBits, uint32 valueToSet)
|
||||
{
|
||||
if (numBits > 32)
|
||||
{
|
||||
|
|
@ -271,10 +271,12 @@ void BigInteger::setBitRangeAsInt (const int startBit, int numBits, uint32 value
|
|||
setBit (startBit + i, (valueToSet & 1) != 0);
|
||||
valueToSet >>= 1;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void BigInteger::clear() noexcept
|
||||
BigInteger& BigInteger::clear() noexcept
|
||||
{
|
||||
heapAllocation.free();
|
||||
allocatedSize = numPreallocatedInts;
|
||||
|
|
@ -283,9 +285,11 @@ void BigInteger::clear() noexcept
|
|||
|
||||
for (int i = 0; i < numPreallocatedInts; ++i)
|
||||
preallocated[i] = 0;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void BigInteger::setBit (const int bit)
|
||||
BigInteger& BigInteger::setBit (const int bit)
|
||||
{
|
||||
if (bit >= 0)
|
||||
{
|
||||
|
|
@ -297,17 +301,21 @@ void BigInteger::setBit (const int bit)
|
|||
|
||||
getValues() [bitToIndex (bit)] |= bitToMask (bit);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void BigInteger::setBit (const int bit, const bool shouldBeSet)
|
||||
BigInteger& BigInteger::setBit (const int bit, const bool shouldBeSet)
|
||||
{
|
||||
if (shouldBeSet)
|
||||
setBit (bit);
|
||||
else
|
||||
clearBit (bit);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void BigInteger::clearBit (const int bit) noexcept
|
||||
BigInteger& BigInteger::clearBit (const int bit) noexcept
|
||||
{
|
||||
if (bit >= 0 && bit <= highestBit)
|
||||
{
|
||||
|
|
@ -316,20 +324,25 @@ void BigInteger::clearBit (const int bit) noexcept
|
|||
if (bit == highestBit)
|
||||
highestBit = getHighestBit();
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void BigInteger::setRange (int startBit, int numBits, const bool shouldBeSet)
|
||||
BigInteger& BigInteger::setRange (int startBit, int numBits, const bool shouldBeSet)
|
||||
{
|
||||
while (--numBits >= 0)
|
||||
setBit (startBit++, shouldBeSet);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void BigInteger::insertBit (const int bit, const bool shouldBeSet)
|
||||
BigInteger& BigInteger::insertBit (const int bit, const bool shouldBeSet)
|
||||
{
|
||||
if (bit >= 0)
|
||||
shiftBits (1, bit);
|
||||
|
||||
setBit (bit, shouldBeSet);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
|
|
@ -855,7 +868,7 @@ void BigInteger::shiftRight (int bits, const int startBit)
|
|||
}
|
||||
}
|
||||
|
||||
void BigInteger::shiftBits (int bits, const int startBit)
|
||||
BigInteger& BigInteger::shiftBits (int bits, const int startBit)
|
||||
{
|
||||
if (highestBit >= 0)
|
||||
{
|
||||
|
|
@ -864,6 +877,8 @@ void BigInteger::shiftBits (int bits, const int startBit)
|
|||
else if (bits > 0)
|
||||
shiftLeft (bits, startBit);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
|
|
|
|||
|
|
@ -102,16 +102,16 @@ public:
|
|||
|
||||
//==============================================================================
|
||||
/** Resets the value to 0. */
|
||||
void clear() noexcept;
|
||||
BigInteger& clear() noexcept;
|
||||
|
||||
/** Clears a particular bit in the number. */
|
||||
void clearBit (int bitNumber) noexcept;
|
||||
BigInteger& clearBit (int bitNumber) noexcept;
|
||||
|
||||
/** Sets a specified bit to 1. */
|
||||
void setBit (int bitNumber);
|
||||
BigInteger& setBit (int bitNumber);
|
||||
|
||||
/** Sets or clears a specified bit. */
|
||||
void setBit (int bitNumber, bool shouldBeSet);
|
||||
BigInteger& setBit (int bitNumber, bool shouldBeSet);
|
||||
|
||||
/** Sets a range of bits to be either on or off.
|
||||
|
||||
|
|
@ -119,10 +119,10 @@ public:
|
|||
@param numBits the number of bits to change
|
||||
@param shouldBeSet whether to turn these bits on or off
|
||||
*/
|
||||
void setRange (int startBit, int numBits, bool shouldBeSet);
|
||||
BigInteger& setRange (int startBit, int numBits, bool shouldBeSet);
|
||||
|
||||
/** Inserts a bit an a given position, shifting up any bits above it. */
|
||||
void insertBit (int bitNumber, bool shouldBeSet);
|
||||
BigInteger& insertBit (int bitNumber, bool shouldBeSet);
|
||||
|
||||
/** Returns a range of bits as a new BigInteger.
|
||||
|
||||
|
|
@ -145,14 +145,14 @@ public:
|
|||
Copies the given integer onto a range of bits, starting at startBit,
|
||||
and using up to numBits of the available bits.
|
||||
*/
|
||||
void setBitRangeAsInt (int startBit, int numBits, uint32 valueToSet);
|
||||
BigInteger& setBitRangeAsInt (int startBit, int numBits, uint32 valueToSet);
|
||||
|
||||
/** Shifts a section of bits left or right.
|
||||
|
||||
@param howManyBitsLeft how far to move the bits (+ve numbers shift it left, -ve numbers shift it right).
|
||||
@param startBit the first bit to affect - if this is > 0, only bits above that index will be affected.
|
||||
*/
|
||||
void shiftBits (int howManyBitsLeft, int startBit);
|
||||
BigInteger& shiftBits (int howManyBitsLeft, int startBit);
|
||||
|
||||
/** Returns the total number of set bits in the value. */
|
||||
int countNumberOfSetBits() const noexcept;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue