49template <
class ObjectClass,
class TypeOfCriticalSectionToUse = DummyCriticalSection>
65 values.addArray (other.begin(), other.size());
69 o->incReferenceCount();
74 : values (std::move (other.values))
79 template <
class OtherObjectClass,
class OtherCriticalSection>
83 values.addArray (other.begin(), other.size());
87 o->incReferenceCount();
96 auto otherCopy = other;
104 template <
class OtherObjectClass>
107 auto otherCopy = other;
116 values = std::move (other.values);
136 values.setAllocatedSize (0);
150 inline int size() const noexcept
152 return values.size();
196 return values.getValueWithDefault (index);
205 return values[index];
216 return values.getFirst();
227 return values.getLast();
236 return values.begin();
243 inline ObjectClass**
begin() noexcept
245 return values.begin();
251 inline ObjectClass*
const*
begin() const noexcept
253 return values.begin();
259 inline ObjectClass**
end() noexcept
267 inline ObjectClass*
const*
end() const noexcept
275 inline ObjectClass**
data() noexcept
283 inline ObjectClass*
const*
data() const noexcept
294 int indexOf (
const ObjectClass* objectToLookFor)
const noexcept
297 auto* e = values.begin();
298 auto* endPointer = values.end();
300 while (e != endPointer)
302 if (objectToLookFor == *e)
303 return static_cast<int> (e - values.begin());
323 bool contains (
const ObjectClass* objectToLookFor)
const noexcept
326 auto* e = values.begin();
327 auto* endPointer = values.end();
329 while (e != endPointer)
331 if (objectToLookFor == *e)
354 ObjectClass*
add (ObjectClass* newObject)
357 values.add (newObject);
359 if (newObject !=
nullptr)
360 newObject->incReferenceCount();
387 ObjectClass*
insert (
int indexToInsertAt, ObjectClass* newObject)
389 values.insert (indexToInsertAt, newObject, 1);
391 if (newObject !=
nullptr)
392 newObject->incReferenceCount();
453 void set (
int indexToChange, ObjectClass* newObject)
455 if (indexToChange >= 0)
459 if (newObject !=
nullptr)
460 newObject->incReferenceCount();
462 if (indexToChange < values.size())
464 auto* e = values[indexToChange];
465 values[indexToChange] = newObject;
470 values.add (newObject);
500 int numElementsToAdd = -1) noexcept
507 auto numElementsAdded = values.addArray (arrayToAddFrom.values, startIndex, numElementsToAdd);
508 auto** e = values.end();
510 for (
int i = 0; i < numElementsAdded; ++i)
511 (*(--e))->incReferenceCount();
527 template <
class ElementComparator>
528 int addSorted (ElementComparator& comparator, ObjectClass* newObject)
noexcept
531 auto index = findInsertIndexInSortedArray (comparator, values.begin(), newObject, 0, values.size());
532 insert (index, newObject);
541 template <
class ElementComparator>
545 auto index = findInsertIndexInSortedArray (comparator, values.begin(), newObject, 0, values.size());
547 if (index > 0 && comparator.compareElements (newObject, values[index - 1]) == 0)
548 set (index - 1, newObject);
550 insert (index, newObject);
565 template <
class ElementComparator>
566 int indexOfSorted (ElementComparator& comparator,
const ObjectClass* objectToLookFor)
const noexcept;
588 auto* e = *(values.begin() + indexToRemove);
589 values.removeElements (indexToRemove, 1);
592 if ((values.size() << 1) < values.capacity())
613 auto* e = *(values.begin() + indexToRemove);
615 values.removeElements (indexToRemove, 1);
618 if ((values.size() << 1) < values.capacity())
668 startIndex =
jlimit (0, values.size(), startIndex);
669 auto endIndex =
jlimit (0, values.size(), startIndex + numberToRemove);
670 numberToRemove = endIndex - startIndex;
672 if (numberToRemove > 0)
675 objectsToRemove.
addArray (values.begin() + startIndex, numberToRemove);
677 values.removeElements (startIndex, numberToRemove);
679 for (
auto& o : objectsToRemove)
682 if ((values.size() << 1) < values.capacity())
699 if (howManyToRemove > values.size())
700 howManyToRemove = values.size();
702 while (--howManyToRemove >= 0)
703 remove (values.size() - 1);
711 void swap (
int index1,
int index2)
noexcept
718 std::swap (values[index1], values[index2]);
735 void move (
int currentIndex,
int newIndex)
noexcept
737 if (currentIndex != newIndex)
740 values.move (currentIndex, newIndex);
750 template <
class OtherArrayType>
751 void swapWith (OtherArrayType& otherArray)
noexcept
754 const typename OtherArrayType::ScopedLockType lock2 (otherArray.getLock());
755 values.swapWith (otherArray.values);
767 return values == other.values;
806 template <
class ElementComparator>
807 void sort (ElementComparator& comparator,
bool retainOrderOfEquivalentItems =
false) noexcept;
819 values.shrinkToNoMoreThan (values.size());
831 values.ensureAllocatedSize (minNumElements);
846 [[deprecated (
"This method has been replaced by a more flexible templated version and renamed "
847 "to swapWith to be more consistent with the names used in other classes.")]]
855 void releaseAllObjects()
857 auto i = values.size();
862 values.removeElements (i, 1);
867 static void releaseObject (ObjectClass* o)
869 if (o !=
nullptr && o->decReferenceCountWithoutDeleting())
870 ContainerDeletePolicy<ObjectClass>::destroy (o);
875template <
class ObjectClass,
class TypeOfCriticalSectionToUse>
876template <
class ElementComparator>
878 [[maybe_unused]] ElementComparator& comparator,
879 const ObjectClass* objectToLookFor)
const noexcept
882 int s = 0, e = values.size();
886 if (comparator.compareElements (objectToLookFor, values[s]) == 0)
889 auto halfway = (s + e) / 2;
894 if (comparator.compareElements (objectToLookFor, values[halfway]) >= 0)
903template <
class ObjectClass,
class TypeOfCriticalSectionToUse>
904template <
class ElementComparator>
906 [[maybe_unused]] ElementComparator& comparator,
907 bool retainOrderOfEquivalentItems)
noexcept
910 sortArray (comparator, values.begin(), 0, values.size() - 1, retainOrderOfEquivalentItems);
A basic object container.
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.
Holds a list of objects derived from ReferenceCountedObject, or which implement basic reference-count...
ReferenceCountedArray(ReferenceCountedArray &&other) noexcept
Moves from another array.
ObjectClass * add(const ObjectClassPtr &newObject)
Appends a new object to the end of the array.
bool isEmpty() const noexcept
Returns true if the array is empty, false otherwise.
int indexOf(const ObjectClass *objectToLookFor) const noexcept
Finds the index of the first occurrence of an object in the array.
ReferenceCountedArray(const ReferenceCountedArray &other) noexcept
Creates a copy of another array.
int size() const noexcept
Returns the current number of objects in the array.
void removeLast(int howManyToRemove=1)
Removes the last n objects from the array.
ObjectClass *const * end() const noexcept
Returns a pointer to the element which follows the last element in the array.
void minimiseStorageOverheads() noexcept
Reduces the amount of storage being used by the array.
void set(int indexToChange, ObjectClass *newObject)
Replaces an object in the array with a different one.
ObjectClass *const * begin() const noexcept
Returns a pointer to the first element in the array.
ObjectClass * insert(int indexToInsertAt, const ObjectClassPtr &newObject)
Inserts a new object into the array at the given index.
ReferenceCountedArray()=default
Creates an empty array.
bool operator!=(const ReferenceCountedArray< ObjectClass, TypeOfCriticalSectionToUse > &other) const noexcept
Compares this array to another one.
int indexOfSorted(ElementComparator &comparator, const ObjectClass *objectToLookFor) const noexcept
Finds the index of an object in the array, assuming that the array is sorted.
typename TypeOfCriticalSectionToUse::ScopedLockType ScopedLockType
Returns the type of scoped lock to use for locking this array.
bool addIfNotAlreadyThere(ObjectClass *newObject)
Appends a new object at the end of the array as long as the array doesn't already contain it.
ObjectClassPtr operator[](int index) const noexcept
Returns a pointer to the object at this index in the array.
void remove(int indexToRemove)
Removes an object from the array.
void sort(ElementComparator &comparator, bool retainOrderOfEquivalentItems=false) noexcept
Sorts the elements in the array.
ReferenceCountedArray(const ReferenceCountedArray< OtherObjectClass, OtherCriticalSection > &other) noexcept
Creates a copy of another array.
bool operator==(const ReferenceCountedArray &other) const noexcept
Compares this array to another one.
void removeObject(ObjectClass *objectToRemove)
Removes the first occurrence of a specified object from the array.
ObjectClass * getObjectPointer(int index) const noexcept
Returns a raw pointer to the object at this index in the array.
void swap(int index1, int index2) noexcept
Swaps a pair of objects in the array.
ObjectClassPtr getFirst() const noexcept
Returns a pointer to the first object in the array.
void ensureStorageAllocated(const int minNumElements)
Increases the array's internal storage to hold a minimum number of elements.
~ReferenceCountedArray()
Destructor.
void clearQuick()
Removes all objects from the array without freeing the array's allocated storage.
void set(int indexToChange, const ObjectClassPtr &newObject)
Replaces an object in the array with a different one.
ObjectClass *const * data() const noexcept
Returns a pointer to the first element in the array.
const TypeOfCriticalSectionToUse & getLock() const noexcept
Returns the CriticalSection that locks this array.
ObjectClassPtr 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 ** begin() noexcept
Returns a pointer to the first element in the array.
void addOrReplaceSorted(ElementComparator &comparator, ObjectClass *newObject) noexcept
Inserts or replaces an object in the array, assuming it is sorted.
bool contains(const ObjectClass *objectToLookFor) const noexcept
Returns true if the array contains a specified object.
void removeRange(int startIndex, int numberToRemove)
Removes a range of objects from the array.
ObjectClass * getObjectPointerUnchecked(int index) const noexcept
Returns a raw pointer to the object at this index in the array, without checking whether the index is...
void move(int currentIndex, int newIndex) noexcept
Moves one of the objects to a different position.
ObjectClassPtr removeAndReturn(int indexToRemove)
Removes and returns an object from the array.
ReferenceCountedArray & operator=(const ReferenceCountedArray &other) noexcept
Copies another array into this one.
ObjectClass ** end() noexcept
Returns a pointer to the element which follows the last element in the array.
void clear()
Removes all objects from the array.
ObjectClass ** getRawDataPointer() const noexcept
Returns a pointer to the actual array data.
bool contains(const ObjectClassPtr &objectToLookFor) const noexcept
Returns true if the array contains a specified object.
ObjectClass ** data() noexcept
Returns a pointer to the first element in the array.
void swapWith(OtherArrayType &otherArray) noexcept
This swaps the contents of this array with those of another array.
bool addIfNotAlreadyThere(const ObjectClassPtr &newObject)
Appends a new object at the end of the array as long as the array doesn't already contain it.
int addSorted(ElementComparator &comparator, ObjectClass *newObject) noexcept
Inserts a new object into the array assuming that the array is sorted.
int indexOf(const ObjectClassPtr &objectToLookFor) const noexcept
Finds the index of the first occurrence of an object in the array.
ObjectClassPtr getLast() const noexcept
Returns a pointer to the last object in the array.
void addArray(const ReferenceCountedArray &arrayToAddFrom, int startIndex=0, int numElementsToAdd=-1) noexcept
Adds elements from another array to the end of this array.
ObjectClass * insert(int indexToInsertAt, ObjectClass *newObject)
Inserts a new object into the array at the given index.
ObjectClass * add(ObjectClass *newObject)
Appends a new object to the end of the array.
void removeObject(const ObjectClassPtr &objectToRemove)
Removes the first occurrence of a specified object from the array.
A smart-pointer class which points to a reference-counted object.
ReferencedType * get() const noexcept
Returns the object that this pointer references.
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.