56 template <
typename ElementType,
57 typename TypeOfCriticalSectionToUse = DummyCriticalSection,
58 int minimumAllocatedSize = 0>
62 using ParameterType =
typename TypeHelpers::ParameterType<ElementType>::type;
75 values.addArray (other.values.begin(), other.values.size());
79 : values (std::move (other.values))
86 template <
typename TypeToCreateFrom>
89 while (*values != TypeToCreateFrom())
97 template <
typename TypeToCreateFrom>
100 values.addArray (data, numValues);
104 Array (
const ElementType& singleElementToAdd)
106 add (singleElementToAdd);
110 Array (ElementType&& singleElementToAdd)
112 add (std::move (singleElementToAdd));
116 template <
typename... OtherElements>
117 Array (
const ElementType& firstNewElement, OtherElements... otherElements)
119 values.add (firstNewElement, otherElements...);
123 template <
typename... OtherElements>
124 Array (ElementType&& firstNewElement, OtherElements... otherElements)
126 values.add (std::move (firstNewElement), otherElements...);
129 template <
typename TypeToCreateFrom>
130 Array (
const std::initializer_list<TypeToCreateFrom>& items)
145 auto otherCopy (other);
155 values = std::move (other.values);
165 template <
class OtherArrayType>
169 const typename OtherArrayType::ScopedLockType lock2 (other.getLock());
170 return values == other;
178 template <
class OtherArrayType>
196 values.setAllocatedSize (0);
209 void fill (
const ParameterType& newValue) noexcept
213 for (
auto& e : *
this)
219 inline int size() const noexcept
222 return values.size();
244 return values.getValueWithDefault (index);
259 return values[index];
274 return values[index];
288 return values[index];
297 return values.getFirst();
307 return values.getLast();
316 return values.begin();
325 return values.begin();
332 inline ElementType*
begin() noexcept
334 return values.begin();
340 inline const ElementType*
begin() const noexcept
342 return values.begin();
348 inline ElementType*
end() noexcept
356 inline const ElementType*
end() const noexcept
364 inline ElementType*
data() noexcept
372 inline const ElementType*
data() const noexcept
386 int indexOf (ParameterType elementToLookFor)
const 389 auto e = values.begin();
390 auto endPtr = values.end();
392 for (; e != endPtr; ++e)
393 if (elementToLookFor == *e)
394 return static_cast<int> (e - values.begin());
404 bool contains (ParameterType elementToLookFor)
const 407 auto e = values.begin();
408 auto endPtr = values.end();
410 for (; e != endPtr; ++e)
411 if (elementToLookFor == *e)
422 void add (
const ElementType& newElement)
425 values.add (newElement);
432 void add (ElementType&& newElement)
435 values.add (std::move (newElement));
439 template <
typename... OtherElements>
440 void add (
const ElementType& firstNewElement, OtherElements... otherElements)
443 values.add (firstNewElement, otherElements...);
447 template <
typename... OtherElements>
448 void add (ElementType&& firstNewElement, OtherElements... otherElements)
451 values.add (std::move (firstNewElement), otherElements...);
466 void insert (
int indexToInsertAt, ParameterType newElement)
469 values.insert (indexToInsertAt, newElement, 1);
485 int numberOfTimesToInsertIt)
487 if (numberOfTimesToInsertIt > 0)
490 values.insert (indexToInsertAt, newElement, numberOfTimesToInsertIt);
507 const ElementType* newElements,
508 int numberOfElements)
510 if (numberOfElements > 0)
513 values.insertArray (indexToInsertAt, newElements, numberOfElements);
546 void set (
int indexToChange, ParameterType newValue)
548 if (indexToChange >= 0)
552 if (indexToChange < values.size())
553 values[indexToChange] = newValue;
555 values.add (newValue);
575 jassert (isPositiveAndBelow (indexToChange, values.size()));
576 values[indexToChange] = newValue;
586 template <
typename Type>
587 void addArray (
const Type* elementsToAdd,
int numElementsToAdd)
591 if (numElementsToAdd > 0)
592 values.addArray (elementsToAdd, numElementsToAdd);
595 template <
typename TypeToCreateFrom>
596 void addArray (
const std::initializer_list<TypeToCreateFrom>& items)
599 values.addArray (items);
608 template <
typename Type>
613 for (
auto e = elementsToAdd; *e !=
nullptr; ++e)
624 template <
class OtherArrayType>
625 void swapWith (OtherArrayType& otherArray) noexcept
628 const typename OtherArrayType::ScopedLockType lock2 (otherArray.getLock());
629 values.swapWith (otherArray.values);
637 template <
class OtherArrayType>
638 void addArray (
const OtherArrayType& arrayToAddFrom)
640 const typename OtherArrayType::ScopedLockType lock1 (arrayToAddFrom.getLock());
643 values.addArray (arrayToAddFrom);
655 template <
class OtherArrayType>
656 typename std::enable_if<! std::is_pointer<OtherArrayType>::value,
void>::type
659 int numElementsToAdd = -1)
661 const typename OtherArrayType::ScopedLockType lock1 (arrayToAddFrom.getLock());
664 values.addArray (arrayToAddFrom, startIndex, numElementsToAdd);
676 jassert (targetNumItems >= 0);
677 auto numToAdd = targetNumItems - values.size();
681 else if (numToAdd < 0)
697 template <
class ElementComparator>
698 int addSorted (ElementComparator& comparator, ParameterType newElement)
701 auto index = findInsertIndexInSortedArray (comparator, values.begin(), newElement, 0, values.size());
702 insert (index, newElement);
733 template <
typename ElementComparator,
typename TargetValueType>
734 int indexOfSorted (ElementComparator& comparator, TargetValueType elementToLookFor)
const 736 ignoreUnused (comparator);
741 for (
int s = 0, e = values.size();;)
746 if (comparator.compareElements (elementToLookFor, values[s]) == 0)
749 auto halfway = (s + e) / 2;
754 if (comparator.compareElements (elementToLookFor, values[halfway]) >= 0)
771 void remove (
int indexToRemove)
775 if (isPositiveAndBelow (indexToRemove, values.size()))
776 removeInternal (indexToRemove);
793 if (isPositiveAndBelow (indexToRemove, values.size()))
795 ElementType removed (values[indexToRemove]);
796 removeInternal (indexToRemove);
800 return ElementType();
813 void remove (
const ElementType* elementToRemove)
815 jassert (elementToRemove !=
nullptr);
818 jassert (values.begin() !=
nullptr);
819 auto indexToRemove = (int) (elementToRemove - values.begin());
821 if (! isPositiveAndBelow (indexToRemove, values.size()))
827 removeInternal (indexToRemove);
841 auto* e = values.begin();
843 for (
int i = 0; i < values.size(); ++i)
845 if (valueToRemove == e[i])
867 for (
int i = values.size(); --i >= 0;)
869 if (valueToRemove == values[i])
890 template <
typename PredicateType>
896 for (
int i = values.size(); --i >= 0;)
898 if (predicate (values[i]))
924 auto endIndex = jlimit (0, values.size(), startIndex + numberToRemove);
925 startIndex = jlimit (0, values.size(), startIndex);
926 numberToRemove = endIndex - startIndex;
928 if (numberToRemove > 0)
930 values.removeElements (startIndex, numberToRemove);
931 minimiseStorageAfterRemoval();
942 jassert (howManyToRemove >= 0);
944 if (howManyToRemove > 0)
948 if (howManyToRemove > values.size())
949 howManyToRemove = values.size();
951 values.removeElements (values.size() - howManyToRemove, howManyToRemove);
952 minimiseStorageAfterRemoval();
961 template <
class OtherArrayType>
964 const typename OtherArrayType::ScopedLockType lock1 (otherArray.getLock());
967 if (
this == &otherArray)
973 if (otherArray.size() > 0)
975 for (
int i = values.size(); --i >= 0;)
976 if (otherArray.contains (values[i]))
989 template <
class OtherArrayType>
992 const typename OtherArrayType::ScopedLockType lock1 (otherArray.getLock());
995 if (
this != &otherArray)
997 if (otherArray.size() <= 0)
1003 for (
int i = values.size(); --i >= 0;)
1004 if (! otherArray.contains (values[i]))
1021 values.swap (index1, index2);
1038 void move (
int currentIndex,
int newIndex) noexcept
1040 if (currentIndex != newIndex)
1043 values.move (currentIndex, newIndex);
1057 values.shrinkToNoMoreThan (values.size());
1069 values.ensureAllocatedSize (minNumElements);
1109 template <
class ElementComparator>
1110 void sort (ElementComparator& comparator,
1111 bool retainOrderOfEquivalentItems =
false)
1114 ignoreUnused (comparator);
1116 sortArray (comparator, values.begin(), 0,
size() - 1, retainOrderOfEquivalentItems);
1124 inline const TypeOfCriticalSectionToUse&
getLock() const noexcept {
return values; }
1134 JUCE_DEPRECATED_WITH_BODY (
void swapWithArray (
Array& other) noexcept, {
swapWith (other); })
1141 void removeInternal (
int indexToRemove)
1143 values.removeElements (indexToRemove, 1);
1144 minimiseStorageAfterRemoval();
1147 void minimiseStorageAfterRemoval()
1149 if (values.capacity() > jmax (minimumAllocatedSize, values.size() * 2))
1150 values.shrinkToNoMoreThan (jmax (values.size(), jmax (minimumAllocatedSize, 64 / (
int)
sizeof (ElementType))));
void sort()
Sorts the array using a default comparison operation.
void removeValuesNotIn(const OtherArrayType &otherArray)
Removes any elements which are not found in another array.
int removeIf(PredicateType &&predicate)
Removes items from the array.
void removeFirstMatchingValue(ParameterType valueToRemove)
Removes an item from the array.
void addArray(const Type *elementsToAdd, int numElementsToAdd)
Adds elements from an array to the end of this array.
const ElementType * data() const noexcept
Returns a pointer to the first element in the array.
Array & operator=(const Array &other)
Copies another array.
ElementType * end() noexcept
Returns a pointer to the element which follows the last element in the array.
void minimiseStorageOverheads()
Reduces the amount of storage being used by the array.
void swapWith(OtherArrayType &otherArray) noexcept
This swaps the contents of this array with those of another array.
void fill(const ParameterType &newValue) noexcept
Fills the Array with the provided value.
void clearQuick()
Removes all elements from the array without freeing the array's allocated storage.
void insertArray(int indexToInsertAt, const ElementType *newElements, int numberOfElements)
Inserts an array of values into this array at a given position.
typename DummyCriticalSection ::ScopedLockType ScopedLockType
Returns the type of scoped lock to use for locking this array.
int indexOf(ParameterType elementToLookFor) const
Finds the index of the first element which matches the value passed in.
bool addIfNotAlreadyThere(ParameterType newElement)
Appends a new element at the end of the array as long as the array doesn't already contain it...
Array(const ElementType &firstNewElement, OtherElements... otherElements)
Initalises an Array from a list of items.
void removeValuesIn(const OtherArrayType &otherArray)
Removes any elements which are also in another array.
void ensureStorageAllocated(int minNumElements)
Increases the array's internal storage to hold a minimum number of elements.
void swap(int index1, int index2)
Swaps over two elements in the array.
void add(ElementType &&newElement)
Appends a new element at the end of the array.
void add(const ElementType &newElement)
Appends a new element at the end of the array.
void add(const ElementType &firstNewElement, OtherElements... otherElements)
Appends multiple new elements at the end of the array.
ElementType getLast() const noexcept
Returns the last element in the array, or a default value if the array is empty.
ElementType * data() noexcept
Returns a pointer to the first element in the array.
const ElementType & getReference(int index) const noexcept
Returns a direct reference to one of the elements in the array, without checking the index passed in...
void addArray(const OtherArrayType &arrayToAddFrom)
Adds elements from another array to the end of this array.
void move(int currentIndex, int newIndex) noexcept
Moves one of the values to a different position.
Array(const TypeToCreateFrom *data)
Initalises from a null-terminated raw array of values.
Array(const ElementType &singleElementToAdd)
Initalises an Array of size 1 containing a single element.
ElementType operator[](int index) const
Returns one of the elements in the array.
A basic object container.
Array(const TypeToCreateFrom *data, int numValues)
Initalises from a raw array of values.
void insert(int indexToInsertAt, ParameterType newElement)
Inserts a new element into the array at a given position.
void resize(int targetNumItems)
This will enlarge or shrink the array to the given number of elements, by adding or removing items fr...
ElementType getUnchecked(int index) const
Returns one of the elements in the array, without checking the index passed in.
void addNullTerminatedArray(const Type *const *elementsToAdd)
Adds elements from a null-terminated array of pointers to the end of this array.
int addSorted(ElementComparator &comparator, ParameterType newElement)
Inserts a new element into the array, assuming that the array is sorted.
Array(const Array &other)
Creates a copy of another array.
const ElementType * begin() const noexcept
Returns a pointer to the first element in the array.
Array(ElementType &&singleElementToAdd)
Initalises an Array of size 1 containing a single element.
bool operator!=(const OtherArrayType &other) const
Compares this array to another one.
ElementType * begin() noexcept
Returns a pointer to the first element in the array.
void insertMultiple(int indexToInsertAt, ParameterType newElement, int numberOfTimesToInsertIt)
Inserts multiple copies of an element into the array at a given position.
Array()=default
Creates an empty array.
void add(ElementType &&firstNewElement, OtherElements... otherElements)
Appends multiple new elements at the end of the array.
void removeLast(int howManyToRemove=1)
Removes the last n elements from the array.
void setUnchecked(int indexToChange, ParameterType newValue)
Replaces an element with a new value without doing any bounds-checking.
void addUsingDefaultSort(ParameterType newElement)
Inserts a new element into the array, assuming that the array is sorted.
A simple ElementComparator class that can be used to sort an array of objects that support the '<' op...
~Array()=default
Destructor.
Holds a resizable array of primitive or copy-by-value objects.
void clear()
Removes all elements from the array.
int size() const noexcept
Returns the current number of elements in the array.
ElementType removeAndReturn(int indexToRemove)
Removes an element from the array.
const TypeOfCriticalSectionToUse & getLock() const noexcept
Returns the CriticalSection that locks this array.
ElementType getFirst() const noexcept
Returns the first element in the array, or a default value if the array is empty. ...
ElementType * getRawDataPointer() noexcept
Returns a pointer to the actual array data.
const ElementType * end() const noexcept
Returns a pointer to the element which follows the last element in the array.
ElementType & getReference(int index) noexcept
Returns a direct reference to one of the elements in the array, without checking the index passed in...
bool isEmpty() const noexcept
Returns true if the array is empty, false otherwise.
Array(ElementType &&firstNewElement, OtherElements... otherElements)
Initalises an Array from a list of items.
int indexOfSorted(ElementComparator &comparator, TargetValueType elementToLookFor) const
Finds the index of an element in the array, assuming that the array is sorted.
const ElementType * getRawDataPointer() const noexcept
Returns a pointer to the actual array data.
int removeAllInstancesOf(ParameterType valueToRemove)
Removes items from the array.
bool contains(ParameterType elementToLookFor) const
Returns true if the array contains at least one occurrence of an object.
std::enable_if<! std::is_pointer< OtherArrayType >::value, void >::type addArray(const OtherArrayType &arrayToAddFrom, int startIndex, int numElementsToAdd=-1)
Adds elements from another array to the end of this array.
void removeRange(int startIndex, int numberToRemove)
Removes a range of elements from the array.
bool operator==(const OtherArrayType &other) const
Compares this array to another one.
void sort(ElementComparator &comparator, bool retainOrderOfEquivalentItems=false)
Sorts the elements in the array.