47template <
class ObjectClass,
68 : values (std::move (other.values))
83 values = std::move (other.values);
88 template <
class OtherObjectClass,
class OtherCriticalSection>
90 : values (std::move (other.values))
95 template <
class OtherObjectClass,
class OtherCriticalSection>
100 values = std::move (other.values);
106 void clear (
bool deleteObjects =
true)
110 values.setAllocatedSize (0);
129 inline int size() const noexcept
131 return values.size();
151 return values.getValueWithDefault (index);
162 return values[index];
173 return values.getFirst();
184 return values.getLast();
193 return values.begin();
200 inline ObjectClass**
begin() noexcept
202 return values.begin();
208 inline ObjectClass*
const*
begin() const noexcept
210 return values.begin();
216 inline ObjectClass**
end() noexcept
224 inline ObjectClass*
const*
end() const noexcept
232 inline ObjectClass**
data() noexcept
240 inline ObjectClass*
const*
data() const noexcept
251 int indexOf (
const ObjectClass* objectToLookFor)
const noexcept
254 auto* e = values.begin();
256 for (; e != values.end(); ++e)
257 if (objectToLookFor == *e)
258 return static_cast<int> (e - values.begin());
268 bool contains (
const ObjectClass* objectToLookFor)
const noexcept
271 auto* e = values.begin();
273 for (; e != values.end(); ++e)
274 if (objectToLookFor == *e)
293 ObjectClass*
add (ObjectClass* newObject)
296 values.add (newObject);
335 ObjectClass*
insert (
int indexToInsertAt, ObjectClass* newObject)
338 values.insert (indexToInsertAt, newObject, 1);
378 ObjectClass*
const* newObjects,
379 int numberOfElements)
381 if (numberOfElements > 0)
384 values.insertArray (indexToInsertAt, newObjects, numberOfElements);
401 ObjectClass*
set (
int indexToChange, ObjectClass* newObject,
bool deleteOldElement =
true)
403 if (indexToChange >= 0)
410 if (indexToChange < values.size())
412 if (deleteOldElement)
414 toDelete.
reset (values[indexToChange]);
416 if (toDelete.
get() == newObject)
420 values[indexToChange] = newObject;
424 values.add (newObject);
452 return set (indexToChange, newObject.
release(), deleteOldElement);
464 template <
class OtherArrayType>
465 void addArray (
const OtherArrayType& arrayToAddFrom,
467 int numElementsToAdd = -1)
469 const typename OtherArrayType::ScopedLockType lock1 (arrayToAddFrom.getLock());
471 values.addArray (arrayToAddFrom, startIndex, numElementsToAdd);
475 template <
typename OtherArrayType>
479 values.addArray (items);
496 template <
class OtherArrayType>
499 int numElementsToAdd = -1)
501 const typename OtherArrayType::ScopedLockType lock1 (arrayToAddFrom.getLock());
510 if (numElementsToAdd < 0 || startIndex + numElementsToAdd > arrayToAddFrom.size())
511 numElementsToAdd = arrayToAddFrom.size() - startIndex;
513 jassert (numElementsToAdd >= 0);
514 values.ensureAllocatedSize (values.size() + numElementsToAdd);
516 while (--numElementsToAdd >= 0)
532 template <
class ElementComparator>
533 int addSorted (ElementComparator& comparator, ObjectClass* newObject)
noexcept;
547 template <
typename ElementComparator>
548 int indexOfSorted (ElementComparator& comparator,
const ObjectClass* objectToLookFor)
const noexcept;
561 void remove (
int indexToRemove,
bool deleteObject =
true)
570 auto** e = values.begin() + indexToRemove;
575 values.removeElements (indexToRemove, 1);
579 if ((values.size() << 1) < values.capacity())
594 ObjectClass* removedItem =
nullptr;
599 removedItem = values[indexToRemove];
601 values.removeElements (indexToRemove, 1);
603 if ((values.size() << 1) < values.capacity())
618 void removeObject (
const ObjectClass* objectToRemove,
bool deleteObject =
true)
622 for (
int i = 0; i < values.size(); ++i)
624 if (objectToRemove == values[i])
645 void removeRange (
int startIndex,
int numberToRemove,
bool deleteObjects =
true)
648 auto endIndex =
jlimit (0, values.size(), startIndex + numberToRemove);
649 startIndex =
jlimit (0, values.size(), startIndex);
650 numberToRemove = endIndex - startIndex;
652 if (numberToRemove > 0)
657 objectsToDelete.
addArray (values.begin() + startIndex, numberToRemove);
659 values.removeElements (startIndex, numberToRemove);
661 for (
auto& o : objectsToDelete)
664 if ((values.size() << 1) < values.capacity())
676 bool deleteObjects =
true)
680 if (howManyToRemove >= values.size())
681 clear (deleteObjects);
683 removeRange (values.size() - howManyToRemove, howManyToRemove, deleteObjects);
691 void swap (
int index1,
int index2)
noexcept
694 values.swap (index1, index2);
710 void move (
int currentIndex,
int newIndex)
noexcept
712 if (currentIndex != newIndex)
715 values.move (currentIndex, newIndex);
724 template <
class OtherArrayType>
725 void swapWith (OtherArrayType& otherArray)
noexcept
728 const typename OtherArrayType::ScopedLockType lock2 (otherArray.getLock());
729 values.swapWith (otherArray.values);
742 values.shrinkToNoMoreThan (values.size());
754 values.ensureAllocatedSize (minNumElements);
783 template <
class ElementComparator>
784 void sort (ElementComparator& comparator,
bool retainOrderOfEquivalentItems =
false) noexcept;
798 [[deprecated (
"This method has been replaced by a more flexible templated version and renamed "
799 "to swapWith to be more consistent with the names used in other classes.")]]
805 ArrayBase <ObjectClass*, TypeOfCriticalSectionToUse> values;
807 void deleteAllObjects()
809 auto i = values.size();
814 values.removeElements (i, 1);
819 template <
class OtherObjectClass,
class OtherCriticalSection>
820 friend class OwnedArray;
826template <
class ObjectClass,
class TypeOfCriticalSectionToUse>
827template <
class ElementComparator>
829 [[maybe_unused]] ElementComparator& comparator,
830 ObjectClass* newObject)
noexcept
833 auto index = findInsertIndexInSortedArray (comparator, values.begin(), newObject, 0, values.size());
834 insert (index, newObject);
838template <
class ObjectClass,
class TypeOfCriticalSectionToUse>
839template <
typename ElementComparator>
841 [[maybe_unused]] ElementComparator& comparator,
842 const ObjectClass* objectToLookFor)
const noexcept
845 int s = 0, e = values.size();
849 if (comparator.compareElements (objectToLookFor, values[s]) == 0)
852 auto halfway = (s + e) / 2;
857 if (comparator.compareElements (objectToLookFor, values[halfway]) >= 0)
866template <
class ObjectClass,
class TypeOfCriticalSectionToUse>
867template <
typename ElementComparator>
869 [[maybe_unused]] ElementComparator& comparator,
870 bool retainOrderOfEquivalentItems)
noexcept
875 sortArray (comparator, values.begin(), 0, size() - 1, retainOrderOfEquivalentItems);
Holds a resizable array of primitive or copy-by-value objects.
void addArray(const Type *elementsToAdd, int numElementsToAdd)
Adds elements from an array to the end of this array.
An array designed for holding objects.
ObjectClass *const * data() const noexcept
Returns a pointer to the first element in the array.
int size() const noexcept
Returns the number of items currently in the array.
ObjectClass * getUnchecked(int index) const noexcept
Returns a pointer to the object at this index in the array, without checking whether the index is in-...
ObjectClass * set(int indexToChange, ObjectClass *newObject, bool deleteOldElement=true)
Replaces an object in the array with a different one.
ObjectClass * removeAndReturn(int indexToRemove)
Removes and returns an object from the array without deleting it.
ObjectClass * add(std::unique_ptr< ObjectClass > newObject)
Appends a new object to the end of the array.
ObjectClass * set(int indexToChange, std::unique_ptr< ObjectClass > newObject, bool deleteOldElement=true)
Replaces an object in the array with a different one.
void addCopiesOf(const OtherArrayType &arrayToAddFrom, int startIndex=0, int numElementsToAdd=-1)
Adds copies of the elements in another array to the end of this array.
void addArray(const OtherArrayType &arrayToAddFrom, int startIndex=0, int numElementsToAdd=-1)
Adds elements from another array to the end of this array.
bool isEmpty() const noexcept
Returns true if the array is empty, false otherwise.
void swapWith(OtherArrayType &otherArray) noexcept
This swaps the contents of this array with those of another array.
void remove(int indexToRemove, bool deleteObject=true)
Removes an object from the array.
typename TypeOfCriticalSectionToUse::ScopedLockType ScopedLockType
Returns the type of scoped lock to use for locking this array.
void ensureStorageAllocated(int minNumElements) noexcept
Increases the array's internal storage to hold a minimum number of elements.
ObjectClass * getFirst() const noexcept
Returns a pointer to the first object in the array.
void minimiseStorageOverheads() noexcept
Reduces the amount of storage being used by the array.
void clear(bool deleteObjects=true)
Clears the array, optionally deleting the objects inside it first.
void removeLast(int howManyToRemove=1, bool deleteObjects=true)
Removes the last n objects from the array.
int indexOf(const ObjectClass *objectToLookFor) const noexcept
Finds the index of an object which might be in the array.
ObjectClass * add(ObjectClass *newObject)
Appends a new object to the end of the array.
ObjectClass ** begin() noexcept
Returns a pointer to the first element in the array.
OwnedArray(OwnedArray< OtherObjectClass, OtherCriticalSection > &&other) noexcept
Converting move constructor.
~OwnedArray()
Deletes the array and also deletes any objects inside it.
void swap(int index1, int index2) noexcept
Swaps a pair of objects in the array.
int indexOfSorted(ElementComparator &comparator, const ObjectClass *objectToLookFor) const noexcept
Finds the index of an object in the array, assuming that the array is sorted.
ObjectClass * insert(int indexToInsertAt, std::unique_ptr< ObjectClass > newObject)
Inserts a new object into the array at the given index.
void clearQuick(bool deleteObjects)
Clears the array, optionally deleting the objects inside it first.
void removeObject(const ObjectClass *objectToRemove, bool deleteObject=true)
Removes a specified object from the array.
ObjectClass *const * begin() const noexcept
Returns a pointer to the first element in the array.
ObjectClass *const * end() const noexcept
Returns a pointer to the element which follows the last element in the array.
ObjectClass ** end() noexcept
Returns a pointer to the element which follows the last element in the array.
OwnedArray()=default
Creates an empty array.
OwnedArray & operator=(OwnedArray &&other) noexcept
Move assignment operator.
OwnedArray(OwnedArray &&other) noexcept
Move constructor.
void insertArray(int indexToInsertAt, ObjectClass *const *newObjects, int numberOfElements)
Inserts an array of values into this array at a given position.
void move(int currentIndex, int newIndex) noexcept
Moves one of the objects to a different position.
int addSorted(ElementComparator &comparator, ObjectClass *newObject) noexcept
Inserts a new object into the array assuming that the array is sorted.
const TypeOfCriticalSectionToUse & getLock() const noexcept
Returns the CriticalSection that locks this array.
void addArray(const std::initializer_list< OtherArrayType > &items)
Adds elements from another array to the end of this array.
void sort(ElementComparator &comparator, bool retainOrderOfEquivalentItems=false) noexcept
Sorts the elements in the array.
ObjectClass * insert(int indexToInsertAt, ObjectClass *newObject)
Inserts a new object into the array at the given index.
ObjectClass * operator[](int index) const noexcept
Returns a pointer to the object at this index in the array.
ObjectClass ** getRawDataPointer() noexcept
Returns a pointer to the actual array data.
ObjectClass ** data() noexcept
Returns a pointer to the first element in the array.
OwnedArray(const std::initializer_list< ObjectClass * > &items)
Creates an array from a list of objects.
void removeRange(int startIndex, int numberToRemove, bool deleteObjects=true)
Removes a range of objects from the array.
ObjectClass * getLast() const noexcept
Returns a pointer to the last object in the array.
bool contains(const ObjectClass *objectToLookFor) const noexcept
Returns true if the array contains a specified object.
Type * createCopyIfNotNull(const Type *objectToCopy)
If a pointer is non-null, this returns a new copy of the object that it points to,...
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Constrains a value to keep it within a given range.
bool isPositiveAndBelow(Type1 valueToTest, Type2 upperLimit) noexcept
Returns true if a value is at least zero, and also below a specified upper limit.
Used by container classes as an indirect way to delete an object of a particular type.