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.

350 lines
12KB

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