2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCE_ARRAY_JUCEHEADER__
27 #define __JUCE_ARRAY_JUCEHEADER__
29 #include "juce_ArrayAllocationBase.h"
30 #include "juce_ElementComparator.h"
31 #include "../threads/juce_CriticalSection.h"
34 //==============================================================================
36 Holds a resizable array of primitive or copy-by-value objects.
38 Examples of arrays are: Array<int>, Array<Rectangle> or Array<MyClass*>
40 The Array class can be used to hold simple, non-polymorphic objects as well as primitive types - to
41 do so, the class must fulfil these requirements:
42 - it must have a copy constructor and assignment operator
43 - it must be able to be relocated in memory by a memcpy without this causing any problems - so
44 objects whose functionality relies on external pointers or references to themselves can be used.
46 You can of course have an array of pointers to any kind of object, e.g. Array <MyClass*>, but if
47 you do this, the array doesn't take any ownership of the objects - see the OwnedArray class or the
48 ReferenceCountedArray class for more powerful ways of holding lists of objects.
50 For holding lists of strings, you can use Array\<String\>, but it's usually better to use the
51 specialised class StringArray, which provides more useful functions.
53 To make all the array's methods thread-safe, pass in "CriticalSection" as the templated
54 TypeOfCriticalSectionToUse parameter, instead of the default DummyCriticalSection.
56 @see OwnedArray, ReferenceCountedArray, StringArray, CriticalSection
58 template <typename ElementType
,
59 typename TypeOfCriticalSectionToUse
= DummyCriticalSection
>
63 typedef PARAMETER_TYPE (ElementType
) ParameterType
;
66 //==============================================================================
67 /** Creates an empty array. */
73 /** Creates a copy of another array.
74 @param other the array to copy
76 Array (const Array
<ElementType
, TypeOfCriticalSectionToUse
>& other
)
78 const ScopedLockType
lock (other
.getLock());
79 numUsed
= other
.numUsed
;
80 data
.setAllocatedSize (other
.numUsed
);
82 for (int i
= 0; i
< numUsed
; ++i
)
83 new (data
.elements
+ i
) ElementType (other
.data
.elements
[i
]);
86 /** Initalises from a null-terminated C array of values.
88 @param values the array to copy from
90 template <typename TypeToCreateFrom
>
91 explicit Array (const TypeToCreateFrom
* values
)
94 while (*values
!= TypeToCreateFrom())
98 /** Initalises from a C array of values.
100 @param values the array to copy from
101 @param numValues the number of values in the array
103 template <typename TypeToCreateFrom
>
104 Array (const TypeToCreateFrom
* values
, int numValues
)
105 : numUsed (numValues
)
107 data
.setAllocatedSize (numValues
);
109 for (int i
= 0; i
< numValues
; ++i
)
110 new (data
.elements
+ i
) ElementType (values
[i
]);
116 for (int i
= 0; i
< numUsed
; ++i
)
117 data
.elements
[i
].~ElementType();
120 /** Copies another array.
121 @param other the array to copy
123 Array
& operator= (const Array
& other
)
127 Array
<ElementType
, TypeOfCriticalSectionToUse
> otherCopy (other
);
128 swapWithArray (otherCopy
);
134 //==============================================================================
135 /** Compares this array to another one.
136 Two arrays are considered equal if they both contain the same set of
137 elements, in the same order.
138 @param other the other array to compare with
140 template <class OtherArrayType
>
141 bool operator== (const OtherArrayType
& other
) const
143 const ScopedLockType
lock (getLock());
144 const typename
OtherArrayType::ScopedLockType
lock2 (other
.getLock());
146 if (numUsed
!= other
.numUsed
)
149 for (int i
= numUsed
; --i
>= 0;)
150 if (! (data
.elements
[i
] == other
.data
.elements
[i
]))
156 /** Compares this array to another one.
157 Two arrays are considered equal if they both contain the same set of
158 elements, in the same order.
159 @param other the other array to compare with
161 template <class OtherArrayType
>
162 bool operator!= (const OtherArrayType
& other
) const
164 return ! operator== (other
);
167 //==============================================================================
168 /** Removes all elements from the array.
169 This will remove all the elements, and free any storage that the array is
170 using. To clear the array without freeing the storage, use the clearQuick()
177 const ScopedLockType
lock (getLock());
179 for (int i
= 0; i
< numUsed
; ++i
)
180 data
.elements
[i
].~ElementType();
182 data
.setAllocatedSize (0);
186 /** Removes all elements from the array without freeing the array's allocated storage.
192 const ScopedLockType
lock (getLock());
194 for (int i
= 0; i
< numUsed
; ++i
)
195 data
.elements
[i
].~ElementType();
200 //==============================================================================
201 /** Returns the current number of elements in the array.
203 inline int size() const noexcept
208 /** Returns one of the elements in the array.
209 If the index passed in is beyond the range of valid elements, this
212 If you're certain that the index will always be a valid element, you
213 can call getUnchecked() instead, which is faster.
215 @param index the index of the element being requested (0 is the first element in the array)
216 @see getUnchecked, getFirst, getLast
218 const ElementType
operator[] (const int index
) const
220 const ScopedLockType
lock (getLock());
221 return isPositiveAndBelow (index
, numUsed
) ? data
.elements
[index
]
225 /** Returns one of the elements in the array, without checking the index passed in.
227 Unlike the operator[] method, this will try to return an element without
228 checking that the index is within the bounds of the array, so should only
229 be used when you're confident that it will always be a valid index.
231 @param index the index of the element being requested (0 is the first element in the array)
232 @see operator[], getFirst, getLast
234 inline const ElementType
getUnchecked (const int index
) const
236 const ScopedLockType
lock (getLock());
237 jassert (isPositiveAndBelow (index
, numUsed
));
238 return data
.elements
[index
];
241 /** Returns a direct reference to one of the elements in the array, without checking the index passed in.
243 This is like getUnchecked, but returns a direct reference to the element, so that
244 you can alter it directly. Obviously this can be dangerous, so only use it when
245 absolutely necessary.
247 @param index the index of the element being requested (0 is the first element in the array)
248 @see operator[], getFirst, getLast
250 inline ElementType
& getReference (const int index
) const noexcept
252 const ScopedLockType
lock (getLock());
253 jassert (isPositiveAndBelow (index
, numUsed
));
254 return data
.elements
[index
];
257 /** Returns the first element in the array, or 0 if the array is empty.
259 @see operator[], getUnchecked, getLast
261 inline ElementType
getFirst() const
263 const ScopedLockType
lock (getLock());
264 return (numUsed
> 0) ? data
.elements
[0]
268 /** Returns the last element in the array, or 0 if the array is empty.
270 @see operator[], getUnchecked, getFirst
272 inline ElementType
getLast() const
274 const ScopedLockType
lock (getLock());
275 return (numUsed
> 0) ? data
.elements
[numUsed
- 1]
279 /** Returns a pointer to the actual array data.
280 This pointer will only be valid until the next time a non-const method
281 is called on the array.
283 inline ElementType
* getRawDataPointer() noexcept
285 return data
.elements
;
288 //==============================================================================
289 /** Returns a pointer to the first element in the array.
290 This method is provided for compatibility with standard C++ iteration mechanisms.
292 inline ElementType
* begin() const noexcept
294 return data
.elements
;
297 /** Returns a pointer to the element which follows the last element in the array.
298 This method is provided for compatibility with standard C++ iteration mechanisms.
300 inline ElementType
* end() const noexcept
302 return data
.elements
+ numUsed
;
305 //==============================================================================
306 /** Finds the index of the first element which matches the value passed in.
308 This will search the array for the given object, and return the index
309 of its first occurrence. If the object isn't found, the method will return -1.
311 @param elementToLookFor the value or object to look for
312 @returns the index of the object, or -1 if it's not found
314 int indexOf (ParameterType elementToLookFor
) const
316 const ScopedLockType
lock (getLock());
317 const ElementType
* e
= data
.elements
.getData();
318 const ElementType
* const end_
= e
+ numUsed
;
320 for (; e
!= end_
; ++e
)
321 if (elementToLookFor
== *e
)
322 return static_cast <int> (e
- data
.elements
.getData());
327 /** Returns true if the array contains at least one occurrence of an object.
329 @param elementToLookFor the value or object to look for
330 @returns true if the item is found
332 bool contains (ParameterType elementToLookFor
) const
334 const ScopedLockType
lock (getLock());
335 const ElementType
* e
= data
.elements
.getData();
336 const ElementType
* const end_
= e
+ numUsed
;
338 for (; e
!= end_
; ++e
)
339 if (elementToLookFor
== *e
)
345 //==============================================================================
346 /** Appends a new element at the end of the array.
348 @param newElement the new object to add to the array
349 @see set, insert, addIfNotAlreadyThere, addSorted, addUsingDefaultSort, addArray
351 void add (ParameterType newElement
)
353 const ScopedLockType
lock (getLock());
354 data
.ensureAllocatedSize (numUsed
+ 1);
355 new (data
.elements
+ numUsed
++) ElementType (newElement
);
358 /** Inserts a new element into the array at a given position.
360 If the index is less than 0 or greater than the size of the array, the
361 element will be added to the end of the array.
362 Otherwise, it will be inserted into the array, moving all the later elements
365 @param indexToInsertAt the index at which the new element should be
366 inserted (pass in -1 to add it to the end)
367 @param newElement the new object to add to the array
368 @see add, addSorted, addUsingDefaultSort, set
370 void insert (int indexToInsertAt
, ParameterType newElement
)
372 const ScopedLockType
lock (getLock());
373 data
.ensureAllocatedSize (numUsed
+ 1);
375 if (isPositiveAndBelow (indexToInsertAt
, numUsed
))
377 ElementType
* const insertPos
= data
.elements
+ indexToInsertAt
;
378 const int numberToMove
= numUsed
- indexToInsertAt
;
380 if (numberToMove
> 0)
381 memmove (insertPos
+ 1, insertPos
, numberToMove
* sizeof (ElementType
));
383 new (insertPos
) ElementType (newElement
);
388 new (data
.elements
+ numUsed
++) ElementType (newElement
);
392 /** Inserts multiple copies of an element into the array at a given position.
394 If the index is less than 0 or greater than the size of the array, the
395 element will be added to the end of the array.
396 Otherwise, it will be inserted into the array, moving all the later elements
399 @param indexToInsertAt the index at which the new element should be inserted
400 @param newElement the new object to add to the array
401 @param numberOfTimesToInsertIt how many copies of the value to insert
402 @see insert, add, addSorted, set
404 void insertMultiple (int indexToInsertAt
, ParameterType newElement
,
405 int numberOfTimesToInsertIt
)
407 if (numberOfTimesToInsertIt
> 0)
409 const ScopedLockType
lock (getLock());
410 data
.ensureAllocatedSize (numUsed
+ numberOfTimesToInsertIt
);
411 ElementType
* insertPos
;
413 if (isPositiveAndBelow (indexToInsertAt
, numUsed
))
415 insertPos
= data
.elements
+ indexToInsertAt
;
416 const int numberToMove
= numUsed
- indexToInsertAt
;
417 memmove (insertPos
+ numberOfTimesToInsertIt
, insertPos
, numberToMove
* sizeof (ElementType
));
421 insertPos
= data
.elements
+ numUsed
;
424 numUsed
+= numberOfTimesToInsertIt
;
426 while (--numberOfTimesToInsertIt
>= 0)
427 new (insertPos
++) ElementType (newElement
);
431 /** Inserts an array of values into this array at a given position.
433 If the index is less than 0 or greater than the size of the array, the
434 new elements will be added to the end of the array.
435 Otherwise, they will be inserted into the array, moving all the later elements
438 @param indexToInsertAt the index at which the first new element should be inserted
439 @param newElements the new values to add to the array
440 @param numberOfElements how many items are in the array
441 @see insert, add, addSorted, set
443 void insertArray (int indexToInsertAt
,
444 const ElementType
* newElements
,
445 int numberOfElements
)
447 if (numberOfElements
> 0)
449 const ScopedLockType
lock (getLock());
450 data
.ensureAllocatedSize (numUsed
+ numberOfElements
);
451 ElementType
* insertPos
;
453 if (isPositiveAndBelow (indexToInsertAt
, numUsed
))
455 insertPos
= data
.elements
+ indexToInsertAt
;
456 const int numberToMove
= numUsed
- indexToInsertAt
;
457 memmove (insertPos
+ numberOfElements
, insertPos
, numberToMove
* sizeof (ElementType
));
461 insertPos
= data
.elements
+ numUsed
;
464 numUsed
+= numberOfElements
;
466 while (--numberOfElements
>= 0)
467 new (insertPos
++) ElementType (*newElements
++);
471 /** Appends a new element at the end of the array as long as the array doesn't
474 If the array already contains an element that matches the one passed in, nothing
477 @param newElement the new object to add to the array
479 void addIfNotAlreadyThere (ParameterType newElement
)
481 const ScopedLockType
lock (getLock());
483 if (! contains (newElement
))
487 /** Replaces an element with a new value.
489 If the index is less than zero, this method does nothing.
490 If the index is beyond the end of the array, the item is added to the end of the array.
492 @param indexToChange the index whose value you want to change
493 @param newValue the new value to set for this index.
496 void set (const int indexToChange
, ParameterType newValue
)
498 jassert (indexToChange
>= 0);
499 const ScopedLockType
lock (getLock());
501 if (isPositiveAndBelow (indexToChange
, numUsed
))
503 data
.elements
[indexToChange
] = newValue
;
505 else if (indexToChange
>= 0)
507 data
.ensureAllocatedSize (numUsed
+ 1);
508 new (data
.elements
+ numUsed
++) ElementType (newValue
);
512 /** Replaces an element with a new value without doing any bounds-checking.
514 This just sets a value directly in the array's internal storage, so you'd
515 better make sure it's in range!
517 @param indexToChange the index whose value you want to change
518 @param newValue the new value to set for this index.
519 @see set, getUnchecked
521 void setUnchecked (const int indexToChange
, ParameterType newValue
)
523 const ScopedLockType
lock (getLock());
524 jassert (isPositiveAndBelow (indexToChange
, numUsed
));
525 data
.elements
[indexToChange
] = newValue
;
528 /** Adds elements from an array to the end of this array.
530 @param elementsToAdd the array of elements to add
531 @param numElementsToAdd how many elements are in this other array
534 void addArray (const ElementType
* elementsToAdd
, int numElementsToAdd
)
536 const ScopedLockType
lock (getLock());
538 if (numElementsToAdd
> 0)
540 data
.ensureAllocatedSize (numUsed
+ numElementsToAdd
);
542 while (--numElementsToAdd
>= 0)
544 new (data
.elements
+ numUsed
) ElementType (*elementsToAdd
++);
550 /** This swaps the contents of this array with those of another array.
552 If you need to exchange two arrays, this is vastly quicker than using copy-by-value
553 because it just swaps their internal pointers.
555 void swapWithArray (Array
& otherArray
) noexcept
557 const ScopedLockType
lock1 (getLock());
558 const ScopedLockType
lock2 (otherArray
.getLock());
560 data
.swapWith (otherArray
.data
);
561 swapVariables (numUsed
, otherArray
.numUsed
);
564 /** Adds elements from another array to the end of this array.
566 @param arrayToAddFrom the array from which to copy the elements
567 @param startIndex the first element of the other array to start copying from
568 @param numElementsToAdd how many elements to add from the other array. If this
569 value is negative or greater than the number of available elements,
570 all available elements will be copied.
573 template <class OtherArrayType
>
574 void addArray (const OtherArrayType
& arrayToAddFrom
,
576 int numElementsToAdd
= -1)
578 const typename
OtherArrayType::ScopedLockType
lock1 (arrayToAddFrom
.getLock());
581 const ScopedLockType
lock2 (getLock());
589 if (numElementsToAdd
< 0 || startIndex
+ numElementsToAdd
> arrayToAddFrom
.size())
590 numElementsToAdd
= arrayToAddFrom
.size() - startIndex
;
592 while (--numElementsToAdd
>= 0)
593 add (arrayToAddFrom
.getUnchecked (startIndex
++));
597 /** This will enlarge or shrink the array to the given number of elements, by adding
598 or removing items from its end.
600 If the array is smaller than the given target size, empty elements will be appended
601 until its size is as specified. If its size is larger than the target, items will be
602 removed from its end to shorten it.
604 void resize (const int targetNumItems
)
606 jassert (targetNumItems
>= 0);
608 const int numToAdd
= targetNumItems
- numUsed
;
610 insertMultiple (numUsed
, ElementType(), numToAdd
);
611 else if (numToAdd
< 0)
612 removeRange (targetNumItems
, -numToAdd
);
615 /** Inserts a new element into the array, assuming that the array is sorted.
617 This will use a comparator to find the position at which the new element
618 should go. If the array isn't sorted, the behaviour of this
619 method will be unpredictable.
621 @param comparator the comparator to use to compare the elements - see the sort()
622 method for details about the form this object should take
623 @param newElement the new element to insert to the array
624 @returns the index at which the new item was added
625 @see addUsingDefaultSort, add, sort
627 template <class ElementComparator
>
628 int addSorted (ElementComparator
& comparator
, ParameterType newElement
)
630 const ScopedLockType
lock (getLock());
631 const int index
= findInsertIndexInSortedArray (comparator
, data
.elements
.getData(), newElement
, 0, numUsed
);
632 insert (index
, newElement
);
636 /** Inserts a new element into the array, assuming that the array is sorted.
638 This will use the DefaultElementComparator class for sorting, so your ElementType
639 must be suitable for use with that class. If the array isn't sorted, the behaviour of this
640 method will be unpredictable.
642 @param newElement the new element to insert to the array
645 void addUsingDefaultSort (ParameterType newElement
)
647 DefaultElementComparator
<ElementType
> comparator
;
648 addSorted (comparator
, newElement
);
651 /** Finds the index of an element in the array, assuming that the array is sorted.
653 This will use a comparator to do a binary-chop to find the index of the given
654 element, if it exists. If the array isn't sorted, the behaviour of this
655 method will be unpredictable.
657 @param comparator the comparator to use to compare the elements - see the sort()
658 method for details about the form this object should take
659 @param elementToLookFor the element to search for
660 @returns the index of the element, or -1 if it's not found
663 template <class ElementComparator
>
664 int indexOfSorted (ElementComparator
& comparator
, ParameterType elementToLookFor
) const
666 (void) comparator
; // if you pass in an object with a static compareElements() method, this
667 // avoids getting warning messages about the parameter being unused
669 const ScopedLockType
lock (getLock());
679 else if (comparator
.compareElements (elementToLookFor
, data
.elements
[start
]) == 0)
685 const int halfway
= (start
+ end_
) >> 1;
687 if (halfway
== start
)
689 else if (comparator
.compareElements (elementToLookFor
, data
.elements
[halfway
]) >= 0)
697 //==============================================================================
698 /** Removes an element from the array.
700 This will remove the element at a given index, and move back
701 all the subsequent elements to close the gap.
702 If the index passed in is out-of-range, nothing will happen.
704 @param indexToRemove the index of the element to remove
705 @returns the element that has been removed
706 @see removeValue, removeRange
708 ElementType
remove (const int indexToRemove
)
710 const ScopedLockType
lock (getLock());
712 if (isPositiveAndBelow (indexToRemove
, numUsed
))
716 ElementType
* const e
= data
.elements
+ indexToRemove
;
717 ElementType
removed (*e
);
719 const int numberToShift
= numUsed
- indexToRemove
;
721 if (numberToShift
> 0)
722 memmove (e
, e
+ 1, numberToShift
* sizeof (ElementType
));
724 if ((numUsed
<< 1) < data
.numAllocated
)
725 minimiseStorageOverheads();
731 return ElementType();
735 /** Removes an item from the array.
737 This will remove the first occurrence of the given element from the array.
738 If the item isn't found, no action is taken.
740 @param valueToRemove the object to try to remove
741 @see remove, removeRange
743 void removeValue (ParameterType valueToRemove
)
745 const ScopedLockType
lock (getLock());
746 ElementType
* const e
= data
.elements
;
748 for (int i
= 0; i
< numUsed
; ++i
)
750 if (valueToRemove
== e
[i
])
758 /** Removes a range of elements from the array.
760 This will remove a set of elements, starting from the given index,
761 and move subsequent elements down to close the gap.
763 If the range extends beyond the bounds of the array, it will
764 be safely clipped to the size of the array.
766 @param startIndex the index of the first element to remove
767 @param numberToRemove how many elements should be removed
768 @see remove, removeValue
770 void removeRange (int startIndex
, int numberToRemove
)
772 const ScopedLockType
lock (getLock());
773 const int endIndex
= jlimit (0, numUsed
, startIndex
+ numberToRemove
);
774 startIndex
= jlimit (0, numUsed
, startIndex
);
776 if (endIndex
> startIndex
)
778 ElementType
* const e
= data
.elements
+ startIndex
;
780 numberToRemove
= endIndex
- startIndex
;
781 for (int i
= 0; i
< numberToRemove
; ++i
)
784 const int numToShift
= numUsed
- endIndex
;
786 memmove (e
, e
+ numberToRemove
, numToShift
* sizeof (ElementType
));
788 numUsed
-= numberToRemove
;
790 if ((numUsed
<< 1) < data
.numAllocated
)
791 minimiseStorageOverheads();
795 /** Removes the last n elements from the array.
797 @param howManyToRemove how many elements to remove from the end of the array
798 @see remove, removeValue, removeRange
800 void removeLast (int howManyToRemove
= 1)
802 const ScopedLockType
lock (getLock());
804 if (howManyToRemove
> numUsed
)
805 howManyToRemove
= numUsed
;
807 for (int i
= 1; i
<= howManyToRemove
; ++i
)
808 data
.elements
[numUsed
- i
].~ElementType();
810 numUsed
-= howManyToRemove
;
812 if ((numUsed
<< 1) < data
.numAllocated
)
813 minimiseStorageOverheads();
816 /** Removes any elements which are also in another array.
818 @param otherArray the other array in which to look for elements to remove
819 @see removeValuesNotIn, remove, removeValue, removeRange
821 template <class OtherArrayType
>
822 void removeValuesIn (const OtherArrayType
& otherArray
)
824 const typename
OtherArrayType::ScopedLockType
lock1 (otherArray
.getLock());
825 const ScopedLockType
lock2 (getLock());
827 if (this == &otherArray
)
833 if (otherArray
.size() > 0)
835 for (int i
= numUsed
; --i
>= 0;)
836 if (otherArray
.contains (data
.elements
[i
]))
842 /** Removes any elements which are not found in another array.
844 Only elements which occur in this other array will be retained.
846 @param otherArray the array in which to look for elements NOT to remove
847 @see removeValuesIn, remove, removeValue, removeRange
849 template <class OtherArrayType
>
850 void removeValuesNotIn (const OtherArrayType
& otherArray
)
852 const typename
OtherArrayType::ScopedLockType
lock1 (otherArray
.getLock());
853 const ScopedLockType
lock2 (getLock());
855 if (this != &otherArray
)
857 if (otherArray
.size() <= 0)
863 for (int i
= numUsed
; --i
>= 0;)
864 if (! otherArray
.contains (data
.elements
[i
]))
870 /** Swaps over two elements in the array.
872 This swaps over the elements found at the two indexes passed in.
873 If either index is out-of-range, this method will do nothing.
875 @param index1 index of one of the elements to swap
876 @param index2 index of the other element to swap
878 void swap (const int index1
,
881 const ScopedLockType
lock (getLock());
883 if (isPositiveAndBelow (index1
, numUsed
)
884 && isPositiveAndBelow (index2
, numUsed
))
886 swapVariables (data
.elements
[index1
],
887 data
.elements
[index2
]);
891 /** Moves one of the values to a different position.
893 This will move the value to a specified index, shuffling along
894 any intervening elements as required.
896 So for example, if you have the array { 0, 1, 2, 3, 4, 5 } then calling
897 move (2, 4) would result in { 0, 1, 3, 4, 2, 5 }.
899 @param currentIndex the index of the value to be moved. If this isn't a
900 valid index, then nothing will be done
901 @param newIndex the index at which you'd like this value to end up. If this
902 is less than zero, the value will be moved to the end
905 void move (const int currentIndex
, int newIndex
) noexcept
907 if (currentIndex
!= newIndex
)
909 const ScopedLockType
lock (getLock());
911 if (isPositiveAndBelow (currentIndex
, numUsed
))
913 if (! isPositiveAndBelow (newIndex
, numUsed
))
914 newIndex
= numUsed
- 1;
916 char tempCopy
[sizeof (ElementType
)];
917 memcpy (tempCopy
, data
.elements
+ currentIndex
, sizeof (ElementType
));
919 if (newIndex
> currentIndex
)
921 memmove (data
.elements
+ currentIndex
,
922 data
.elements
+ currentIndex
+ 1,
923 (newIndex
- currentIndex
) * sizeof (ElementType
));
927 memmove (data
.elements
+ newIndex
+ 1,
928 data
.elements
+ newIndex
,
929 (currentIndex
- newIndex
) * sizeof (ElementType
));
932 memcpy (data
.elements
+ newIndex
, tempCopy
, sizeof (ElementType
));
937 //==============================================================================
938 /** Reduces the amount of storage being used by the array.
940 Arrays typically allocate slightly more storage than they need, and after
941 removing elements, they may have quite a lot of unused space allocated.
942 This method will reduce the amount of allocated storage to a minimum.
944 void minimiseStorageOverheads()
946 const ScopedLockType
lock (getLock());
947 data
.shrinkToNoMoreThan (numUsed
);
950 /** Increases the array's internal storage to hold a minimum number of elements.
952 Calling this before adding a large known number of elements means that
953 the array won't have to keep dynamically resizing itself as the elements
954 are added, and it'll therefore be more efficient.
956 void ensureStorageAllocated (const int minNumElements
)
958 const ScopedLockType
lock (getLock());
959 data
.ensureAllocatedSize (minNumElements
);
962 //==============================================================================
963 /** Sorts the elements in the array.
965 This will use a comparator object to sort the elements into order. The object
966 passed must have a method of the form:
968 int compareElements (ElementType first, ElementType second);
971 ..and this method must return:
972 - a value of < 0 if the first comes before the second
973 - a value of 0 if the two objects are equivalent
974 - a value of > 0 if the second comes before the first
976 To improve performance, the compareElements() method can be declared as static or const.
978 @param comparator the comparator to use for comparing elements.
979 @param retainOrderOfEquivalentItems if this is true, then items
980 which the comparator says are equivalent will be
981 kept in the order in which they currently appear
982 in the array. This is slower to perform, but may
983 be important in some cases. If it's false, a faster
984 algorithm is used, but equivalent elements may be
987 @see addSorted, indexOfSorted, sortArray
989 template <class ElementComparator
>
990 void sort (ElementComparator
& comparator
,
991 const bool retainOrderOfEquivalentItems
= false) const
993 const ScopedLockType
lock (getLock());
994 (void) comparator
; // if you pass in an object with a static compareElements() method, this
995 // avoids getting warning messages about the parameter being unused
996 sortArray (comparator
, data
.elements
.getData(), 0, size() - 1, retainOrderOfEquivalentItems
);
999 //==============================================================================
1000 /** Returns the CriticalSection that locks this array.
1001 To lock, you can call getLock().enter() and getLock().exit(), or preferably use
1002 an object of ScopedLockType as an RAII lock for it.
1004 inline const TypeOfCriticalSectionToUse
& getLock() const noexcept
{ return data
; }
1006 /** Returns the type of scoped lock to use for locking this array */
1007 typedef typename
TypeOfCriticalSectionToUse::ScopedLockType ScopedLockType
;
1011 //==============================================================================
1012 ArrayAllocationBase
<ElementType
, TypeOfCriticalSectionToUse
> data
;
1017 #endif // __JUCE_ARRAY_JUCEHEADER__