56template <
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];
283 return values.getFirst();
293 return values.getLast();
302 return values.begin();
309 inline ElementType*
begin() const noexcept
311 return values.begin();
317 inline ElementType*
end() const noexcept
325 inline ElementType*
data() const noexcept
339 int indexOf (ParameterType elementToLookFor)
const
342 auto e = values.begin();
343 auto endPtr = values.end();
345 for (; e != endPtr; ++e)
346 if (elementToLookFor == *e)
347 return static_cast<int> (e - values.begin());
357 bool contains (ParameterType elementToLookFor)
const
360 auto e = values.begin();
361 auto endPtr = values.end();
363 for (; e != endPtr; ++e)
364 if (elementToLookFor == *e)
375 void add (
const ElementType& newElement)
378 values.add (newElement);
385 void add (ElementType&& newElement)
388 values.add (std::move (newElement));
392 template <
typename... OtherElements>
393 void add (
const ElementType& firstNewElement, OtherElements... otherElements)
396 values.add (firstNewElement, otherElements...);
400 template <
typename... OtherElements>
401 void add (ElementType&& firstNewElement, OtherElements... otherElements)
404 values.add (std::move (firstNewElement), otherElements...);
419 void insert (
int indexToInsertAt, ParameterType newElement)
422 values.insert (indexToInsertAt, newElement, 1);
438 int numberOfTimesToInsertIt)
440 if (numberOfTimesToInsertIt > 0)
443 values.insert (indexToInsertAt, newElement, numberOfTimesToInsertIt);
460 const ElementType* newElements,
461 int numberOfElements)
463 if (numberOfElements > 0)
466 values.insertArray (indexToInsertAt, newElements, numberOfElements);
499 void set (
int indexToChange, ParameterType newValue)
501 if (indexToChange >= 0)
505 if (indexToChange < values.size())
506 values[indexToChange] = newValue;
508 values.add (newValue);
528 jassert (isPositiveAndBelow (indexToChange, values.size()));
529 values[indexToChange] = newValue;
539 template <
typename Type>
540 void addArray (
const Type* elementsToAdd,
int numElementsToAdd)
544 if (numElementsToAdd > 0)
545 values.addArray (elementsToAdd, numElementsToAdd);
548 template <
typename TypeToCreateFrom>
549 void addArray (
const std::initializer_list<TypeToCreateFrom>& items)
552 values.addArray (items);
561 template <
typename Type>
566 for (
auto e = elementsToAdd; *e !=
nullptr; ++e)
577 template <
class OtherArrayType>
578 void swapWith (OtherArrayType& otherArray)
noexcept
581 const typename OtherArrayType::ScopedLockType lock2 (otherArray.getLock());
582 values.swapWith (otherArray.values);
590 template <
class OtherArrayType>
591 void addArray (
const OtherArrayType& arrayToAddFrom)
593 const typename OtherArrayType::ScopedLockType lock1 (arrayToAddFrom.getLock());
596 values.addArray (arrayToAddFrom);
608 template <
class OtherArrayType>
609 typename std::enable_if<! std::is_pointer<OtherArrayType>::value,
void>::type
612 int numElementsToAdd = -1)
614 const typename OtherArrayType::ScopedLockType lock1 (arrayToAddFrom.getLock());
617 values.addArray (arrayToAddFrom, startIndex, numElementsToAdd);
629 jassert (targetNumItems >= 0);
630 auto numToAdd = targetNumItems - values.size();
634 else if (numToAdd < 0)
650 template <
class ElementComparator>
651 int addSorted (ElementComparator& comparator, ParameterType newElement)
654 auto index = findInsertIndexInSortedArray (comparator, values.begin(), newElement, 0, values.size());
655 insert (index, newElement);
670 DefaultElementComparator <ElementType> comparator;
686 template <
typename ElementComparator,
typename TargetValueType>
687 int indexOfSorted (ElementComparator& comparator, TargetValueType elementToLookFor)
const
689 ignoreUnused (comparator);
694 for (
int s = 0, e = values.size();;)
699 if (comparator.compareElements (elementToLookFor, values[s]) == 0)
702 auto halfway = (s + e) / 2;
707 if (comparator.compareElements (elementToLookFor, values[halfway]) >= 0)
728 if (isPositiveAndBelow (indexToRemove, values.size()))
729 removeInternal (indexToRemove);
746 if (isPositiveAndBelow (indexToRemove, values.size()))
748 ElementType removed (values[indexToRemove]);
749 removeInternal (indexToRemove);
753 return ElementType();
766 void remove (
const ElementType* elementToRemove)
768 jassert (elementToRemove !=
nullptr);
771 jassert (values.begin() !=
nullptr);
772 auto indexToRemove = (int) (elementToRemove - values.begin());
774 if (! isPositiveAndBelow (indexToRemove, values.size()))
780 removeInternal (indexToRemove);
794 auto* e = values.begin();
796 for (
int i = 0; i < values.size(); ++i)
798 if (valueToRemove == e[i])
820 for (
int i = values.size(); --i >= 0;)
822 if (valueToRemove == values[i])
843 template <
typename PredicateType>
849 for (
int i = values.size(); --i >= 0;)
851 if (predicate (values[i]))
877 auto endIndex = jlimit (0, values.size(), startIndex + numberToRemove);
878 startIndex = jlimit (0, values.size(), startIndex);
879 numberToRemove = endIndex - startIndex;
881 if (numberToRemove > 0)
883 values.removeElements (startIndex, numberToRemove);
884 minimiseStorageAfterRemoval();
895 jassert (howManyToRemove >= 0);
897 if (howManyToRemove > 0)
901 if (howManyToRemove > values.size())
902 howManyToRemove = values.size();
904 values.removeElements (values.size() - howManyToRemove, howManyToRemove);
905 minimiseStorageAfterRemoval();
914 template <
class OtherArrayType>
917 const typename OtherArrayType::ScopedLockType lock1 (otherArray.getLock());
920 if (
this == &otherArray)
926 if (otherArray.size() > 0)
928 for (
int i = values.size(); --i >= 0;)
929 if (otherArray.contains (values[i]))
942 template <
class OtherArrayType>
945 const typename OtherArrayType::ScopedLockType lock1 (otherArray.getLock());
948 if (
this != &otherArray)
950 if (otherArray.size() <= 0)
956 for (
int i = values.size(); --i >= 0;)
957 if (! otherArray.contains (values[i]))
971 void swap (
int index1,
int index2)
974 values.swap (index1, index2);
991 void move (
int currentIndex,
int newIndex)
noexcept
993 if (currentIndex != newIndex)
996 values.move (currentIndex, newIndex);
1010 values.shrinkToNoMoreThan (values.size());
1022 values.ensureAllocatedSize (minNumElements);
1062 template <
class ElementComparator>
1063 void sort (ElementComparator& comparator,
1064 bool retainOrderOfEquivalentItems =
false)
1067 ignoreUnused (comparator);
1069 sortArray (comparator, values.begin(), 0,
size() - 1, retainOrderOfEquivalentItems);
1077 inline const TypeOfCriticalSectionToUse&
getLock() const noexcept {
return values; }
1087 JUCE_DEPRECATED_WITH_BODY (
void swapWithArray (
Array& other)
noexcept, {
swapWith (other); })
1092 ArrayBase<ElementType, TypeOfCriticalSectionToUse> values;
1094 void removeInternal (
int indexToRemove)
1096 values.removeElements (indexToRemove, 1);
1097 minimiseStorageAfterRemoval();
1100 void minimiseStorageAfterRemoval()
1102 if (values.capacity() > jmax (minimumAllocatedSize, values.size() * 2))
1103 values.shrinkToNoMoreThan (jmax (values.size(), jmax (minimumAllocatedSize, 64 / (
int)
sizeof (ElementType))));
A basic object container.
Holds a resizable array of primitive or copy-by-value objects.
int removeIf(PredicateType &&predicate)
Removes items from the array.
Array(ElementType &&firstNewElement, OtherElements... otherElements)
Initalises an Array from a list of items.
bool operator==(const OtherArrayType &other) const
Compares this array to another one.
void swapWith(OtherArrayType &otherArray) noexcept
This swaps the contents of this array with those of another array.
void add(ElementType &&newElement)
Appends a new element at the end of the array.
void setUnchecked(int indexToChange, ParameterType newValue)
Replaces an element with a new value without doing any bounds-checking.
bool operator!=(const OtherArrayType &other) const
Compares this array to another one.
typename TypeOfCriticalSectionToUse::ScopedLockType ScopedLockType
Returns the type of scoped lock to use for locking this array.
void addNullTerminatedArray(const Type *const *elementsToAdd)
Adds elements from a null-terminated array of pointers to the end of this array.
ElementType getUnchecked(int index) const
Returns one of the elements in the array, without checking the index passed in.
void insertArray(int indexToInsertAt, const ElementType *newElements, int numberOfElements)
Inserts an array of values into this array at a given position.
bool isEmpty() const noexcept
Returns true if the array is empty, false otherwise.
void removeLast(int howManyToRemove=1)
Removes the last n elements from the array.
void ensureStorageAllocated(int minNumElements)
Increases the array's internal storage to hold a minimum number of elements.
void remove(const ElementType *elementToRemove)
Removes an element from the array.
const TypeOfCriticalSectionToUse & getLock() const noexcept
Returns the CriticalSection that locks this array.
void addArray(const Type *elementsToAdd, int numElementsToAdd)
Adds elements from an array to the end of this array.
Array(const TypeToCreateFrom *data)
Initalises from a null-terminated raw array of values.
void clearQuick()
Removes all elements from the array without freeing the array's allocated storage.
void sort()
Sorts the array using a default comparison operation.
int removeAllInstancesOf(ParameterType valueToRemove)
Removes items from the array.
Array(const TypeToCreateFrom *data, int numValues)
Initalises from a raw array of values.
int size() const noexcept
Returns the current number of elements in the array.
void removeFirstMatchingValue(ParameterType valueToRemove)
Removes an item from the array.
void fill(const ParameterType &newValue) noexcept
Fills the Array with the provided value.
void removeRange(int startIndex, int numberToRemove)
Removes a range of elements from the array.
void removeValuesIn(const OtherArrayType &otherArray)
Removes any elements which are also in another array.
ElementType * begin() const noexcept
Returns a pointer to the first element in the array.
void add(const ElementType &firstNewElement, OtherElements... otherElements)
Appends multiple new elements at the end of the array.
void remove(int indexToRemove)
Removes an element from the array.
void insert(int indexToInsertAt, ParameterType newElement)
Inserts a new element into the array at a given position.
int indexOfSorted(ElementComparator &comparator, TargetValueType elementToLookFor) const
Finds the index of an element in the array, assuming that the array is sorted.
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.
Array(ElementType &&singleElementToAdd)
Initalises an Array of size 1 containing a single element.
void addUsingDefaultSort(ParameterType newElement)
Inserts a new element into the array, assuming that the array is sorted.
Array()=default
Creates an empty array.
int indexOf(ParameterType elementToLookFor) const
Finds the index of the first element which matches the value passed in.
void add(const ElementType &newElement)
Appends a new element at the end of the array.
ElementType removeAndReturn(int indexToRemove)
Removes an element from the array.
Array(const ElementType &firstNewElement, OtherElements... otherElements)
Initalises an Array from a list of items.
ElementType operator[](int index) const
Returns one of the elements in the array.
Array(const ElementType &singleElementToAdd)
Initalises an Array of size 1 containing a single element.
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 set(int indexToChange, ParameterType newValue)
Replaces an element with a new value.
int addSorted(ElementComparator &comparator, ParameterType newElement)
Inserts a new element into the array, assuming that the array is sorted.
bool contains(ParameterType elementToLookFor) const
Returns true if the array contains at least one occurrence of an object.
void insertMultiple(int indexToInsertAt, ParameterType newElement, int numberOfTimesToInsertIt)
Inserts multiple copies of an element into the array at a given position.
void sort(ElementComparator &comparator, bool retainOrderOfEquivalentItems=false)
Sorts the elements in the array.
~Array()=default
Destructor.
void resize(int targetNumItems)
This will enlarge or shrink the array to the given number of elements, by adding or removing items fr...
void clear()
Removes all elements from the array.
void move(int currentIndex, int newIndex) noexcept
Moves one of the values to a different position.
void swap(int index1, int index2)
Swaps over two elements in the array.
void removeValuesNotIn(const OtherArrayType &otherArray)
Removes any elements which are not found in another array.
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.
ElementType * data() const noexcept
Returns a pointer to the first element in the array.
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 Array &other)
Creates a copy of another array.
void minimiseStorageOverheads()
Reduces the amount of storage being used by the array.
void addArray(const OtherArrayType &arrayToAddFrom)
Adds elements from another array to the end of this array.
Array & operator=(const Array &other)
Copies another array.
ElementType getLast() const noexcept
Returns the last element in the array, or a default value if the array is empty.
void add(ElementType &&firstNewElement, OtherElements... otherElements)
Appends multiple new elements at the end of the array.
ElementType * end() const noexcept
Returns a pointer to the element which follows the last element in the array.
A simple ElementComparator class that can be used to sort an array of objects that support the '<' op...