From 108d79d8d9c7ecb53f922a0a3538f412a2d85bb2 Mon Sep 17 00:00:00 2001 From: jules Date: Thu, 7 Jun 2018 09:44:09 +0100 Subject: [PATCH] Made Array::addArray() more flexible, so it can read from other array classes --- modules/juce_core/containers/juce_Array.h | 50 +++++++++++++++-------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/modules/juce_core/containers/juce_Array.h b/modules/juce_core/containers/juce_Array.h index f3911dba4e..12df635ccd 100644 --- a/modules/juce_core/containers/juce_Array.h +++ b/modules/juce_core/containers/juce_Array.h @@ -183,11 +183,13 @@ public: const ScopedLockType lock (getLock()); const typename OtherArrayType::ScopedLockType lock2 (other.getLock()); - if (numUsed != other.numUsed) + if (size() != other.size()) 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 true; @@ -262,7 +264,7 @@ public: @param index the index of the element being requested (0 is the first element in the array) @see getUnchecked, getFirst, getLast */ - ElementType operator[] (const int index) const + ElementType operator[] (int index) const { const ScopedLockType lock (getLock()); @@ -272,7 +274,7 @@ public: return data.elements[index]; } - return ElementType(); + return {}; } /** 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) @see operator[], getFirst, getLast */ - inline ElementType getUnchecked (const int index) const + inline ElementType getUnchecked (int index) const { const ScopedLockType lock (getLock()); 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) @see operator[], getFirst, getLast */ - inline ElementType& getReference (const int index) const noexcept + inline ElementType& getReference (int index) const noexcept { const ScopedLockType lock (getLock()); jassert (isPositiveAndBelow (index, numUsed) && data.elements != nullptr); @@ -320,7 +322,7 @@ public: return data.elements[0]; } - return ElementType(); + return {}; } /** 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 ElementType(); + return {}; } /** Returns a pointer to the actual array data. @@ -599,7 +601,7 @@ public: @param newValue the new value to set for this index. @see add, insert */ - void set (const int indexToChange, ParameterType newValue) + void set (int indexToChange, ParameterType newValue) { jassert (indexToChange >= 0); const ScopedLockType lock (getLock()); @@ -625,7 +627,7 @@ public: @param newValue the new value to set for this index. @see set, getUnchecked */ - void setUnchecked (const int indexToChange, ParameterType newValue) + void setUnchecked (int indexToChange, ParameterType newValue) { const ScopedLockType lock (getLock()); jassert (isPositiveAndBelow (indexToChange, numUsed)); @@ -700,6 +702,22 @@ public: 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 + 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. @param arrayToAddFrom the array from which to copy the elements @@ -711,7 +729,7 @@ public: */ template void addArray (const OtherArrayType& arrayToAddFrom, - int startIndex = 0, + int startIndex, int numElementsToAdd = -1) { 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 removed from its end to shorten it. */ - void resize (const int targetNumItems) + void resize (int targetNumItems) { jassert (targetNumItems >= 0); auto numToAdd = targetNumItems - numUsed; @@ -1174,7 +1192,7 @@ public: the array won't have to keep dynamically resizing itself as the elements are added, and it'll therefore be more efficient. */ - void ensureStorageAllocated (const int minNumElements) + void ensureStorageAllocated (int minNumElements) { const ScopedLockType lock (getLock()); data.ensureAllocatedSize (minNumElements); @@ -1219,7 +1237,7 @@ public: */ template void sort (ElementComparator& comparator, - const bool retainOrderOfEquivalentItems = false) + bool retainOrderOfEquivalentItems = false) { const ScopedLockType lock (getLock()); ignoreUnused (comparator); // if you pass in an object with a static compareElements() method, this @@ -1250,7 +1268,7 @@ private: ArrayAllocationBase data; int numUsed = 0; - void removeInternal (const int indexToRemove) + void removeInternal (int indexToRemove) { --numUsed; auto* e = data.elements + indexToRemove;