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.

946 lines
22KB

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