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.

846 lines
31KB

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