Browse Source

Made Array::addArray() more flexible, so it can read from other array classes

tags/2021-05-28
jules 7 years ago
parent
commit
108d79d8d9
1 changed files with 34 additions and 16 deletions
  1. +34
    -16
      modules/juce_core/containers/juce_Array.h

+ 34
- 16
modules/juce_core/containers/juce_Array.h View File

@@ -183,11 +183,13 @@ public:
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
const typename OtherArrayType::ScopedLockType lock2 (other.getLock()); const typename OtherArrayType::ScopedLockType lock2 (other.getLock());
if (numUsed != other.numUsed)
if (size() != other.size())
return false; return false;
for (int i = numUsed; --i >= 0;)
if (! (data.elements[i] == other.data.elements[i]))
auto e = begin();
for (auto& o : other)
if (! (*e++ == o))
return false; return false;
return true; return true;
@@ -262,7 +264,7 @@ public:
@param index the index of the element being requested (0 is the first element in the array) @param index the index of the element being requested (0 is the first element in the array)
@see getUnchecked, getFirst, getLast @see getUnchecked, getFirst, getLast
*/ */
ElementType operator[] (const int index) const
ElementType operator[] (int index) const
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
@@ -272,7 +274,7 @@ public:
return data.elements[index]; return data.elements[index];
} }
return ElementType();
return {};
} }
/** Returns one of the elements in the array, without checking the index passed in. /** Returns one of the elements in the array, without checking the index passed in.
@@ -284,7 +286,7 @@ public:
@param index the index of the element being requested (0 is the first element in the array) @param index the index of the element being requested (0 is the first element in the array)
@see operator[], getFirst, getLast @see operator[], getFirst, getLast
*/ */
inline ElementType getUnchecked (const int index) const
inline ElementType getUnchecked (int index) const
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
jassert (isPositiveAndBelow (index, numUsed) && data.elements != nullptr); jassert (isPositiveAndBelow (index, numUsed) && data.elements != nullptr);
@@ -300,7 +302,7 @@ public:
@param index the index of the element being requested (0 is the first element in the array) @param index the index of the element being requested (0 is the first element in the array)
@see operator[], getFirst, getLast @see operator[], getFirst, getLast
*/ */
inline ElementType& getReference (const int index) const noexcept
inline ElementType& getReference (int index) const noexcept
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
jassert (isPositiveAndBelow (index, numUsed) && data.elements != nullptr); jassert (isPositiveAndBelow (index, numUsed) && data.elements != nullptr);
@@ -320,7 +322,7 @@ public:
return data.elements[0]; return data.elements[0];
} }
return ElementType();
return {};
} }
/** Returns the last element in the array, or a default value if the array is empty. /** Returns the last element in the array, or a default value if the array is empty.
@@ -337,7 +339,7 @@ public:
return data.elements[numUsed - 1]; return data.elements[numUsed - 1];
} }
return ElementType();
return {};
} }
/** Returns a pointer to the actual array data. /** Returns a pointer to the actual array data.
@@ -599,7 +601,7 @@ public:
@param newValue the new value to set for this index. @param newValue the new value to set for this index.
@see add, insert @see add, insert
*/ */
void set (const int indexToChange, ParameterType newValue)
void set (int indexToChange, ParameterType newValue)
{ {
jassert (indexToChange >= 0); jassert (indexToChange >= 0);
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
@@ -625,7 +627,7 @@ public:
@param newValue the new value to set for this index. @param newValue the new value to set for this index.
@see set, getUnchecked @see set, getUnchecked
*/ */
void setUnchecked (const int indexToChange, ParameterType newValue)
void setUnchecked (int indexToChange, ParameterType newValue)
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
jassert (isPositiveAndBelow (indexToChange, numUsed)); jassert (isPositiveAndBelow (indexToChange, numUsed));
@@ -700,6 +702,22 @@ public:
std::swap (numUsed, otherArray.numUsed); std::swap (numUsed, otherArray.numUsed);
} }
/** Adds elements from another array to the end of this array.
@param arrayToAddFrom the array from which to copy the elements
@see add
*/
template <class OtherArrayType>
void addArray (const OtherArrayType& arrayToAddFrom)
{
const typename OtherArrayType::ScopedLockType lock1 (arrayToAddFrom.getLock());
const ScopedLockType lock2 (getLock());
data.ensureAllocatedSize (numUsed + arrayToAddFrom.size());
for (auto& e : arrayToAddFrom)
addAssumingCapacityIsReady (e);
}
/** Adds elements from another array to the end of this array. /** Adds elements from another array to the end of this array.
@param arrayToAddFrom the array from which to copy the elements @param arrayToAddFrom the array from which to copy the elements
@@ -711,7 +729,7 @@ public:
*/ */
template <class OtherArrayType> template <class OtherArrayType>
void addArray (const OtherArrayType& arrayToAddFrom, void addArray (const OtherArrayType& arrayToAddFrom,
int startIndex = 0,
int startIndex,
int numElementsToAdd = -1) int numElementsToAdd = -1)
{ {
const typename OtherArrayType::ScopedLockType lock1 (arrayToAddFrom.getLock()); const typename OtherArrayType::ScopedLockType lock1 (arrayToAddFrom.getLock());
@@ -742,7 +760,7 @@ public:
until its size is as specified. If its size is larger than the target, items will be until its size is as specified. If its size is larger than the target, items will be
removed from its end to shorten it. removed from its end to shorten it.
*/ */
void resize (const int targetNumItems)
void resize (int targetNumItems)
{ {
jassert (targetNumItems >= 0); jassert (targetNumItems >= 0);
auto numToAdd = targetNumItems - numUsed; auto numToAdd = targetNumItems - numUsed;
@@ -1174,7 +1192,7 @@ public:
the array won't have to keep dynamically resizing itself as the elements the array won't have to keep dynamically resizing itself as the elements
are added, and it'll therefore be more efficient. are added, and it'll therefore be more efficient.
*/ */
void ensureStorageAllocated (const int minNumElements)
void ensureStorageAllocated (int minNumElements)
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
data.ensureAllocatedSize (minNumElements); data.ensureAllocatedSize (minNumElements);
@@ -1219,7 +1237,7 @@ public:
*/ */
template <class ElementComparator> template <class ElementComparator>
void sort (ElementComparator& comparator, void sort (ElementComparator& comparator,
const bool retainOrderOfEquivalentItems = false)
bool retainOrderOfEquivalentItems = false)
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
ignoreUnused (comparator); // if you pass in an object with a static compareElements() method, this ignoreUnused (comparator); // if you pass in an object with a static compareElements() method, this
@@ -1250,7 +1268,7 @@ private:
ArrayAllocationBase <ElementType, TypeOfCriticalSectionToUse> data; ArrayAllocationBase <ElementType, TypeOfCriticalSectionToUse> data;
int numUsed = 0; int numUsed = 0;
void removeInternal (const int indexToRemove)
void removeInternal (int indexToRemove)
{ {
--numUsed; --numUsed;
auto* e = data.elements + indexToRemove; auto* e = data.elements + indexToRemove;


Loading…
Cancel
Save