The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

337 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_BIGINTEGER_JUCEHEADER__
  19. #define __JUCE_BIGINTEGER_JUCEHEADER__
  20. #include "../text/juce_String.h"
  21. #include "../memory/juce_HeapBlock.h"
  22. class MemoryBlock;
  23. //==============================================================================
  24. /**
  25. An arbitrarily large integer class.
  26. A BigInteger can be used in a similar way to a normal integer, but has no size
  27. limit (except for memory and performance constraints).
  28. Negative values are possible, but the value isn't stored as 2s-complement, so
  29. be careful if you use negative values and look at the values of individual bits.
  30. */
  31. class JUCE_API BigInteger
  32. {
  33. public:
  34. //==============================================================================
  35. /** Creates an empty BigInteger */
  36. BigInteger();
  37. /** Creates a BigInteger containing an integer value in its low bits.
  38. The low 32 bits of the number are initialised with this value.
  39. */
  40. BigInteger (uint32 value);
  41. /** Creates a BigInteger containing an integer value in its low bits.
  42. The low 32 bits of the number are initialised with the absolute value
  43. passed in, and its sign is set to reflect the sign of the number.
  44. */
  45. BigInteger (int32 value);
  46. /** Creates a BigInteger containing an integer value in its low bits.
  47. The low 64 bits of the number are initialised with the absolute value
  48. passed in, and its sign is set to reflect the sign of the number.
  49. */
  50. BigInteger (int64 value);
  51. /** Creates a copy of another BigInteger. */
  52. BigInteger (const BigInteger& other);
  53. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  54. BigInteger (BigInteger&& other) noexcept;
  55. BigInteger& operator= (BigInteger&& other) noexcept;
  56. #endif
  57. /** Destructor. */
  58. ~BigInteger();
  59. //==============================================================================
  60. /** Copies another BigInteger onto this one. */
  61. BigInteger& operator= (const BigInteger& other);
  62. /** Swaps the internal contents of this with another object. */
  63. void swapWith (BigInteger& other) noexcept;
  64. //==============================================================================
  65. /** Returns the value of a specified bit in the number.
  66. If the index is out-of-range, the result will be false.
  67. */
  68. bool operator[] (int bit) const noexcept;
  69. /** Returns true if no bits are set. */
  70. bool isZero() const noexcept;
  71. /** Returns true if the value is 1. */
  72. bool isOne() const noexcept;
  73. /** Attempts to get the lowest bits of the value as an integer.
  74. If the value is bigger than the integer limits, this will return only the lower bits.
  75. */
  76. int toInteger() const noexcept;
  77. //==============================================================================
  78. /** Resets the value to 0. */
  79. void clear();
  80. /** Clears a particular bit in the number. */
  81. void clearBit (int bitNumber) noexcept;
  82. /** Sets a specified bit to 1. */
  83. void setBit (int bitNumber);
  84. /** Sets or clears a specified bit. */
  85. void setBit (int bitNumber, bool shouldBeSet);
  86. /** Sets a range of bits to be either on or off.
  87. @param startBit the first bit to change
  88. @param numBits the number of bits to change
  89. @param shouldBeSet whether to turn these bits on or off
  90. */
  91. void setRange (int startBit, int numBits, bool shouldBeSet);
  92. /** Inserts a bit an a given position, shifting up any bits above it. */
  93. void insertBit (int bitNumber, bool shouldBeSet);
  94. /** Returns a range of bits as a new BigInteger.
  95. e.g. getBitRangeAsInt (0, 64) would return the lowest 64 bits.
  96. @see getBitRangeAsInt
  97. */
  98. BigInteger getBitRange (int startBit, int numBits) const;
  99. /** Returns a range of bits as an integer value.
  100. e.g. getBitRangeAsInt (0, 32) would return the lowest 32 bits.
  101. Asking for more than 32 bits isn't allowed (obviously) - for that, use
  102. getBitRange().
  103. */
  104. uint32 getBitRangeAsInt (int startBit, int numBits) const noexcept;
  105. /** Sets a range of bits to an integer value.
  106. Copies the given integer onto a range of bits, starting at startBit,
  107. and using up to numBits of the available bits.
  108. */
  109. void setBitRangeAsInt (int startBit, int numBits, uint32 valueToSet);
  110. /** Shifts a section of bits left or right.
  111. @param howManyBitsLeft how far to move the bits (+ve numbers shift it left, -ve numbers shift it right).
  112. @param startBit the first bit to affect - if this is > 0, only bits above that index will be affected.
  113. */
  114. void shiftBits (int howManyBitsLeft, int startBit);
  115. /** Returns the total number of set bits in the value. */
  116. int countNumberOfSetBits() const noexcept;
  117. /** Looks for the index of the next set bit after a given starting point.
  118. This searches from startIndex (inclusive) upwards for the first set bit,
  119. and returns its index. If no set bits are found, it returns -1.
  120. */
  121. int findNextSetBit (int startIndex = 0) const noexcept;
  122. /** Looks for the index of the next clear bit after a given starting point.
  123. This searches from startIndex (inclusive) upwards for the first clear bit,
  124. and returns its index.
  125. */
  126. int findNextClearBit (int startIndex = 0) const noexcept;
  127. /** Returns the index of the highest set bit in the number.
  128. If the value is zero, this will return -1.
  129. */
  130. int getHighestBit() const noexcept;
  131. //==============================================================================
  132. // All the standard arithmetic ops...
  133. BigInteger& operator+= (const BigInteger& other);
  134. BigInteger& operator-= (const BigInteger& other);
  135. BigInteger& operator*= (const BigInteger& other);
  136. BigInteger& operator/= (const BigInteger& other);
  137. BigInteger& operator|= (const BigInteger& other);
  138. BigInteger& operator&= (const BigInteger& other);
  139. BigInteger& operator^= (const BigInteger& other);
  140. BigInteger& operator%= (const BigInteger& other);
  141. BigInteger& operator<<= (int numBitsToShift);
  142. BigInteger& operator>>= (int numBitsToShift);
  143. BigInteger& operator++();
  144. BigInteger& operator--();
  145. BigInteger operator++ (int);
  146. BigInteger operator-- (int);
  147. BigInteger operator-() const;
  148. BigInteger operator+ (const BigInteger& other) const;
  149. BigInteger operator- (const BigInteger& other) const;
  150. BigInteger operator* (const BigInteger& other) const;
  151. BigInteger operator/ (const BigInteger& other) const;
  152. BigInteger operator| (const BigInteger& other) const;
  153. BigInteger operator& (const BigInteger& other) const;
  154. BigInteger operator^ (const BigInteger& other) const;
  155. BigInteger operator% (const BigInteger& other) const;
  156. BigInteger operator<< (int numBitsToShift) const;
  157. BigInteger operator>> (int numBitsToShift) const;
  158. bool operator== (const BigInteger& other) const noexcept;
  159. bool operator!= (const BigInteger& other) const noexcept;
  160. bool operator< (const BigInteger& other) const noexcept;
  161. bool operator<= (const BigInteger& other) const noexcept;
  162. bool operator> (const BigInteger& other) const noexcept;
  163. bool operator>= (const BigInteger& other) const noexcept;
  164. //==============================================================================
  165. /** Does a signed comparison of two BigIntegers.
  166. Return values are:
  167. - 0 if the numbers are the same
  168. - < 0 if this number is smaller than the other
  169. - > 0 if this number is bigger than the other
  170. */
  171. int compare (const BigInteger& other) const noexcept;
  172. /** Compares the magnitudes of two BigIntegers, ignoring their signs.
  173. Return values are:
  174. - 0 if the numbers are the same
  175. - < 0 if this number is smaller than the other
  176. - > 0 if this number is bigger than the other
  177. */
  178. int compareAbsolute (const BigInteger& other) const noexcept;
  179. /** Divides this value by another one and returns the remainder.
  180. This number is divided by other, leaving the quotient in this number,
  181. with the remainder being copied to the other BigInteger passed in.
  182. */
  183. void divideBy (const BigInteger& divisor, BigInteger& remainder);
  184. /** Returns the largest value that will divide both this value and the one passed-in.
  185. */
  186. BigInteger findGreatestCommonDivisor (BigInteger other) const;
  187. /** Performs a combined exponent and modulo operation.
  188. This BigInteger's value becomes (this ^ exponent) % modulus.
  189. */
  190. void exponentModulo (const BigInteger& exponent, const BigInteger& modulus);
  191. /** Performs an inverse modulo on the value.
  192. i.e. the result is (this ^ -1) mod (modulus).
  193. */
  194. void inverseModulo (const BigInteger& modulus);
  195. //==============================================================================
  196. /** Returns true if the value is less than zero.
  197. @see setNegative, negate
  198. */
  199. bool isNegative() const noexcept;
  200. /** Changes the sign of the number to be positive or negative.
  201. @see isNegative, negate
  202. */
  203. void setNegative (bool shouldBeNegative) noexcept;
  204. /** Inverts the sign of the number.
  205. @see isNegative, setNegative
  206. */
  207. void negate() noexcept;
  208. //==============================================================================
  209. /** Converts the number to a string.
  210. Specify a base such as 2 (binary), 8 (octal), 10 (decimal), 16 (hex).
  211. If minimumNumCharacters is greater than 0, the returned string will be
  212. padded with leading zeros to reach at least that length.
  213. */
  214. String toString (int base, int minimumNumCharacters = 1) const;
  215. /** Reads the numeric value from a string.
  216. Specify a base such as 2 (binary), 8 (octal), 10 (decimal), 16 (hex).
  217. Any invalid characters will be ignored.
  218. */
  219. void parseString (const String& text, int base);
  220. //==============================================================================
  221. /** Turns the number into a block of binary data.
  222. The data is arranged as little-endian, so the first byte of data is the low 8 bits
  223. of the number, and so on.
  224. @see loadFromMemoryBlock
  225. */
  226. MemoryBlock toMemoryBlock() const;
  227. /** Converts a block of raw data into a number.
  228. The data is arranged as little-endian, so the first byte of data is the low 8 bits
  229. of the number, and so on.
  230. @see toMemoryBlock
  231. */
  232. void loadFromMemoryBlock (const MemoryBlock& data);
  233. private:
  234. //==============================================================================
  235. HeapBlock <uint32> values;
  236. size_t numValues;
  237. int highestBit;
  238. bool negative;
  239. void ensureSize (size_t numVals);
  240. void shiftLeft (int bits, int startBit);
  241. void shiftRight (int bits, int startBit);
  242. JUCE_LEAK_DETECTOR (BigInteger);
  243. };
  244. /** Writes a BigInteger to an OutputStream as a UTF8 decimal string. */
  245. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const BigInteger& value);
  246. //==============================================================================
  247. #ifndef DOXYGEN
  248. // For backwards compatibility, BitArray is defined as an alias for BigInteger.
  249. typedef BigInteger BitArray;
  250. #endif
  251. #endif // __JUCE_BIGINTEGER_JUCEHEADER__