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.

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