Audio plugin host https://kx.studio/carla
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.

859 lines
30KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_REFERENCECOUNTEDARRAY_H_INCLUDED
  24. #define JUCE_REFERENCECOUNTEDARRAY_H_INCLUDED
  25. namespace water {
  26. //==============================================================================
  27. /**
  28. Holds a list of objects derived from ReferenceCountedObject, or which implement basic
  29. reference-count handling methods.
  30. The template parameter specifies the class of the object you want to point to - the easiest
  31. way to make a class reference-countable is to simply make it inherit from ReferenceCountedObject
  32. or SingleThreadedReferenceCountedObject, but if you need to, you can roll your own reference-countable
  33. class by implementing a set of methods called incReferenceCount(), decReferenceCount(), and
  34. decReferenceCountWithoutDeleting(). See ReferenceCountedObject for examples of how these methods
  35. should behave.
  36. A ReferenceCountedArray holds objects derived from ReferenceCountedObject,
  37. and takes care of incrementing and decrementing their ref counts when they
  38. are added and removed from the array.
  39. To make all the array's methods thread-safe, pass in "CriticalSection" as the templated
  40. TypeOfCriticalSectionToUse parameter, instead of the default DummyCriticalSection.
  41. @see Array, OwnedArray, StringArray
  42. */
  43. template <class ObjectClass>
  44. class ReferenceCountedArray
  45. {
  46. public:
  47. typedef ReferenceCountedObjectPtr<ObjectClass> ObjectClassPtr;
  48. //==============================================================================
  49. /** Creates an empty array.
  50. @see ReferenceCountedObject, Array, OwnedArray
  51. */
  52. ReferenceCountedArray() noexcept
  53. : numUsed (0)
  54. {
  55. }
  56. /** Creates a copy of another array */
  57. ReferenceCountedArray (const ReferenceCountedArray& other) noexcept
  58. {
  59. numUsed = other.size();
  60. data.setAllocatedSize (numUsed);
  61. memcpy (data.elements, other.getRawDataPointer(), (size_t) numUsed * sizeof (ObjectClass*));
  62. for (int i = numUsed; --i >= 0;)
  63. if (ObjectClass* o = data.elements[i])
  64. o->incReferenceCount();
  65. }
  66. /** Creates a copy of another array */
  67. template <class OtherObjectClass>
  68. ReferenceCountedArray (const ReferenceCountedArray<OtherObjectClass>& other) noexcept
  69. {
  70. numUsed = other.size();
  71. data.setAllocatedSize (numUsed);
  72. memcpy (data.elements, other.getRawDataPointer(), numUsed * sizeof (ObjectClass*));
  73. for (int i = numUsed; --i >= 0;)
  74. if (ObjectClass* o = data.elements[i])
  75. o->incReferenceCount();
  76. }
  77. /** Copies another array into this one.
  78. Any existing objects in this array will first be released.
  79. */
  80. ReferenceCountedArray& operator= (const ReferenceCountedArray& other) noexcept
  81. {
  82. ReferenceCountedArray otherCopy (other);
  83. swapWith (otherCopy);
  84. return *this;
  85. }
  86. /** Copies another array into this one.
  87. Any existing objects in this array will first be released.
  88. */
  89. template <class OtherObjectClass>
  90. ReferenceCountedArray<ObjectClass>& operator= (const ReferenceCountedArray<OtherObjectClass>& other) noexcept
  91. {
  92. ReferenceCountedArray<ObjectClass> otherCopy (other);
  93. swapWith (otherCopy);
  94. return *this;
  95. }
  96. /** Destructor.
  97. Any objects in the array will be released, and may be deleted if not referenced from elsewhere.
  98. */
  99. ~ReferenceCountedArray()
  100. {
  101. releaseAllObjects();
  102. }
  103. //==============================================================================
  104. /** Removes all objects from the array.
  105. Any objects in the array that whose reference counts drop to zero will be deleted.
  106. */
  107. void clear()
  108. {
  109. releaseAllObjects();
  110. data.setAllocatedSize (0);
  111. }
  112. /** Removes all objects from the array without freeing the array's allocated storage.
  113. Any objects in the array that whose reference counts drop to zero will be deleted.
  114. @see clear
  115. */
  116. void clearQuick()
  117. {
  118. releaseAllObjects();
  119. }
  120. /** Returns the current number of objects in the array. */
  121. inline int size() const noexcept
  122. {
  123. return numUsed;
  124. }
  125. /** Returns true if the array is empty, false otherwise. */
  126. inline bool isEmpty() const noexcept
  127. {
  128. return size() == 0;
  129. }
  130. /** Returns a pointer to the object at this index in the array.
  131. If the index is out-of-range, this will return a null pointer, (and
  132. it could be null anyway, because it's ok for the array to hold null
  133. pointers as well as objects).
  134. @see getUnchecked
  135. */
  136. inline ObjectClassPtr operator[] (const int index) const noexcept
  137. {
  138. return getObjectPointer (index);
  139. }
  140. /** Returns a pointer to the object at this index in the array, without checking
  141. whether the index is in-range.
  142. This is a faster and less safe version of operator[] which doesn't check the index passed in, so
  143. it can be used when you're sure the index is always going to be legal.
  144. */
  145. inline ObjectClassPtr getUnchecked (const int index) const noexcept
  146. {
  147. return getObjectPointerUnchecked (index);
  148. }
  149. /** Returns a raw pointer to the object at this index in the array.
  150. If the index is out-of-range, this will return a null pointer, (and
  151. it could be null anyway, because it's ok for the array to hold null
  152. pointers as well as objects).
  153. @see getUnchecked
  154. */
  155. inline ObjectClass* getObjectPointer (const int index) const noexcept
  156. {
  157. if (isPositiveAndBelow (index, numUsed))
  158. {
  159. jassert (data.elements != nullptr);
  160. return data.elements [index];
  161. }
  162. return ObjectClassPtr();
  163. }
  164. /** Returns a raw pointer to the object at this index in the array, without checking
  165. whether the index is in-range.
  166. */
  167. inline ObjectClass* getObjectPointerUnchecked (const int index) const noexcept
  168. {
  169. jassert (isPositiveAndBelow (index, numUsed) && data.elements != nullptr);
  170. return data.elements [index];
  171. }
  172. /** Returns a pointer to the first object in the array.
  173. This will return a null pointer if the array's empty.
  174. @see getLast
  175. */
  176. inline ObjectClassPtr getFirst() const noexcept
  177. {
  178. if (numUsed > 0)
  179. {
  180. jassert (data.elements != nullptr);
  181. return data.elements [0];
  182. }
  183. return ObjectClassPtr();
  184. }
  185. /** Returns a pointer to the last object in the array.
  186. This will return a null pointer if the array's empty.
  187. @see getFirst
  188. */
  189. inline ObjectClassPtr getLast() const noexcept
  190. {
  191. if (numUsed > 0)
  192. {
  193. jassert (data.elements != nullptr);
  194. return data.elements [numUsed - 1];
  195. }
  196. return ObjectClassPtr();
  197. }
  198. /** Returns a pointer to the actual array data.
  199. This pointer will only be valid until the next time a non-const method
  200. is called on the array.
  201. */
  202. inline ObjectClass** getRawDataPointer() const noexcept
  203. {
  204. return data.elements;
  205. }
  206. //==============================================================================
  207. /** Returns a pointer to the first element in the array.
  208. This method is provided for compatibility with standard C++ iteration mechanisms.
  209. */
  210. inline ObjectClass** begin() const noexcept
  211. {
  212. return data.elements;
  213. }
  214. /** Returns a pointer to the element which follows the last element in the array.
  215. This method is provided for compatibility with standard C++ iteration mechanisms.
  216. */
  217. inline ObjectClass** end() const noexcept
  218. {
  219. return data.elements + numUsed;
  220. }
  221. //==============================================================================
  222. /** Finds the index of the first occurrence of an object in the array.
  223. @param objectToLookFor the object to look for
  224. @returns the index at which the object was found, or -1 if it's not found
  225. */
  226. int indexOf (const ObjectClass* const objectToLookFor) const noexcept
  227. {
  228. ObjectClass** e = data.elements.getData();
  229. ObjectClass** const endPointer = e + numUsed;
  230. while (e != endPointer)
  231. {
  232. if (objectToLookFor == *e)
  233. return static_cast<int> (e - data.elements.getData());
  234. ++e;
  235. }
  236. return -1;
  237. }
  238. /** Returns true if the array contains a specified object.
  239. @param objectToLookFor the object to look for
  240. @returns true if the object is in the array
  241. */
  242. bool contains (const ObjectClass* const objectToLookFor) const noexcept
  243. {
  244. ObjectClass** e = data.elements.getData();
  245. ObjectClass** const endPointer = e + numUsed;
  246. while (e != endPointer)
  247. {
  248. if (objectToLookFor == *e)
  249. return true;
  250. ++e;
  251. }
  252. return false;
  253. }
  254. /** Appends a new object to the end of the array.
  255. This will increase the new object's reference count.
  256. @param newObject the new object to add to the array
  257. @see set, insert, addIfNotAlreadyThere, addSorted, addArray
  258. */
  259. ObjectClass* add (ObjectClass* const newObject) noexcept
  260. {
  261. data.ensureAllocatedSize (numUsed + 1);
  262. jassert (data.elements != nullptr);
  263. data.elements [numUsed++] = newObject;
  264. if (newObject != nullptr)
  265. newObject->incReferenceCount();
  266. return newObject;
  267. }
  268. /** Inserts a new object into the array at the given index.
  269. If the index is less than 0 or greater than the size of the array, the
  270. element will be added to the end of the array.
  271. Otherwise, it will be inserted into the array, moving all the later elements
  272. along to make room.
  273. This will increase the new object's reference count.
  274. @param indexToInsertAt the index at which the new element should be inserted
  275. @param newObject the new object to add to the array
  276. @see add, addSorted, addIfNotAlreadyThere, set
  277. */
  278. ObjectClass* insert (int indexToInsertAt,
  279. ObjectClass* const newObject) noexcept
  280. {
  281. if (indexToInsertAt < 0)
  282. return add (newObject);
  283. if (indexToInsertAt > numUsed)
  284. indexToInsertAt = numUsed;
  285. data.ensureAllocatedSize (numUsed + 1);
  286. jassert (data.elements != nullptr);
  287. ObjectClass** const e = data.elements + indexToInsertAt;
  288. const int numToMove = numUsed - indexToInsertAt;
  289. if (numToMove > 0)
  290. memmove (e + 1, e, sizeof (ObjectClass*) * (size_t) numToMove);
  291. *e = newObject;
  292. if (newObject != nullptr)
  293. newObject->incReferenceCount();
  294. ++numUsed;
  295. return newObject;
  296. }
  297. /** Appends a new object at the end of the array as long as the array doesn't
  298. already contain it.
  299. If the array already contains a matching object, nothing will be done.
  300. @param newObject the new object to add to the array
  301. @returns true if the object has been added, false otherwise
  302. */
  303. bool addIfNotAlreadyThere (ObjectClass* const newObject) noexcept
  304. {
  305. if (contains (newObject))
  306. return false;
  307. add (newObject);
  308. return true;
  309. }
  310. /** Replaces an object in the array with a different one.
  311. If the index is less than zero, this method does nothing.
  312. If the index is beyond the end of the array, the new object is added to the end of the array.
  313. The object being added has its reference count increased, and if it's replacing
  314. another object, then that one has its reference count decreased, and may be deleted.
  315. @param indexToChange the index whose value you want to change
  316. @param newObject the new value to set for this index.
  317. @see add, insert, remove
  318. */
  319. void set (const int indexToChange,
  320. ObjectClass* const newObject)
  321. {
  322. if (indexToChange >= 0)
  323. {
  324. if (newObject != nullptr)
  325. newObject->incReferenceCount();
  326. if (indexToChange < numUsed)
  327. {
  328. if (ObjectClass* o = data.elements [indexToChange])
  329. releaseObject (o);
  330. data.elements [indexToChange] = newObject;
  331. }
  332. else
  333. {
  334. data.ensureAllocatedSize (numUsed + 1);
  335. jassert (data.elements != nullptr);
  336. data.elements [numUsed++] = newObject;
  337. }
  338. }
  339. }
  340. /** Adds elements from another array to the end of this array.
  341. @param arrayToAddFrom the array from which to copy the elements
  342. @param startIndex the first element of the other array to start copying from
  343. @param numElementsToAdd how many elements to add from the other array. If this
  344. value is negative or greater than the number of available elements,
  345. all available elements will be copied.
  346. @see add
  347. */
  348. void addArray (const ReferenceCountedArray<ObjectClass>& arrayToAddFrom,
  349. int startIndex = 0,
  350. int numElementsToAdd = -1) noexcept
  351. {
  352. if (startIndex < 0)
  353. {
  354. jassertfalse;
  355. startIndex = 0;
  356. }
  357. if (numElementsToAdd < 0 || startIndex + numElementsToAdd > arrayToAddFrom.size())
  358. numElementsToAdd = arrayToAddFrom.size() - startIndex;
  359. if (numElementsToAdd > 0)
  360. {
  361. data.ensureAllocatedSize (numUsed + numElementsToAdd);
  362. while (--numElementsToAdd >= 0)
  363. add (arrayToAddFrom.getUnchecked (startIndex++));
  364. }
  365. }
  366. /** Inserts a new object into the array assuming that the array is sorted.
  367. This will use a comparator to find the position at which the new object
  368. should go. If the array isn't sorted, the behaviour of this
  369. method will be unpredictable.
  370. @param comparator the comparator object to use to compare the elements - see the
  371. sort() method for details about this object's form
  372. @param newObject the new object to insert to the array
  373. @returns the index at which the new object was added
  374. @see add, sort
  375. */
  376. template <class ElementComparator>
  377. int addSorted (ElementComparator& comparator, ObjectClass* newObject) noexcept
  378. {
  379. const int index = findInsertIndexInSortedArray (comparator, data.elements.getData(), newObject, 0, numUsed);
  380. insert (index, newObject);
  381. return index;
  382. }
  383. /** Inserts or replaces an object in the array, assuming it is sorted.
  384. This is similar to addSorted, but if a matching element already exists, then it will be
  385. replaced by the new one, rather than the new one being added as well.
  386. */
  387. template <class ElementComparator>
  388. void addOrReplaceSorted (ElementComparator& comparator,
  389. ObjectClass* newObject) noexcept
  390. {
  391. const int index = findInsertIndexInSortedArray (comparator, data.elements.getData(), newObject, 0, numUsed);
  392. if (index > 0 && comparator.compareElements (newObject, data.elements [index - 1]) == 0)
  393. set (index - 1, newObject); // replace an existing object that matches
  394. else
  395. insert (index, newObject); // no match, so insert the new one
  396. }
  397. /** Finds the index of an object in the array, assuming that the array is sorted.
  398. This will use a comparator to do a binary-chop to find the index of the given
  399. element, if it exists. If the array isn't sorted, the behaviour of this
  400. method will be unpredictable.
  401. @param comparator the comparator to use to compare the elements - see the sort()
  402. method for details about the form this object should take
  403. @param objectToLookFor the object to search for
  404. @returns the index of the element, or -1 if it's not found
  405. @see addSorted, sort
  406. */
  407. template <class ElementComparator>
  408. int indexOfSorted (ElementComparator& comparator,
  409. const ObjectClass* const objectToLookFor) const noexcept
  410. {
  411. ignoreUnused (comparator);
  412. int s = 0, e = numUsed;
  413. while (s < e)
  414. {
  415. if (comparator.compareElements (objectToLookFor, data.elements [s]) == 0)
  416. return s;
  417. const int halfway = (s + e) / 2;
  418. if (halfway == s)
  419. break;
  420. if (comparator.compareElements (objectToLookFor, data.elements [halfway]) >= 0)
  421. s = halfway;
  422. else
  423. e = halfway;
  424. }
  425. return -1;
  426. }
  427. //==============================================================================
  428. /** Removes an object from the array.
  429. This will remove the object at a given index and move back all the
  430. subsequent objects to close the gap.
  431. If the index passed in is out-of-range, nothing will happen.
  432. The object that is removed will have its reference count decreased,
  433. and may be deleted if not referenced from elsewhere.
  434. @param indexToRemove the index of the element to remove
  435. @see removeObject, removeRange
  436. */
  437. void remove (const int indexToRemove)
  438. {
  439. if (isPositiveAndBelow (indexToRemove, numUsed))
  440. {
  441. ObjectClass** const e = data.elements + indexToRemove;
  442. if (ObjectClass* o = *e)
  443. releaseObject (o);
  444. --numUsed;
  445. const int numberToShift = numUsed - indexToRemove;
  446. if (numberToShift > 0)
  447. memmove (e, e + 1, sizeof (ObjectClass*) * (size_t) numberToShift);
  448. if ((numUsed << 1) < data.numAllocated)
  449. minimiseStorageOverheads();
  450. }
  451. }
  452. /** Removes and returns an object from the array.
  453. This will remove the object at a given index and return it, moving back all
  454. the subsequent objects to close the gap. If the index passed in is out-of-range,
  455. nothing will happen and a null pointer will be returned.
  456. @param indexToRemove the index of the element to remove
  457. @see remove, removeObject, removeRange
  458. */
  459. ObjectClassPtr removeAndReturn (const int indexToRemove)
  460. {
  461. ObjectClassPtr removedItem;
  462. if (isPositiveAndBelow (indexToRemove, numUsed))
  463. {
  464. ObjectClass** const e = data.elements + indexToRemove;
  465. if (ObjectClass* o = *e)
  466. {
  467. removedItem = o;
  468. releaseObject (o);
  469. }
  470. --numUsed;
  471. const int numberToShift = numUsed - indexToRemove;
  472. if (numberToShift > 0)
  473. memmove (e, e + 1, sizeof (ObjectClass*) * (size_t) numberToShift);
  474. if ((numUsed << 1) < data.numAllocated)
  475. minimiseStorageOverheads();
  476. }
  477. return removedItem;
  478. }
  479. /** Removes the first occurrence of a specified object from the array.
  480. If the item isn't found, no action is taken. If it is found, it is
  481. removed and has its reference count decreased.
  482. @param objectToRemove the object to try to remove
  483. @see remove, removeRange
  484. */
  485. void removeObject (ObjectClass* const objectToRemove)
  486. {
  487. remove (indexOf (objectToRemove));
  488. }
  489. /** Removes a range of objects from the array.
  490. This will remove a set of objects, starting from the given index,
  491. and move any subsequent elements down to close the gap.
  492. If the range extends beyond the bounds of the array, it will
  493. be safely clipped to the size of the array.
  494. The objects that are removed will have their reference counts decreased,
  495. and may be deleted if not referenced from elsewhere.
  496. @param startIndex the index of the first object to remove
  497. @param numberToRemove how many objects should be removed
  498. @see remove, removeObject
  499. */
  500. void removeRange (const int startIndex,
  501. const int numberToRemove)
  502. {
  503. const int start = jlimit (0, numUsed, startIndex);
  504. const int endIndex = jlimit (0, numUsed, startIndex + numberToRemove);
  505. if (endIndex > start)
  506. {
  507. int i;
  508. for (i = start; i < endIndex; ++i)
  509. {
  510. if (ObjectClass* o = data.elements[i])
  511. {
  512. releaseObject (o);
  513. data.elements[i] = nullptr; // (in case one of the destructors accesses this array and hits a dangling pointer)
  514. }
  515. }
  516. const int rangeSize = endIndex - start;
  517. ObjectClass** e = data.elements + start;
  518. i = numUsed - endIndex;
  519. numUsed -= rangeSize;
  520. while (--i >= 0)
  521. {
  522. *e = e [rangeSize];
  523. ++e;
  524. }
  525. if ((numUsed << 1) < data.numAllocated)
  526. minimiseStorageOverheads();
  527. }
  528. }
  529. /** Removes the last n objects from the array.
  530. The objects that are removed will have their reference counts decreased,
  531. and may be deleted if not referenced from elsewhere.
  532. @param howManyToRemove how many objects to remove from the end of the array
  533. @see remove, removeObject, removeRange
  534. */
  535. void removeLast (int howManyToRemove = 1)
  536. {
  537. if (howManyToRemove > numUsed)
  538. howManyToRemove = numUsed;
  539. while (--howManyToRemove >= 0)
  540. remove (numUsed - 1);
  541. }
  542. /** Swaps a pair of objects in the array.
  543. If either of the indexes passed in is out-of-range, nothing will happen,
  544. otherwise the two objects at these positions will be exchanged.
  545. */
  546. void swap (const int index1,
  547. const int index2) noexcept
  548. {
  549. if (isPositiveAndBelow (index1, numUsed)
  550. && isPositiveAndBelow (index2, numUsed))
  551. {
  552. std::swap (data.elements [index1],
  553. data.elements [index2]);
  554. }
  555. }
  556. /** Moves one of the objects to a different position.
  557. This will move the object to a specified index, shuffling along
  558. any intervening elements as required.
  559. So for example, if you have the array { 0, 1, 2, 3, 4, 5 } then calling
  560. move (2, 4) would result in { 0, 1, 3, 4, 2, 5 }.
  561. @param currentIndex the index of the object to be moved. If this isn't a
  562. valid index, then nothing will be done
  563. @param newIndex the index at which you'd like this object to end up. If this
  564. is less than zero, it will be moved to the end of the array
  565. */
  566. void move (const int currentIndex,
  567. int newIndex) noexcept
  568. {
  569. if (currentIndex != newIndex)
  570. {
  571. if (isPositiveAndBelow (currentIndex, numUsed))
  572. {
  573. if (! isPositiveAndBelow (newIndex, numUsed))
  574. newIndex = numUsed - 1;
  575. ObjectClass* const value = data.elements [currentIndex];
  576. if (newIndex > currentIndex)
  577. {
  578. memmove (data.elements + currentIndex,
  579. data.elements + currentIndex + 1,
  580. sizeof (ObjectClass*) * (size_t) (newIndex - currentIndex));
  581. }
  582. else
  583. {
  584. memmove (data.elements + newIndex + 1,
  585. data.elements + newIndex,
  586. sizeof (ObjectClass*) * (size_t) (currentIndex - newIndex));
  587. }
  588. data.elements [newIndex] = value;
  589. }
  590. }
  591. }
  592. //==============================================================================
  593. /** This swaps the contents of this array with those of another array.
  594. If you need to exchange two arrays, this is vastly quicker than using copy-by-value
  595. because it just swaps their internal pointers.
  596. */
  597. template <class OtherArrayType>
  598. void swapWith (OtherArrayType& otherArray) noexcept
  599. {
  600. data.swapWith (otherArray.data);
  601. std::swap (numUsed, otherArray.numUsed);
  602. }
  603. //==============================================================================
  604. /** Compares this array to another one.
  605. @returns true only if the other array contains the same objects in the same order
  606. */
  607. bool operator== (const ReferenceCountedArray& other) const noexcept
  608. {
  609. if (numUsed != other.numUsed)
  610. return false;
  611. for (int i = numUsed; --i >= 0;)
  612. if (data.elements [i] != other.data.elements [i])
  613. return false;
  614. return true;
  615. }
  616. /** Compares this array to another one.
  617. @see operator==
  618. */
  619. bool operator!= (const ReferenceCountedArray<ObjectClass>& other) const noexcept
  620. {
  621. return ! operator== (other);
  622. }
  623. //==============================================================================
  624. /** Sorts the elements in the array.
  625. This will use a comparator object to sort the elements into order. The object
  626. passed must have a method of the form:
  627. @code
  628. int compareElements (ElementType first, ElementType second);
  629. @endcode
  630. ..and this method must return:
  631. - a value of < 0 if the first comes before the second
  632. - a value of 0 if the two objects are equivalent
  633. - a value of > 0 if the second comes before the first
  634. To improve performance, the compareElements() method can be declared as static or const.
  635. @param comparator the comparator to use for comparing elements.
  636. @param retainOrderOfEquivalentItems if this is true, then items
  637. which the comparator says are equivalent will be
  638. kept in the order in which they currently appear
  639. in the array. This is slower to perform, but may
  640. be important in some cases. If it's false, a faster
  641. algorithm is used, but equivalent elements may be
  642. rearranged.
  643. @see sortArray
  644. */
  645. template <class ElementComparator>
  646. void sort (ElementComparator& comparator,
  647. const bool retainOrderOfEquivalentItems = false) const noexcept
  648. {
  649. ignoreUnused (comparator); // if you pass in an object with a static compareElements() method, this
  650. // avoids getting warning messages about the parameter being unused
  651. sortArray (comparator, data.elements.getData(), 0, size() - 1, retainOrderOfEquivalentItems);
  652. }
  653. //==============================================================================
  654. /** Reduces the amount of storage being used by the array.
  655. Arrays typically allocate slightly more storage than they need, and after
  656. removing elements, they may have quite a lot of unused space allocated.
  657. This method will reduce the amount of allocated storage to a minimum.
  658. */
  659. void minimiseStorageOverheads() noexcept
  660. {
  661. data.shrinkToNoMoreThan (numUsed);
  662. }
  663. /** Increases the array's internal storage to hold a minimum number of elements.
  664. Calling this before adding a large known number of elements means that
  665. the array won't have to keep dynamically resizing itself as the elements
  666. are added, and it'll therefore be more efficient.
  667. */
  668. void ensureStorageAllocated (const int minNumElements)
  669. {
  670. data.ensureAllocatedSize (minNumElements);
  671. }
  672. private:
  673. //==============================================================================
  674. ArrayAllocationBase <ObjectClass*> data;
  675. int numUsed;
  676. void releaseAllObjects()
  677. {
  678. while (numUsed > 0)
  679. if (ObjectClass* o = data.elements [--numUsed])
  680. releaseObject (o);
  681. jassert (numUsed == 0);
  682. }
  683. static void releaseObject (ObjectClass* o)
  684. {
  685. if (o->decReferenceCountWithoutDeleting())
  686. delete o;
  687. }
  688. };
  689. }
  690. #endif // JUCE_REFERENCECOUNTEDARRAY_H_INCLUDED