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.

345 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 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_BITARRAY_JUCEHEADER__
  19. #define __JUCE_BITARRAY_JUCEHEADER__
  20. #include "../text/juce_String.h"
  21. #include "juce_Array.h"
  22. #include "juce_HeapBlock.h"
  23. class MemoryBlock;
  24. //==============================================================================
  25. /**
  26. An array of on/off bits, also usable to store large binary integers.
  27. A BitArray acts like an arbitrarily large integer whose bits can be set or
  28. cleared, and some basic mathematical operations can be done on the number as
  29. a whole.
  30. */
  31. class JUCE_API BitArray
  32. {
  33. public:
  34. //==============================================================================
  35. /** Creates an empty BitArray */
  36. BitArray() throw();
  37. /** Creates a BitArray containing an integer value in its low bits.
  38. The low 32 bits of the array are initialised with this value.
  39. */
  40. BitArray (const unsigned int value) throw();
  41. /** Creates a BitArray containing an integer value in its low bits.
  42. The low 32 bits of the array are initialised with the absolute value
  43. passed in, and its sign is set to reflect the sign of the number.
  44. */
  45. BitArray (const int value) throw();
  46. /** Creates a BitArray containing an integer value in its low bits.
  47. The low 64 bits of the array are initialised with the absolute value
  48. passed in, and its sign is set to reflect the sign of the number.
  49. */
  50. BitArray (int64 value) throw();
  51. /** Creates a copy of another BitArray. */
  52. BitArray (const BitArray& other) throw();
  53. /** Destructor. */
  54. ~BitArray() throw();
  55. //==============================================================================
  56. /** Copies another BitArray onto this one. */
  57. const BitArray& operator= (const BitArray& other) throw();
  58. /** Two arrays are the same if the same bits are set. */
  59. bool operator== (const BitArray& other) const throw();
  60. /** Two arrays are the same if the same bits are set. */
  61. bool operator!= (const BitArray& other) const throw();
  62. //==============================================================================
  63. /** Clears all bits in the BitArray to 0. */
  64. void clear() throw();
  65. /** Clears a particular bit in the array. */
  66. void clearBit (const int bitNumber) throw();
  67. /** Sets a specified bit to 1.
  68. If the bit number is high, this will grow the array to accomodate it.
  69. */
  70. void setBit (const int bitNumber) throw();
  71. /** Sets or clears a specified bit. */
  72. void setBit (const int bitNumber,
  73. const bool shouldBeSet) throw();
  74. /** Sets a range of bits to be either on or off.
  75. @param startBit the first bit to change
  76. @param numBits the number of bits to change
  77. @param shouldBeSet whether to turn these bits on or off
  78. */
  79. void setRange (int startBit,
  80. int numBits,
  81. const bool shouldBeSet) throw();
  82. /** Inserts a bit an a given position, shifting up any bits above it. */
  83. void insertBit (const int bitNumber,
  84. const bool shouldBeSet) throw();
  85. /** Returns the value of a specified bit in the array.
  86. If the index is out-of-range, the result will be false.
  87. */
  88. bool operator[] (const int bit) const throw();
  89. /** Returns true if no bits are set. */
  90. bool isEmpty() const throw();
  91. //==============================================================================
  92. /** Returns a range of bits in the array as a new BitArray.
  93. e.g. getBitRangeAsInt (0, 64) would return the lowest 64 bits.
  94. @see getBitRangeAsInt
  95. */
  96. const BitArray getBitRange (int startBit, int numBits) const throw();
  97. /** Returns a range of bits in the array as an integer value.
  98. e.g. getBitRangeAsInt (0, 32) would return the lowest 32 bits.
  99. Asking for more than 32 bits isn't allowed (obviously) - for that, use
  100. getBitRange().
  101. */
  102. int getBitRangeAsInt (int startBit, int numBits) const throw();
  103. /** Sets a range of bits in the array based on an integer value.
  104. Copies the given integer into the array, starting at startBit,
  105. and only using up to numBits of the available bits.
  106. */
  107. void setBitRangeAsInt (int startBit, int numBits,
  108. unsigned int valueToSet) throw();
  109. //==============================================================================
  110. /** Performs a bitwise OR with another BitArray.
  111. The result ends up in this array.
  112. */
  113. void orWith (const BitArray& other) throw();
  114. /** Performs a bitwise AND with another BitArray.
  115. The result ends up in this array.
  116. */
  117. void andWith (const BitArray& other) throw();
  118. /** Performs a bitwise XOR with another BitArray.
  119. The result ends up in this array.
  120. */
  121. void xorWith (const BitArray& other) throw();
  122. /** Adds another BitArray's value to this one.
  123. Treating the two arrays as large positive integers, this
  124. adds them up and puts the result in this array.
  125. */
  126. void add (const BitArray& other) throw();
  127. /** Subtracts another BitArray's value from this one.
  128. Treating the two arrays as large positive integers, this
  129. subtracts them and puts the result in this array.
  130. Note that if the result should be negative, this won't be
  131. handled correctly.
  132. */
  133. void subtract (const BitArray& other) throw();
  134. /** Multiplies another BitArray's value with this one.
  135. Treating the two arrays as large positive integers, this
  136. multiplies them and puts the result in this array.
  137. */
  138. void multiplyBy (const BitArray& other) throw();
  139. /** Divides another BitArray's value into this one and also produces a remainder.
  140. Treating the two arrays as large positive integers, this
  141. divides this value by the other, leaving the quotient in this
  142. array, and the remainder is copied into the other BitArray passed in.
  143. */
  144. void divideBy (const BitArray& divisor, BitArray& remainder) throw();
  145. /** Returns the largest value that will divide both this value and the one
  146. passed-in.
  147. */
  148. const BitArray findGreatestCommonDivisor (BitArray other) const throw();
  149. /** Performs a modulo operation on this value.
  150. The result is stored in this value.
  151. */
  152. void modulo (const BitArray& divisor) throw();
  153. /** Performs a combined exponent and modulo operation.
  154. This BitArray's value becomes (this ^ exponent) % modulus.
  155. */
  156. void exponentModulo (const BitArray& exponent, const BitArray& modulus) throw();
  157. /** Performs an inverse modulo on the value.
  158. i.e. the result is (this ^ -1) mod (modulus).
  159. */
  160. void inverseModulo (const BitArray& modulus) throw();
  161. /** Shifts a section of bits left or right.
  162. @param howManyBitsLeft how far to move the bits (+ve numbers shift it left, -ve numbers shift it right).
  163. @param startBit the first bit to affect - if this is > 0, only bits above that index will be affected.
  164. */
  165. void shiftBits (int howManyBitsLeft,
  166. int startBit = 0) throw();
  167. /** Does a signed comparison of two BitArrays.
  168. Return values are:
  169. - 0 if the numbers are the same
  170. - < 0 if this number is smaller than the other
  171. - > 0 if this number is bigger than the other
  172. */
  173. int compare (const BitArray& other) const throw();
  174. /** Compares the magnitudes of two BitArrays, ignoring their signs.
  175. Return values are:
  176. - 0 if the numbers are the same
  177. - < 0 if this number is smaller than the other
  178. - > 0 if this number is bigger than the other
  179. */
  180. int compareAbsolute (const BitArray& other) const throw();
  181. //==============================================================================
  182. /** Returns true if the value is less than zero.
  183. @see setNegative, negate
  184. */
  185. bool isNegative() const throw();
  186. /** Changes the sign of the number to be positive or negative.
  187. @see isNegative, negate
  188. */
  189. void setNegative (const bool shouldBeNegative) throw();
  190. /** Inverts the sign of the number.
  191. @see isNegative, setNegative
  192. */
  193. void negate() throw();
  194. //==============================================================================
  195. /** Counts the total number of set bits in the array. */
  196. int countNumberOfSetBits() const throw();
  197. /** Looks for the index of the next set bit after a given starting point.
  198. searches from startIndex (inclusive) upwards for the first set bit,
  199. and returns its index.
  200. If no set bits are found, it returns -1.
  201. */
  202. int findNextSetBit (int startIndex = 0) const throw();
  203. /** Looks for the index of the next clear bit after a given starting point.
  204. searches from startIndex (inclusive) upwards for the first clear bit,
  205. and returns its index.
  206. */
  207. int findNextClearBit (int startIndex = 0) const throw();
  208. /** Returns the index of the highest set bit in the array.
  209. If the array is empty, this will return -1.
  210. */
  211. int getHighestBit() const throw();
  212. //==============================================================================
  213. /** Converts the array to a number string.
  214. Specify a base such as 2 (binary), 8 (octal), 10 (decimal), 16 (hex).
  215. If minuimumNumCharacters is greater than 0, the returned string will be
  216. padded with leading zeros to reach at least that length.
  217. */
  218. const String toString (const int base, const int minimumNumCharacters = 1) const throw();
  219. /** Converts a number string to an array.
  220. Any non-valid characters will be ignored.
  221. Specify a base such as 2 (binary), 8 (octal), 10 (decimal), 16 (hex).
  222. */
  223. void parseString (const String& text,
  224. const int base) throw();
  225. //==============================================================================
  226. /** Turns the array into a block of binary data.
  227. The data is arranged as little-endian, so the first byte of data is the low 8 bits
  228. of the array, and so on.
  229. @see loadFromMemoryBlock
  230. */
  231. const MemoryBlock toMemoryBlock() const throw();
  232. /** Copies a block of raw data onto this array.
  233. The data is arranged as little-endian, so the first byte of data is the low 8 bits
  234. of the array, and so on.
  235. @see toMemoryBlock
  236. */
  237. void loadFromMemoryBlock (const MemoryBlock& data) throw();
  238. //==============================================================================
  239. juce_UseDebuggingNewOperator
  240. private:
  241. void ensureSize (const int numVals) throw();
  242. HeapBlock <unsigned int> values;
  243. int numValues, highestBit;
  244. bool negative;
  245. };
  246. #endif // __JUCE_BITARRAY_JUCEHEADER__