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.

835 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 size_t 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 size_t index) const noexcept
  109. {
  110. if (index < numUsed)
  111. {
  112. CARLA_SAFE_ASSERT_RETURN(data.elements != nullptr, 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. return data.elements [index];
  124. }
  125. /** Returns a pointer to the first object in the array.
  126. This will return a null pointer if the array's empty.
  127. @see getLast
  128. */
  129. inline ObjectClass* getFirst() const noexcept
  130. {
  131. if (numUsed > 0)
  132. {
  133. CARLA_SAFE_ASSERT_RETURN(data.elements != nullptr, nullptr);
  134. return data.elements [0];
  135. }
  136. return nullptr;
  137. }
  138. /** Returns a pointer to the last object in the array.
  139. This will return a null pointer if the array's empty.
  140. @see getFirst
  141. */
  142. inline ObjectClass* getLast() const noexcept
  143. {
  144. if (numUsed > 0)
  145. {
  146. CARLA_SAFE_ASSERT_RETURN(data.elements != nullptr, nullptr);
  147. return data.elements [numUsed - 1];
  148. }
  149. return nullptr;
  150. }
  151. /** Returns a pointer to the actual array data.
  152. This pointer will only be valid until the next time a non-const method
  153. is called on the array.
  154. */
  155. inline ObjectClass** getRawDataPointer() noexcept
  156. {
  157. return data.elements;
  158. }
  159. //==============================================================================
  160. /** Returns a pointer to the first element in the array.
  161. This method is provided for compatibility with standard C++ iteration mechanisms.
  162. */
  163. inline ObjectClass** begin() const noexcept
  164. {
  165. return data.elements;
  166. }
  167. /** Returns a pointer to the element which follows the last element in the array.
  168. This method is provided for compatibility with standard C++ iteration mechanisms.
  169. */
  170. inline ObjectClass** end() const noexcept
  171. {
  172. #ifdef DEBUG
  173. if (data.elements == nullptr || numUsed <= 0) // (to keep static analysers happy)
  174. return data.elements;
  175. #endif
  176. return data.elements + numUsed;
  177. }
  178. //==============================================================================
  179. /** Finds the index of an object which might be in the array.
  180. @param objectToLookFor the object to look for
  181. @returns the index at which the object was found, or -1 if it's not found
  182. */
  183. int indexOf (const ObjectClass* objectToLookFor) const noexcept
  184. {
  185. ObjectClass* const* e = data.elements.getData();
  186. ObjectClass* const* const end_ = e + numUsed;
  187. for (; e != end_; ++e)
  188. if (objectToLookFor == *e)
  189. return static_cast<int> (e - data.elements.getData());
  190. return -1;
  191. }
  192. /** Returns true if the array contains a specified object.
  193. @param objectToLookFor the object to look for
  194. @returns true if the object is in the array
  195. */
  196. bool contains (const ObjectClass* objectToLookFor) const noexcept
  197. {
  198. ObjectClass* const* e = data.elements.getData();
  199. ObjectClass* const* const end_ = e + numUsed;
  200. for (; e != end_; ++e)
  201. if (objectToLookFor == *e)
  202. return true;
  203. return false;
  204. }
  205. //==============================================================================
  206. /** Appends a new object to the end of the array.
  207. Note that the this object will be deleted by the OwnedArray when it
  208. is removed, so be careful not to delete it somewhere else.
  209. Also be careful not to add the same object to the array more than once,
  210. as this will obviously cause deletion of dangling pointers.
  211. @param newObject the new object to add to the array
  212. @returns the new object that was added
  213. @see set, insert, addIfNotAlreadyThere, addSorted
  214. */
  215. ObjectClass* add (ObjectClass* newObject) noexcept
  216. {
  217. if (! data.ensureAllocatedSize (numUsed + 1))
  218. return nullptr;
  219. data.elements [numUsed++] = newObject;
  220. return newObject;
  221. }
  222. /** Inserts a new object into the array at the given index.
  223. Note that the this object will be deleted by the OwnedArray when it
  224. is removed, so be careful not to delete it somewhere else.
  225. If the index is less than 0 or greater than the size of the array, the
  226. element will be added to the end of the array.
  227. Otherwise, it will be inserted into the array, moving all the later elements
  228. along to make room.
  229. Be careful not to add the same object to the array more than once,
  230. as this will obviously cause deletion of dangling pointers.
  231. @param indexToInsertAt the index at which the new element should be inserted
  232. @param newObject the new object to add to the array
  233. @returns the new object that was added
  234. @see add, addSorted, addIfNotAlreadyThere, set
  235. */
  236. ObjectClass* insert (int indexToInsertAt, ObjectClass* newObject) noexcept
  237. {
  238. if (indexToInsertAt < 0)
  239. return add (newObject);
  240. size_t uindexToInsertAt = indexToInsertAt;
  241. if (uindexToInsertAt > numUsed)
  242. uindexToInsertAt = numUsed;
  243. if (! data.ensureAllocatedSize (numUsed + 1))
  244. return nullptr;
  245. ObjectClass** const e = data.elements + uindexToInsertAt;
  246. const int numToMove = numUsed - uindexToInsertAt;
  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 (const size_t indexToInsertAt,
  264. ObjectClass* const* newObjects,
  265. const size_t numberOfElements)
  266. {
  267. if (numberOfElements > 0)
  268. {
  269. data.ensureAllocatedSize (numUsed + numberOfElements);
  270. ObjectClass** insertPos = data.elements;
  271. if (indexToInsertAt < numUsed)
  272. {
  273. insertPos += indexToInsertAt;
  274. const size_t numberToMove = numUsed - indexToInsertAt;
  275. std::memmove (insertPos + numberOfElements, insertPos, numberToMove * sizeof (ObjectClass*));
  276. }
  277. else
  278. {
  279. insertPos += numUsed;
  280. }
  281. numUsed += numberOfElements;
  282. for (size_t i=0; i < numberOfElements; ++i)
  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. size_t startIndex = 0,
  381. int numElementsToAdd = -1)
  382. {
  383. if (numElementsToAdd < 0 || startIndex + numElementsToAdd > arrayToAddFrom.size())
  384. numElementsToAdd = arrayToAddFrom.size() - startIndex;
  385. data.ensureAllocatedSize (numUsed + numElementsToAdd);
  386. jassert (numElementsToAdd <= 0 || data.elements != nullptr);
  387. while (--numElementsToAdd >= 0)
  388. data.elements [numUsed++] = createCopyIfNotNull (arrayToAddFrom.getUnchecked (startIndex++));
  389. }
  390. /** Inserts a new object into the array assuming that the array is sorted.
  391. This will use a comparator to find the position at which the new object
  392. should go. If the array isn't sorted, the behaviour of this
  393. method will be unpredictable.
  394. @param comparator the comparator to use to compare the elements - see the sort method
  395. for details about this object's structure
  396. @param newObject the new object to insert to the array
  397. @returns the index at which the new object was added
  398. @see add, sort, indexOfSorted
  399. */
  400. template <class ElementComparator>
  401. int addSorted (ElementComparator& comparator, ObjectClass* const newObject) noexcept
  402. {
  403. ignoreUnused (comparator); // if you pass in an object with a static compareElements() method, this
  404. // avoids getting warning messages about the parameter being unused
  405. const int index = findInsertIndexInSortedArray (comparator, data.elements.getData(), newObject, 0, numUsed);
  406. insert (index, newObject);
  407. return index;
  408. }
  409. /** Finds the index of an object in the array, assuming that the array is sorted.
  410. This will use a comparator to do a binary-chop to find the index of the given
  411. element, if it exists. If the array isn't sorted, the behaviour of this
  412. method will be unpredictable.
  413. @param comparator the comparator to use to compare the elements - see the sort()
  414. method for details about the form this object should take
  415. @param objectToLookFor the object to search for
  416. @returns the index of the element, or -1 if it's not found
  417. @see addSorted, sort
  418. */
  419. template <typename ElementComparator>
  420. int indexOfSorted (ElementComparator& comparator, const ObjectClass* const objectToLookFor) const noexcept
  421. {
  422. ignoreUnused (comparator);
  423. int s = 0, e = numUsed;
  424. while (s < e)
  425. {
  426. if (comparator.compareElements (objectToLookFor, data.elements [s]) == 0)
  427. return s;
  428. const int halfway = (s + e) / 2;
  429. if (halfway == s)
  430. break;
  431. if (comparator.compareElements (objectToLookFor, data.elements [halfway]) >= 0)
  432. s = halfway;
  433. else
  434. e = halfway;
  435. }
  436. return -1;
  437. }
  438. //==============================================================================
  439. /** Removes an object from the array.
  440. This will remove the object at a given index (optionally also
  441. deleting it) and move back all the subsequent objects to close the gap.
  442. If the index passed in is out-of-range, nothing will happen.
  443. @param indexToRemove the index of the element to remove
  444. @param deleteObject whether to delete the object that is removed
  445. @see removeObject, removeRange
  446. */
  447. void remove (const size_t indexToRemove, bool deleteObject = true)
  448. {
  449. ScopedPointer<ObjectClass> toDelete;
  450. if (indexToRemove < numUsed)
  451. {
  452. ObjectClass** const e = data.elements + indexToRemove;
  453. if (deleteObject)
  454. toDelete = *e;
  455. --numUsed;
  456. const size_t numToShift = numUsed - indexToRemove;
  457. if (numToShift > 0)
  458. std::memmove (e, e + 1, sizeof (ObjectClass*) * numToShift);
  459. }
  460. if ((numUsed << 1) < data.numAllocated)
  461. minimiseStorageOverheads();
  462. }
  463. /** Removes and returns an object from the array without deleting it.
  464. This will remove the object at a given index and return it, moving back all
  465. the subsequent objects to close the gap. If the index passed in is out-of-range,
  466. nothing will happen.
  467. @param indexToRemove the index of the element to remove
  468. @see remove, removeObject, removeRange
  469. */
  470. ObjectClass* removeAndReturn (const size_t indexToRemove)
  471. {
  472. ObjectClass* removedItem = nullptr;
  473. if (indexToRemove < numUsed)
  474. {
  475. ObjectClass** const e = data.elements + indexToRemove;
  476. removedItem = *e;
  477. --numUsed;
  478. const size_t numToShift = numUsed - indexToRemove;
  479. if (numToShift > 0)
  480. std::memmove (e, e + 1, sizeof (ObjectClass*) * (size_t) numToShift);
  481. if ((numUsed << 1) < data.numAllocated)
  482. minimiseStorageOverheads();
  483. }
  484. return removedItem;
  485. }
  486. /** Removes a specified object from the array.
  487. If the item isn't found, no action is taken.
  488. @param objectToRemove the object to try to remove
  489. @param deleteObject whether to delete the object (if it's found)
  490. @see remove, removeRange
  491. */
  492. void removeObject (const ObjectClass* objectToRemove, bool deleteObject = true)
  493. {
  494. ObjectClass** const e = data.elements.getData();
  495. for (int i = 0; i < numUsed; ++i)
  496. {
  497. if (objectToRemove == e[i])
  498. {
  499. remove (i, deleteObject);
  500. break;
  501. }
  502. }
  503. }
  504. /** Removes a range of objects from the array.
  505. This will remove a set of objects, starting from the given index,
  506. and move any subsequent elements down to close the gap.
  507. If the range extends beyond the bounds of the array, it will
  508. be safely clipped to the size of the array.
  509. @param startIndex the index of the first object to remove
  510. @param numberToRemove how many objects should be removed
  511. @param deleteObjects whether to delete the objects that get removed
  512. @see remove, removeObject
  513. */
  514. void removeRange (size_t startIndex, const size_t numberToRemove, bool deleteObjects = true)
  515. {
  516. const size_t endIndex = jlimit ((size_t)0U, numUsed, startIndex + numberToRemove);
  517. startIndex = jlimit ((size_t)0U, numUsed, startIndex);
  518. if (endIndex > startIndex)
  519. {
  520. if (deleteObjects)
  521. {
  522. for (int i = startIndex; i < endIndex; ++i)
  523. {
  524. delete data.elements [i];
  525. data.elements [i] = nullptr; // (in case one of the destructors accesses this array and hits a dangling pointer)
  526. }
  527. }
  528. const size_t rangeSize = endIndex - startIndex;
  529. ObjectClass** e = data.elements + startIndex;
  530. size_t numToShift = numUsed - endIndex;
  531. numUsed -= rangeSize;
  532. for (size_t i=0; i < numToShift; ++i)
  533. {
  534. *e = e [rangeSize];
  535. ++e;
  536. }
  537. if ((numUsed << 1) < data.numAllocated)
  538. minimiseStorageOverheads();
  539. }
  540. }
  541. /** Removes the last n objects from the array.
  542. @param howManyToRemove how many objects to remove from the end of the array
  543. @param deleteObjects whether to also delete the objects that are removed
  544. @see remove, removeObject, removeRange
  545. */
  546. void removeLast (int howManyToRemove = 1,
  547. bool deleteObjects = true)
  548. {
  549. if (howManyToRemove >= numUsed)
  550. clear (deleteObjects);
  551. else
  552. removeRange (numUsed - howManyToRemove, howManyToRemove, deleteObjects);
  553. }
  554. /** Swaps a pair of objects in the array.
  555. If either of the indexes passed in is out-of-range, nothing will happen,
  556. otherwise the two objects at these positions will be exchanged.
  557. */
  558. void swap (const size_t index1,
  559. const size_t index2) noexcept
  560. {
  561. if (index1 < numUsed && index2 < numUsed)
  562. {
  563. std::swap (data.elements [index1],
  564. data.elements [index2]);
  565. }
  566. }
  567. /** Moves one of the objects to a different position.
  568. This will move the object to a specified index, shuffling along
  569. any intervening elements as required.
  570. So for example, if you have the array { 0, 1, 2, 3, 4, 5 } then calling
  571. move (2, 4) would result in { 0, 1, 3, 4, 2, 5 }.
  572. @param currentIndex the index of the object to be moved. If this isn't a
  573. valid index, then nothing will be done
  574. @param newIndex the index at which you'd like this object to end up. If this
  575. is less than zero, it will be moved to the end of the array
  576. */
  577. void move (const size_t currentIndex, const size_t newIndex) noexcept
  578. {
  579. if (currentIndex != newIndex)
  580. {
  581. if (currentIndex < numUsed)
  582. {
  583. if (newIndex >= numUsed)
  584. newIndex = numUsed - 1;
  585. ObjectClass* const value = data.elements [currentIndex];
  586. if (newIndex > currentIndex)
  587. {
  588. std::memmove (data.elements + currentIndex,
  589. data.elements + currentIndex + 1,
  590. sizeof (ObjectClass*) * (size_t) (newIndex - currentIndex));
  591. }
  592. else
  593. {
  594. std::memmove (data.elements + newIndex + 1,
  595. data.elements + newIndex,
  596. sizeof (ObjectClass*) * (size_t) (currentIndex - newIndex));
  597. }
  598. data.elements [newIndex] = value;
  599. }
  600. }
  601. }
  602. /** This swaps the contents of this array with those of another array.
  603. If you need to exchange two arrays, this is vastly quicker than using copy-by-value
  604. because it just swaps their internal pointers.
  605. */
  606. template <class OtherArrayType>
  607. void swapWith (OtherArrayType& otherArray) noexcept
  608. {
  609. data.swapWith (otherArray.data);
  610. std::swap (numUsed, otherArray.numUsed);
  611. }
  612. //==============================================================================
  613. /** Reduces the amount of storage being used by the array.
  614. Arrays typically allocate slightly more storage than they need, and after
  615. removing elements, they may have quite a lot of unused space allocated.
  616. This method will reduce the amount of allocated storage to a minimum.
  617. */
  618. bool minimiseStorageOverheads() noexcept
  619. {
  620. return data.shrinkToNoMoreThan (numUsed);
  621. }
  622. /** Increases the array's internal storage to hold a minimum number of elements.
  623. Calling this before adding a large known number of elements means that
  624. the array won't have to keep dynamically resizing itself as the elements
  625. are added, and it'll therefore be more efficient.
  626. */
  627. bool ensureStorageAllocated (const int minNumElements) noexcept
  628. {
  629. return data.ensureAllocatedSize (minNumElements);
  630. }
  631. //==============================================================================
  632. /** Sorts the elements in the array.
  633. This will use a comparator object to sort the elements into order. The object
  634. passed must have a method of the form:
  635. @code
  636. int compareElements (ElementType* first, ElementType* second);
  637. @endcode
  638. ..and this method must return:
  639. - a value of < 0 if the first comes before the second
  640. - a value of 0 if the two objects are equivalent
  641. - a value of > 0 if the second comes before the first
  642. To improve performance, the compareElements() method can be declared as static or const.
  643. @param comparator the comparator to use for comparing elements.
  644. @param retainOrderOfEquivalentItems if this is true, then items
  645. which the comparator says are equivalent will be
  646. kept in the order in which they currently appear
  647. in the array. This is slower to perform, but may
  648. be important in some cases. If it's false, a faster
  649. algorithm is used, but equivalent elements may be
  650. rearranged.
  651. @see sortArray, indexOfSorted
  652. */
  653. template <class ElementComparator>
  654. void sort (ElementComparator& comparator,
  655. bool retainOrderOfEquivalentItems = false) const noexcept
  656. {
  657. ignoreUnused (comparator); // if you pass in an object with a static compareElements() method, this
  658. // avoids getting warning messages about the parameter being unused
  659. sortArray (comparator, data.elements.getData(), 0, size() - 1, retainOrderOfEquivalentItems);
  660. }
  661. private:
  662. //==============================================================================
  663. ArrayAllocationBase <ObjectClass*> data;
  664. size_t numUsed;
  665. void deleteAllObjects()
  666. {
  667. while (numUsed > 0)
  668. delete data.elements [--numUsed];
  669. }
  670. CARLA_DECLARE_NON_COPY_CLASS (OwnedArray)
  671. };
  672. }
  673. #endif // WATER_OWNEDARRAY_H_INCLUDED