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.

843 lines
30KB

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