From d048fdbc82a55069cf34d341823cad5dc6f2fb2f Mon Sep 17 00:00:00 2001 From: hogliux Date: Thu, 6 Oct 2022 14:57:58 +0200 Subject: [PATCH] BigInteger: Mutating methods now return self references to facilitate easy concatenating of several mutating operations --- modules/juce_core/maths/juce_BigInteger.cpp | 31 +++++++++++++++------ modules/juce_core/maths/juce_BigInteger.h | 16 +++++------ 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/modules/juce_core/maths/juce_BigInteger.cpp b/modules/juce_core/maths/juce_BigInteger.cpp index 226ddccc5f..fd780c8876 100644 --- a/modules/juce_core/maths/juce_BigInteger.cpp +++ b/modules/juce_core/maths/juce_BigInteger.cpp @@ -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; } //============================================================================== diff --git a/modules/juce_core/maths/juce_BigInteger.h b/modules/juce_core/maths/juce_BigInteger.h index 4305138195..8d35e5f117 100644 --- a/modules/juce_core/maths/juce_BigInteger.h +++ b/modules/juce_core/maths/juce_BigInteger.h @@ -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;