15 #ifndef __UT_ARRAY_H_INCLUDED__
16 #define __UT_ARRAY_H_INCLUDED__
32 #include <initializer_list>
63 myData = capacity ? allocateCapacity(capacity) :
nullptr;
78 explicit UT_Array(std::initializer_list<T> init);
101 if (check_dup && ((idx =
find(t)) != -1))
119 template <
typename...
S>
129 template <typename ComparatorBool>
138 template <
typename ComparatorBool>
150 template <
typename ComparatorBool>
159 template <
typename ComparatorBool>
161 bool allow_dups, ComparatorBool is_less);
219 {
return (index >= 0 && index < mySize); }
234 exint idx = --mySize;
250 template <
typename IsEqual>
254 template <
typename IsEqual>
294 return (&t >= myData && &t < (myData + mySize))
305 template <
typename ComparatorBool>
308 std::sort(myData, myData + mySize, is_less);
319 template<
typename ComparatorBool>
330 template<
typename ComparatorBool>
343 std::stable_sort(
array() + start,
array() + end, is_less);
347 template <
typename I,
typename V,
typename ComparatorBool>
357 {
return myCompare(myValues(a), myValues(b)); }
360 const ComparatorBool &myCompare;
371 template <
typename I,
typename ComparatorBool>
373 ComparatorBool is_less)
const
375 IndexedCompare<I, T, ComparatorBool>
compare(*
this, is_less);
376 std::stable_sort(indices.
getArray(),
383 template <
typename I,
typename ComparatorBool>
385 ComparatorBool is_less)
const
396 template <
typename K,
typename ComparatorBool>
414 template <
typename CompareEqual>
419 template<
typename ComparatorBool>
430 template <
typename ComparatorBool>
452 exint newcapacity = mincapacity;
453 if (bumped > mincapacity)
454 newcapacity = bumped;
492 return (inclusive ?
sizeof(*
this) : 0) +
capacity()*
sizeof(
T);
503 if (newsize == mySize)
506 if (mySize > newsize)
514 if (
size() >= minsize)
532 if (newsize == mySize)
535 if (mySize > newsize)
545 if (maxsize >= 0 &&
size() > maxsize)
626 return (i >= 0 && i < mySize) ? myData[i] :
T();
632 return myData[mySize-1];
637 return myData[mySize-1];
646 template <
typename BinaryOp>
653 const T *
array()
const {
return myData; }
656 const T *
data()
const {
return myData; }
661 {
T *
data = myData; myData = newdata;
return data; }
663 template <
typename IT,
bool FORWARD>
665 public std::iterator<std::random_access_iterator_tag, T, exint>
677 template<
typename EIT>
679 : myCurrent(src.myCurrent), myEnd(src.myEnd) {}
682 {
return FORWARD ? myCurrent : myCurrent - 1; }
685 {
return FORWARD ? *myCurrent : myCurrent[-1]; }
688 {
return FORWARD ? *myCurrent : myCurrent[-1]; }
691 {
return FORWARD ? myCurrent[
n] : myCurrent[-n - 1]; }
696 if (FORWARD) ++myCurrent;
else --myCurrent;
703 if (FORWARD) ++myCurrent;
else --myCurrent;
709 if (FORWARD) --myCurrent;
else ++myCurrent;
716 if (FORWARD) --myCurrent;
else ++myCurrent;
737 {
return (*
this) += (-
n); }
739 {
return (*
this) + (-
n); }
741 bool atEnd()
const {
return myCurrent == myEnd; }
745 template<
typename ITR,
bool FR>
747 {
return myCurrent == r.myCurrent; }
749 template<
typename ITR,
bool FR>
751 {
return myCurrent != r.myCurrent; }
753 template<
typename ITR>
754 bool operator<(const base_iterator<ITR, FORWARD> &
r)
const
757 return myCurrent <
r.myCurrent;
759 return r.myCurrent < myCurrent;
762 template<
typename ITR>
766 return myCurrent > r.myCurrent;
768 return r.myCurrent > myCurrent;
771 template<
typename ITR>
772 bool operator<=(const base_iterator<ITR, FORWARD> &
r)
const
775 return myCurrent <=
r.myCurrent;
777 return r.myCurrent <= myCurrent;
780 template<
typename ITR>
784 return myCurrent >= r.myCurrent;
786 return r.myCurrent >= myCurrent;
790 template<
typename ITR>
794 return exint(myCurrent - r.myCurrent);
796 return exint(r.myCurrent - myCurrent);
819 return iterator(myData, myData + mySize);
877 removeAt(&it.item() - myData);
887 myCapacity = src.myCapacity;
893 myCapacity = srcsize;
912 return (myData != (
T *)(((
char*)
this) +
sizeof(*
this)));
916 return (data != (
T *)(((
char*)
this) +
sizeof(*
this)));
926 return SYS_IsPod_v< T >;
932 template <
typename S>
936 template <
typename S>
939 template <
typename S>
945 template <typename First, typename... Rest>
949 static_cast<const void *>(&first) <
950 static_cast<const void *>(myData) ||
951 static_cast<const void *>(&first) >=
952 static_cast<const void *>(myData + mySize),
953 "Argument cannot reference an existing element in the array.");
964 template <
typename...
S>
967 new (&
dst)
T(std::forward<S>(
s)...);
984 ::memcpy((
void *)dst, (
const void *)src,
990 for (
exint i = 0; i <
n; i++)
991 new (&dst[i])
T(src[i]);
1001 memset((
void *)&dst, 0,
sizeof(
T));
1007 for (
exint i = 0; i <
n; i++)
1019 memset((
void *)dst, 0,
sizeof(
T));
1022 memset((
void *)dst, 0,
sizeof(
T) * n);
1035 for (
exint i = 0; i <
n; i++)
1053 T * allocateCapacity(
exint num_items);
1055 template<
typename OS,
typename S>
1056 friend OS &operator<<(OS &os, const UT_Array<S> &d);
1066 template <
typename T,
typename S>
1073 for (
exint i = 0; i <
n; i++)
1074 dest(i) =
T(
src(i));
1076 template <
typename T,
typename S>
1082 for (
exint i = 0; i <
n; i++)
1083 dest(i) =
T(src[i]);
1085 template <
typename T,
typename S>
1091 for (
exint i = 0; i <
n; i++)
1092 dest[i] =
T(
src(i));
1094 template <
typename T,
typename S>
1099 for (
int64 i = 0; i <
n; i++)
1100 dest[i] =
T(src[i]);
1106 template<
typename OS,
typename S>
1108 operator<<(OS &os, const UT_Array<S> &d)
1115 template <
typename T>
UT_API size_t
1120 template <
template <
typename>
class ArrayT,
typename T>
1122 UTarrayDeepMemoryUsage(
const ArrayT<T> &arr,
bool inclusive)
1124 int64 mem = inclusive ?
sizeof(arr) : 0;
1125 mem += arr.getMemoryUsage(
false);
1126 for (
auto &&item : arr)
1127 mem += item.getMemoryUsage(
false);
1132 template <
typename T>
1136 arr.
stdsort([](
const T &
a,
const T &
b) {
return a <
b; });
1140 template <
typename T>
1150 template <
typename T>
1151 struct DefaultClearer;
1153 template <
typename T>
1162 static const bool clearNeedsDestruction =
false;
1166 #endif // __UT_ARRAY_H_INCLUDED__
reference operator*() const
base_iterator & operator++()
Pre-increment operator.
int isEqual(const UT_Array< T > &a, Comparator compare) const
base_iterator & operator--()
Pre-decrement operator.
exint insert(T &&t, exint i)
IndexedCompare(const UT_Array< V > &values, const ComparatorBool &compare)
exint find(const T &t, exint s=0) const
GLboolean GLboolean GLboolean b
const T & operator[](exint i) const
const T * getRawArray() const
bool isHeapBuffer() const
Returns true if the data used by the array was allocated on the heap.
pointer operator->() const
base_iterator operator+(exint n) const
GLenum GLuint GLsizei bufsize
void validateEmplaceArgs() const
Base case for validateEmplaceArgs().
void sortAndRemoveDuplicates(ComparatorBool is_less)
bool operator!=(const UT_Array< T > &a) const
void setSizeIfNeeded(exint minsize)
exint insertImpl(S &&s, exint index)
Similar to appendImpl() but for insertion.
static void copyConstructRange(T *dst, const T *src, exint n)
exint uniqueSortedFind(const T &item, Comparator compare) const
SYS_FORCE_INLINE void removeLast()
void merge(const UT_Array< T > &other, int direction, bool allow_dups, ComparatorBool is_less)
void unsafeShareData(T *src, exint size, exint capacity)
UT_Array< T > & operator=(const UT_Array< T > &a)
IMF_EXPORT IMATH_NAMESPACE::V3f direction(const IMATH_NAMESPACE::Box2i &dataWindow, const IMATH_NAMESPACE::V2f &pixelPosition)
void extractRange(exint begin_i, exint end_i, UT_Array< T > &dest)
void collapseIf(IsEqual is_equal)
Remove all matching elements. Also sets the capacity of the array.
T * aliasArray(T *newdata)
void setSizeNoInit(exint newsize)
bool isValidIndex(exint index) const
Return true if given index is valid.
void zero()
Zeros the array if a POD type, else trivial constructs if a class type.
base_iterator< const T, false > const_reverse_iterator
static void trivialConstruct(T &dst)
Element Constructor.
friend void swap(UT_Array< T > &a, UT_Array< T > &b)
UT_API size_t format(char *buffer, size_t bufsize, const UT_Array< T > &v)
void move(exint src_idx, exint dst_idx, exint how_many)
const_iterator begin() const
int64 getMemoryUsage(bool inclusive=false) const
void bumpEntries(exint newsize)
const T & heapMax() const
void cycle(exint how_many)
Cyclically shifts the entire array by how_many.
exint removeIndex(exint index)
void stdsort(ComparatorBool is_less)
Sort using std::sort. The ComparatorBool uses the less-than semantics.
const_reverse_iterator rend() const
End reverse iterator. Consider using it.atEnd() instead.
bool isHeapBuffer(T *data) const
static constexpr SYS_FORCE_INLINE bool isPOD()
exint concat(const UT_Array< T > &a)
Takes another T array and concatenate it onto my end.
exint append(const T &t, bool check_dup)
#define UT_ASSERT_MSG_P(ZZ,...)
exint index(const T &t) const
bool operator>=(const base_iterator< ITR, FORWARD > &r) const
exint uniqueSortedInsert(const T &t, Comparator compare)
void bumpCapacity(exint mincapacity)
static bool isClear(const UT_Array< T > &v)
void setSize(exint newsize)
bool operator==(const base_iterator< ITR, FR > &r) const
static void trivialDestructRange(T *dst, exint n)
base_iterator operator-(exint n) const
void bumpSize(exint newsize)
bool operator>(const base_iterator< ITR, FORWARD > &r) const
base_iterator< T, false > reverse_iterator
void unsafeShareData(T *src, exint srcsize)
GLenum GLsizei GLsizei GLint * values
exint operator-(const base_iterator< ITR, FORWARD > &r) const
CompareResults OIIO_API compare(const ImageBuf &A, const ImageBuf &B, float failthresh, float warnthresh, ROI roi={}, int nthreads=0)
const_reverse_iterator rbegin() const
Begin iterating over the array in reverse.
exint safeIndex(const T &t) const
void setCapacity(exint newcapacity)
void stableSortByKey(const UT_Array< K > &keys, ComparatorBool is_less)
exint apply(int(*apply_func)(T &t, void *d), void *d)
reverse_iterator rbegin()
Begin iterating over the array in reverse.
exint emplace_back(S &&...s)
static void construct(T &dst, S &&...s)
void entries(exint newsize)
Alias of setSize(). setSize() is preferred.
exint uniqueSortedInsertImpl(S &&s, Comparator compare)
const T & operator()(exint i) const
reference operator[](exint n) const
exint insertAt(const T &t, exint index)
T accumulate(const T &init_value, BinaryOp add) const
GLboolean GLboolean GLboolean GLboolean a
base_iterator< T, true > iterator
GLsizei GLenum const void * indices
static void clearConstruct(UT_Array< T > *p)
static SYS_FORCE_INLINE void trivialDestruct(T &dst)
Element Destructor.
exint sortedInsert(const T &t, Comparator compare)
UT_IteratorRange< reverse_iterator > rrange()
exint insert(const T &t, exint i)
base_iterator(const base_iterator< EIT, FORWARD > &src)
void appendMultiple(const T &t, exint count)
static void trivialConstructRange(T *dst, exint n)
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
base_iterator(IT *c, IT *e)
void stableSortRange(ComparatorBool is_less, exint start, exint end)
Like stableSort, but operates on a subset of the array.
T forcedGet(exint i) const
static void copyConstruct(T &dst, const T &src)
#define SYS_DEPRECATED_HDK(__V__)
exint removeIf(IsEqual is_equal)
const_iterator end() const
End const iterator. Consider using it.atEnd() instead.
base_iterator & operator-=(exint n)
exint sortedRemoveDuplicates()
void unsafeShareData(UT_Array< T > &src)
reverse_iterator rend()
End reverse iterator.
bool operator()(I a, I b) const
bool operator!=(const base_iterator< ITR, FR > &r) const
exint sortedRemoveDuplicatesIf(CompareEqual compare_equal)
exint entries() const
Alias of size(). size() is preferred.
bool hasSortedSubset(const UT_Array< T > &other, Comparator compare) const
base_iterator & operator+=(exint n)
void sortedSetDifference(const UT_Array< T > &other, Comparator compare)
void sortedUnion(const UT_Array< T > &other, Comparator compare)
UT_Array(exint capacity=0)
Construct based on given capacity with a size of 0.
T selectNthLargest(exint idx, ComparatorBool is_less)
base_iterator operator--(int)
Post-decrement operator.
static void clear(UT_Array< T > &v)
int(* Comparator)(const T *, const T *)
void sort(Comparator compare)
base_iterator< const T, true > const_iterator
void stableArgSort(UT_Array< I > &indices, ComparatorBool is_less) const
void truncate(exint maxsize)
Decreases, but never expands, to the given maxsize.
void constant(const T &v)
Quickly set the array to a single value.
void setCapacityIfNeeded(exint mincapacity)
void removeItem(const reverse_iterator &it)
Remove item specified by the reverse_iterator.
void sortedIntersection(const UT_Array< T > &other, Comparator compare)
void UTconvertArray(UT_Array< T > &dest, const UT_Array< S > &src)
Comparator class for stableSortIndices.
ImageBuf OIIO_API add(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
void stableSort(ComparatorBool is_less)
UT_Array(const UT_Array< T > &a)
void clear()
Resets list to an empty list.
void stableSortIndices(UT_Array< I > &indices, ComparatorBool is_less) const
UT_IteratorRange< iterator > range()
UT_IteratorRange< const_iterator > range() const
GA_API const UT_StringHolder rest
T heapPop(Comparator compare)
exint heapPush(const T &t, Comparator compare)
void sort(I begin, I end, const Pred &pred)
void reverse()
Reverses the array by swapping elements in mirrored locations.
exint multipleInsert(exint index, exint count)
Insert an element "count" times at the given index. Return the index.
void removeRange(exint begin_i, exint end_i)
Remove the range [begin_i,end_i) of elements from the array.
base_iterator operator++(int)
Post-increment operator.
void swap(UT_Array< T > &other)
exint insert(exint index)
iterator end()
End iterator.
exint findAndRemove(const T &t)
UT_IteratorRange< const_reverse_iterator > rrange() const
bool isEmpty() const
Returns true iff there are no occupied elements in the array.
bool operator==(const UT_Array< T > &a) const
exint sortedFind(const T &t, Comparator compare) const