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.

979 lines
23KB

  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. #include "../basics/juce_StandardHeader.h"
  24. BEGIN_JUCE_NAMESPACE
  25. #include "juce_BitArray.h"
  26. #include "juce_MemoryBlock.h"
  27. #include "../basics/juce_Random.h"
  28. //==============================================================================
  29. BitArray::BitArray() throw()
  30. : numValues (4),
  31. highestBit (-1),
  32. negative (false)
  33. {
  34. values = (unsigned int*) juce_calloc (sizeof (unsigned int) * (numValues + 1));
  35. }
  36. BitArray::BitArray (const int value) throw()
  37. : numValues (4),
  38. highestBit (31),
  39. negative (value < 0)
  40. {
  41. values = (unsigned int*) juce_calloc (sizeof (unsigned int) * (numValues + 1));
  42. values[0] = abs (value);
  43. highestBit = getHighestBit();
  44. }
  45. BitArray::BitArray (int64 value) throw()
  46. : numValues (4),
  47. highestBit (63),
  48. negative (value < 0)
  49. {
  50. values = (unsigned int*) juce_calloc (sizeof (unsigned int) * (numValues + 1));
  51. if (value < 0)
  52. value = -value;
  53. values[0] = (unsigned int) value;
  54. values[1] = (unsigned int) (value >> 32);
  55. highestBit = getHighestBit();
  56. }
  57. BitArray::BitArray (const unsigned int value) throw()
  58. : numValues (4),
  59. highestBit (31),
  60. negative (false)
  61. {
  62. values = (unsigned int*) juce_calloc (sizeof (unsigned int) * (numValues + 1));
  63. values[0] = value;
  64. highestBit = getHighestBit();
  65. }
  66. BitArray::BitArray (const BitArray& other) throw()
  67. : numValues (jmax (4, (other.highestBit >> 5) + 1)),
  68. highestBit (other.getHighestBit()),
  69. negative (other.negative)
  70. {
  71. const int bytes = sizeof (unsigned int) * (numValues + 1);
  72. values = (unsigned int*) juce_malloc (bytes);
  73. memcpy (values, other.values, bytes);
  74. }
  75. BitArray::~BitArray() throw()
  76. {
  77. juce_free (values);
  78. }
  79. const BitArray& BitArray::operator= (const BitArray& other) throw()
  80. {
  81. if (this != &other)
  82. {
  83. juce_free (values);
  84. highestBit = other.getHighestBit();
  85. numValues = jmax (4, (highestBit >> 5) + 1);
  86. negative = other.negative;
  87. const int memSize = sizeof (unsigned int) * (numValues + 1);
  88. values = (unsigned int*)juce_malloc (memSize);
  89. memcpy (values, other.values, memSize);
  90. }
  91. return *this;
  92. }
  93. // result == 0 = the same
  94. // result < 0 = this number is smaller
  95. // result > 0 = this number is bigger
  96. int BitArray::compare (const BitArray& other) const throw()
  97. {
  98. if (isNegative() == other.isNegative())
  99. {
  100. const int absComp = compareAbsolute (other);
  101. return isNegative() ? -absComp : absComp;
  102. }
  103. else
  104. {
  105. return isNegative() ? -1 : 1;
  106. }
  107. }
  108. int BitArray::compareAbsolute (const BitArray& other) const throw()
  109. {
  110. const int h1 = getHighestBit();
  111. const int h2 = other.getHighestBit();
  112. if (h1 > h2)
  113. return 1;
  114. else if (h1 < h2)
  115. return -1;
  116. for (int i = (h1 >> 5) + 1; --i >= 0;)
  117. if (values[i] != other.values[i])
  118. return (values[i] > other.values[i]) ? 1 : -1;
  119. return 0;
  120. }
  121. bool BitArray::operator== (const BitArray& other) const throw()
  122. {
  123. return compare (other) == 0;
  124. }
  125. bool BitArray::operator!= (const BitArray& other) const throw()
  126. {
  127. return compare (other) != 0;
  128. }
  129. bool BitArray::operator[] (const int bit) const throw()
  130. {
  131. return bit >= 0 && bit <= highestBit
  132. && ((values [bit >> 5] & (1 << (bit & 31))) != 0);
  133. }
  134. bool BitArray::isEmpty() const throw()
  135. {
  136. return getHighestBit() < 0;
  137. }
  138. void BitArray::clear() throw()
  139. {
  140. if (numValues > 16)
  141. {
  142. juce_free (values);
  143. numValues = 4;
  144. values = (unsigned int*) juce_calloc (sizeof (unsigned int) * (numValues + 1));
  145. }
  146. else
  147. {
  148. zeromem (values, sizeof (unsigned int) * (numValues + 1));
  149. }
  150. highestBit = -1;
  151. negative = false;
  152. }
  153. void BitArray::setBit (const int bit) throw()
  154. {
  155. if (bit >= 0)
  156. {
  157. if (bit > highestBit)
  158. {
  159. ensureSize (bit >> 5);
  160. highestBit = bit;
  161. }
  162. values [bit >> 5] |= (1 << (bit & 31));
  163. }
  164. }
  165. void BitArray::setBit (const int bit,
  166. const bool shouldBeSet) throw()
  167. {
  168. if (shouldBeSet)
  169. setBit (bit);
  170. else
  171. clearBit (bit);
  172. }
  173. void BitArray::clearBit (const int bit) throw()
  174. {
  175. if (bit >= 0 && bit <= highestBit)
  176. values [bit >> 5] &= ~(1 << (bit & 31));
  177. }
  178. void BitArray::setRange (int startBit,
  179. int numBits,
  180. const bool shouldBeSet) throw()
  181. {
  182. while (--numBits >= 0)
  183. setBit (startBit++, shouldBeSet);
  184. }
  185. void BitArray::insertBit (const int bit,
  186. const bool shouldBeSet) throw()
  187. {
  188. if (bit >= 0)
  189. shiftBits (1, bit);
  190. setBit (bit, shouldBeSet);
  191. }
  192. //==============================================================================
  193. void BitArray::andWith (const BitArray& other) throw()
  194. {
  195. // this operation will only work with the absolute values
  196. jassert (isNegative() == other.isNegative());
  197. int n = numValues;
  198. while (n > other.numValues)
  199. values[--n] = 0;
  200. while (--n >= 0)
  201. values[n] &= other.values[n];
  202. if (other.highestBit < highestBit)
  203. highestBit = other.highestBit;
  204. highestBit = getHighestBit();
  205. }
  206. void BitArray::orWith (const BitArray& other) throw()
  207. {
  208. if (other.highestBit < 0)
  209. return;
  210. // this operation will only work with the absolute values
  211. jassert (isNegative() == other.isNegative());
  212. ensureSize (other.highestBit >> 5);
  213. int n = (other.highestBit >> 5) + 1;
  214. while (--n >= 0)
  215. values[n] |= other.values[n];
  216. if (other.highestBit > highestBit)
  217. highestBit = other.highestBit;
  218. highestBit = getHighestBit();
  219. }
  220. void BitArray::xorWith (const BitArray& other) throw()
  221. {
  222. if (other.highestBit < 0)
  223. return;
  224. // this operation will only work with the absolute values
  225. jassert (isNegative() == other.isNegative());
  226. ensureSize (other.highestBit >> 5);
  227. int n = (other.highestBit >> 5) + 1;
  228. while (--n >= 0)
  229. values[n] ^= other.values[n];
  230. if (other.highestBit > highestBit)
  231. highestBit = other.highestBit;
  232. highestBit = getHighestBit();
  233. }
  234. //==============================================================================
  235. void BitArray::add (const BitArray& other) throw()
  236. {
  237. if (other.isNegative())
  238. {
  239. BitArray o (other);
  240. o.negate();
  241. subtract (o);
  242. return;
  243. }
  244. if (isNegative())
  245. {
  246. if (compareAbsolute (other) < 0)
  247. {
  248. BitArray temp (*this);
  249. temp.negate();
  250. *this = other;
  251. subtract (temp);
  252. }
  253. else
  254. {
  255. negate();
  256. subtract (other);
  257. negate();
  258. }
  259. return;
  260. }
  261. if (other.highestBit > highestBit)
  262. highestBit = other.highestBit;
  263. ++highestBit;
  264. const int numInts = (highestBit >> 5) + 1;
  265. ensureSize (numInts);
  266. int64 remainder = 0;
  267. for (int i = 0; i <= numInts; ++i)
  268. {
  269. if (i < numValues)
  270. remainder += values[i];
  271. if (i < other.numValues)
  272. remainder += other.values[i];
  273. values[i] = (unsigned int) remainder;
  274. remainder >>= 32;
  275. }
  276. jassert (remainder == 0);
  277. highestBit = getHighestBit();
  278. }
  279. void BitArray::subtract (const BitArray& other) throw()
  280. {
  281. if (other.isNegative())
  282. {
  283. BitArray o (other);
  284. o.negate();
  285. add (o);
  286. return;
  287. }
  288. if (! isNegative())
  289. {
  290. if (compareAbsolute (other) < 0)
  291. {
  292. BitArray temp (*this);
  293. *this = other;
  294. subtract (temp);
  295. negate();
  296. return;
  297. }
  298. }
  299. else
  300. {
  301. negate();
  302. add (other);
  303. negate();
  304. return;
  305. }
  306. const int numInts = (highestBit >> 5) + 1;
  307. const int maxOtherInts = (other.highestBit >> 5) + 1;
  308. int64 amountToSubtract = 0;
  309. for (int i = 0; i <= numInts; ++i)
  310. {
  311. if (i <= maxOtherInts)
  312. amountToSubtract += (int64)other.values[i];
  313. if (values[i] >= amountToSubtract)
  314. {
  315. values[i] = (unsigned int) (values[i] - amountToSubtract);
  316. amountToSubtract = 0;
  317. }
  318. else
  319. {
  320. const int64 n = ((int64) values[i] + (((int64) 1) << 32)) - amountToSubtract;
  321. values[i] = (unsigned int) n;
  322. amountToSubtract = 1;
  323. }
  324. }
  325. }
  326. void BitArray::multiplyBy (const BitArray& other) throw()
  327. {
  328. BitArray total;
  329. highestBit = getHighestBit();
  330. const bool wasNegative = isNegative();
  331. setNegative (false);
  332. for (int i = 0; i <= highestBit; ++i)
  333. {
  334. if (operator[](i))
  335. {
  336. BitArray n (other);
  337. n.setNegative (false);
  338. n.shiftBits (i);
  339. total.add (n);
  340. }
  341. }
  342. *this = total;
  343. negative = wasNegative ^ other.isNegative();
  344. }
  345. void BitArray::divideBy (const BitArray& divisor, BitArray& remainder) throw()
  346. {
  347. jassert (this != &remainder); // (can't handle passing itself in to get the remainder)
  348. const int divHB = divisor.getHighestBit();
  349. const int ourHB = getHighestBit();
  350. if (divHB < 0 || ourHB < 0)
  351. {
  352. // division by zero
  353. remainder.clear();
  354. clear();
  355. }
  356. else
  357. {
  358. remainder = *this;
  359. remainder.setNegative (false);
  360. const bool wasNegative = isNegative();
  361. clear();
  362. BitArray temp (divisor);
  363. temp.setNegative (false);
  364. int leftShift = ourHB - divHB;
  365. temp.shiftBits (leftShift);
  366. while (leftShift >= 0)
  367. {
  368. if (remainder.compareAbsolute (temp) >= 0)
  369. {
  370. remainder.subtract (temp);
  371. setBit (leftShift);
  372. }
  373. if (--leftShift >= 0)
  374. temp.shiftBits (-1);
  375. }
  376. negative = wasNegative ^ divisor.isNegative();
  377. remainder.setNegative (wasNegative);
  378. }
  379. }
  380. void BitArray::modulo (const BitArray& divisor) throw()
  381. {
  382. BitArray remainder;
  383. divideBy (divisor, remainder);
  384. *this = remainder;
  385. }
  386. static const BitArray simpleGCD (BitArray* m, BitArray* n) throw()
  387. {
  388. while (! m->isEmpty())
  389. {
  390. if (n->compareAbsolute (*m) > 0)
  391. swapVariables (m, n);
  392. m->subtract (*n);
  393. }
  394. return *n;
  395. }
  396. const BitArray BitArray::findGreatestCommonDivisor (BitArray n) const throw()
  397. {
  398. BitArray m (*this);
  399. while (! n.isEmpty())
  400. {
  401. if (abs (m.getHighestBit() - n.getHighestBit()) <= 16)
  402. return simpleGCD (&m, &n);
  403. BitArray temp1 (m), temp2;
  404. temp1.divideBy (n, temp2);
  405. m = n;
  406. n = temp2;
  407. }
  408. return m;
  409. }
  410. void BitArray::exponentModulo (const BitArray& exponent,
  411. const BitArray& modulus) throw()
  412. {
  413. BitArray exp (exponent);
  414. exp.modulo (modulus);
  415. BitArray value (*this);
  416. value.modulo (modulus);
  417. clear();
  418. setBit (0);
  419. while (! exp.isEmpty())
  420. {
  421. if (exp [0])
  422. {
  423. multiplyBy (value);
  424. this->modulo (modulus);
  425. }
  426. value.multiplyBy (value);
  427. value.modulo (modulus);
  428. exp.shiftBits (-1);
  429. }
  430. }
  431. void BitArray::inverseModulo (const BitArray& modulus) throw()
  432. {
  433. const BitArray one (1);
  434. if (modulus == one || modulus.isNegative())
  435. {
  436. clear();
  437. return;
  438. }
  439. if (isNegative() || compareAbsolute (modulus) >= 0)
  440. this->modulo (modulus);
  441. if (*this == one)
  442. return;
  443. if (! (*this)[0])
  444. {
  445. // not invertible
  446. clear();
  447. return;
  448. }
  449. BitArray a1 (modulus);
  450. BitArray a2 (*this);
  451. BitArray b1 (modulus);
  452. BitArray b2 (1);
  453. while (a2 != one)
  454. {
  455. BitArray temp1, temp2, multiplier (a1);
  456. multiplier.divideBy (a2, temp1);
  457. temp1 = a2;
  458. temp1.multiplyBy (multiplier);
  459. temp2 = a1;
  460. temp2.subtract (temp1);
  461. a1 = a2;
  462. a2 = temp2;
  463. temp1 = b2;
  464. temp1.multiplyBy (multiplier);
  465. temp2 = b1;
  466. temp2.subtract (temp1);
  467. b1 = b2;
  468. b2 = temp2;
  469. }
  470. while (b2.isNegative())
  471. b2.add (modulus);
  472. b2.modulo (modulus);
  473. *this = b2;
  474. }
  475. //==============================================================================
  476. void BitArray::shiftBits (int bits, const int startBit) throw()
  477. {
  478. if (highestBit < 0)
  479. return;
  480. if (startBit > 0)
  481. {
  482. if (bits < 0)
  483. {
  484. // right shift
  485. for (int i = startBit; i <= highestBit; ++i)
  486. setBit (i, operator[] (i - bits));
  487. highestBit = getHighestBit();
  488. }
  489. else if (bits > 0)
  490. {
  491. // left shift
  492. for (int i = highestBit + 1; --i >= startBit;)
  493. setBit (i + bits, operator[] (i));
  494. while (--bits >= 0)
  495. clearBit (bits + startBit);
  496. }
  497. }
  498. else
  499. {
  500. if (bits < 0)
  501. {
  502. // right shift
  503. bits = -bits;
  504. if (bits > highestBit)
  505. {
  506. clear();
  507. }
  508. else
  509. {
  510. const int wordsToMove = bits >> 5;
  511. int top = 1 + (highestBit >> 5) - wordsToMove;
  512. highestBit -= bits;
  513. if (wordsToMove > 0)
  514. {
  515. int i;
  516. for (i = 0; i < top; ++i)
  517. values [i] = values [i + wordsToMove];
  518. for (i = 0; i < wordsToMove; ++i)
  519. values [top + i] = 0;
  520. bits &= 31;
  521. }
  522. if (bits != 0)
  523. {
  524. const int invBits = 32 - bits;
  525. --top;
  526. for (int i = 0; i < top; ++i)
  527. values[i] = (values[i] >> bits) | (values [i + 1] << invBits);
  528. values[top] = (values[top] >> bits);
  529. }
  530. highestBit = getHighestBit();
  531. }
  532. }
  533. else if (bits > 0)
  534. {
  535. // left shift
  536. ensureSize (((highestBit + bits) >> 5) + 1);
  537. const int wordsToMove = bits >> 5;
  538. int top = 1 + (highestBit >> 5);
  539. highestBit += bits;
  540. if (wordsToMove > 0)
  541. {
  542. int i;
  543. for (i = top; --i >= 0;)
  544. values [i + wordsToMove] = values [i];
  545. for (i = 0; i < wordsToMove; ++i)
  546. values [i] = 0;
  547. bits &= 31;
  548. }
  549. if (bits != 0)
  550. {
  551. const int invBits = 32 - bits;
  552. for (int i = top + 1 + wordsToMove; --i > wordsToMove;)
  553. values[i] = (values[i] << bits) | (values [i - 1] >> invBits);
  554. values [wordsToMove] = values [wordsToMove] << bits;
  555. }
  556. highestBit = getHighestBit();
  557. }
  558. }
  559. }
  560. int BitArray::getBitRangeAsInt (const int startBit, int numBits) const throw()
  561. {
  562. if (numBits > 32)
  563. {
  564. jassertfalse
  565. numBits = 32;
  566. }
  567. if (startBit == 0)
  568. {
  569. if (numBits < 32)
  570. return values[0] & ((1 << numBits) - 1);
  571. return values[0];
  572. }
  573. int n = 0;
  574. for (int i = numBits; --i >= 0;)
  575. {
  576. n <<= 1;
  577. if (operator[] (startBit + i))
  578. n |= 1;
  579. }
  580. return n;
  581. }
  582. void BitArray::setBitRangeAsInt (const int startBit, int numBits, unsigned int valueToSet) throw()
  583. {
  584. if (numBits > 32)
  585. {
  586. jassertfalse
  587. numBits = 32;
  588. }
  589. for (int i = 0; i < numBits; ++i)
  590. {
  591. setBit (startBit + i, (valueToSet & 1) != 0);
  592. valueToSet >>= 1;
  593. }
  594. }
  595. //==============================================================================
  596. void BitArray::fillBitsRandomly (int startBit, int numBits) throw()
  597. {
  598. highestBit = jmax (highestBit, startBit + numBits);
  599. ensureSize (((startBit + numBits) >> 5) + 1);
  600. while ((startBit & 31) != 0 && numBits > 0)
  601. {
  602. setBit (startBit++, Random::getSystemRandom().nextBool());
  603. --numBits;
  604. }
  605. while (numBits >= 32)
  606. {
  607. values [startBit >> 5] = (unsigned int) Random::getSystemRandom().nextInt();
  608. startBit += 32;
  609. numBits -= 32;
  610. }
  611. while (--numBits >= 0)
  612. {
  613. setBit (startBit + numBits, Random::getSystemRandom().nextBool());
  614. }
  615. highestBit = getHighestBit();
  616. }
  617. void BitArray::createRandomNumber (const BitArray& maximumValue) throw()
  618. {
  619. clear();
  620. do
  621. {
  622. fillBitsRandomly (0, maximumValue.getHighestBit() + 1);
  623. }
  624. while (compare (maximumValue) >= 0);
  625. }
  626. //==============================================================================
  627. bool BitArray::isNegative() const throw()
  628. {
  629. return negative && ! isEmpty();
  630. }
  631. void BitArray::setNegative (const bool neg) throw()
  632. {
  633. negative = neg;
  634. }
  635. void BitArray::negate() throw()
  636. {
  637. negative = (! negative) && ! isEmpty();
  638. }
  639. int BitArray::countNumberOfSetBits() const throw()
  640. {
  641. int total = 0;
  642. for (int i = (highestBit >> 5) + 1; --i >= 0;)
  643. {
  644. unsigned int n = values[i];
  645. if (n == 0xffffffff)
  646. {
  647. total += 32;
  648. }
  649. else
  650. {
  651. while (n != 0)
  652. {
  653. total += (n & 1);
  654. n >>= 1;
  655. }
  656. }
  657. }
  658. return total;
  659. }
  660. int BitArray::getHighestBit() const throw()
  661. {
  662. for (int i = highestBit + 1; --i >= 0;)
  663. if ((values [i >> 5] & (1 << (i & 31))) != 0)
  664. return i;
  665. return -1;
  666. }
  667. int BitArray::findNextSetBit (int i) const throw()
  668. {
  669. for (; i <= highestBit; ++i)
  670. if ((values [i >> 5] & (1 << (i & 31))) != 0)
  671. return i;
  672. return -1;
  673. }
  674. int BitArray::findNextClearBit (int i) const throw()
  675. {
  676. for (; i <= highestBit; ++i)
  677. if ((values [i >> 5] & (1 << (i & 31))) == 0)
  678. break;
  679. return i;
  680. }
  681. void BitArray::ensureSize (const int numVals) throw()
  682. {
  683. if (numVals + 2 >= numValues)
  684. {
  685. int oldSize = numValues;
  686. numValues = ((numVals + 2) * 3) / 2;
  687. values = (unsigned int*) juce_realloc (values, sizeof (unsigned int) * numValues + 4);
  688. while (oldSize < numValues)
  689. values [oldSize++] = 0;
  690. }
  691. }
  692. //==============================================================================
  693. const String BitArray::toString (const int base) const throw()
  694. {
  695. String s;
  696. BitArray v (*this);
  697. if (base == 2 || base == 8 || base == 16)
  698. {
  699. const int bits = (base == 2) ? 1 : (base == 8 ? 3 : 4);
  700. static const tchar* const hexDigits = T("0123456789abcdef");
  701. for (;;)
  702. {
  703. const int remainder = v.getBitRangeAsInt (0, bits);
  704. v.shiftBits (-bits);
  705. if (remainder == 0 && v.isEmpty())
  706. break;
  707. s = String::charToString (hexDigits [remainder]) + s;
  708. }
  709. }
  710. else if (base == 10)
  711. {
  712. const BitArray ten (10);
  713. BitArray remainder;
  714. for (;;)
  715. {
  716. v.divideBy (ten, remainder);
  717. if (remainder.isEmpty() && v.isEmpty())
  718. break;
  719. s = String (remainder.getBitRangeAsInt (0, 8)) + s;
  720. }
  721. }
  722. else
  723. {
  724. jassertfalse // can't do the specified base
  725. return String::empty;
  726. }
  727. if (s.isEmpty())
  728. return T("0");
  729. return isNegative() ? T("-") + s : s;
  730. }
  731. void BitArray::parseString (const String& text,
  732. const int base) throw()
  733. {
  734. clear();
  735. const tchar* t = (const tchar*) text;
  736. if (base == 2 || base == 8 || base == 16)
  737. {
  738. const int bits = (base == 2) ? 1 : (base == 8 ? 3 : 4);
  739. for (;;)
  740. {
  741. const tchar c = *t++;
  742. const int digit = CharacterFunctions::getHexDigitValue (c);
  743. if (((unsigned int) digit) < (unsigned int) base)
  744. {
  745. shiftBits (bits);
  746. add (digit);
  747. }
  748. else if (c == 0)
  749. {
  750. break;
  751. }
  752. }
  753. }
  754. else if (base == 10)
  755. {
  756. const BitArray ten ((unsigned int) 10);
  757. for (;;)
  758. {
  759. const tchar c = *t++;
  760. if (c >= T('0') && c <= T('9'))
  761. {
  762. multiplyBy (ten);
  763. add ((int) (c - T('0')));
  764. }
  765. else if (c == 0)
  766. {
  767. break;
  768. }
  769. }
  770. }
  771. setNegative (text.trimStart().startsWithChar (T('-')));
  772. }
  773. const MemoryBlock BitArray::toMemoryBlock() const throw()
  774. {
  775. const int numBytes = (getHighestBit() + 8) >> 3;
  776. MemoryBlock mb (numBytes);
  777. for (int i = 0; i < numBytes; ++i)
  778. mb[i] = (uint8) getBitRangeAsInt (i << 3, 8);
  779. return mb;
  780. }
  781. void BitArray::loadFromMemoryBlock (const MemoryBlock& data) throw()
  782. {
  783. clear();
  784. for (int i = data.getSize(); --i >= 0;)
  785. this->setBitRangeAsInt (i << 3, 8, data [i]);
  786. }
  787. END_JUCE_NAMESPACE