The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

858 lines
31KB

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