HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_ValArray.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: Utility Library (C++)
7  *
8  * COMMENTS:
9  * This is a class that is used by UT_ValArray to do all the work...
10  *
11  */
12 
13 #ifndef __UT_VALARRAY_H_INCLUDED__
14 #define __UT_VALARRAY_H_INCLUDED__
15 
16 #include "UT_API.h"
17 #include "UT_Array.h"
18 #include "UT_Assert.h"
19 #include "UT_Swap.h"
20 #include "UT_VectorTypes.h"
21 
22 #include <SYS/SYS_Pragma.h>
23 #include <SYS/SYS_TypeTraits.h>
24 #include <SYS/SYS_Types.h>
25 
26 #include <functional>
27 #include <iosfwd>
28 #include <limits>
29 #include <vector>
30 #include <stdio.h>
31 #include <string.h>
32 
33 
34 UT_API extern void UTsetCompareFloatsTolerance(float tol);
35 UT_API extern float UTgetCompareFloatsTolerance();
36 UT_API extern int UTcompareFloats(const float *a, const float *b);
37 UT_API extern int UTcompareInts(const int *a, const int *b);
38 UT_API extern int UTcomparePointers(void *const* a, void *const* b);
39 
40 
42 template <typename T>
43 class UT_API_TMPL UT_ValArray : public UT_Array<T>
44 {
45 public:
46  typedef int (*Comparator)(const T *, const T *);
47 
48  /// Copy constructor
49  /// Copy constructor. It duplicates the data.
50  /// It's marked explicit so that it's not accidentally passed by value.
51  /// You can always pass by reference and then copy it, if needed.
52  /// If you have a line like:
53  /// UT_ValArray<int> a = otherarray;
54  /// and it really does need to copy instead of referencing,
55  /// you can rewrite it as:
56  /// UT_ValArray<int> a(otherarray);
57  explicit UT_ValArray(const UT_ValArray<T> &src)
58  : UT_Array<T>::UT_Array(src)
59  {}
61  : UT_Array<T>::UT_Array(std::move(src))
62  {}
63  explicit UT_ValArray(const UT_Array<T> &src)
64  : UT_Array<T>::UT_Array(src)
65  {}
66  explicit UT_ValArray(exint capacity = 0)
67  : UT_Array<T>::UT_Array(capacity)
68  {}
70  : UT_Array<T>::UT_Array(capacity, entries)
71  {}
72 
74  {
76  return *this;
77  }
79  {
81  return *this;
82  }
84  {
85  UT_Array<T>::operator=(std::move(src));
86  return *this;
87  }
89  {
90  UT_Array<T>::operator=(std::move(src));
91  return *this;
92  }
93 
94  static bool compareElementsBool(const T &a, const T &b)
95  {
96  return a < b;
97  }
98 
99  /// Sort and then remove all duplicate entries. This returns the number of
100  /// elements removed.
102  {
103  stableSort(compareElementsBool);
105  }
106 
107  static int compareElements(const T *a, const T *b)
108  {
109  if (*a > *b)
110  return 1;
111  if (*a < *b)
112  return -1;
113  return 0;
114  }
115 
116  void sort(Comparator compare)
117  {
118  UT_Array<T>::sort(compare);
119  }
120  template <typename ComparatorBool>
121  void stdsort(ComparatorBool is_less)
122  {
123  UT_Array<T>::stdsort(is_less);
124  }
125  void sort()
126  {
127  stdsort(std::less<T>());
128  }
130  {
131  sort();
132  }
133 
134  template<typename ComparatorBool>
135  void stableSort(ComparatorBool is_less)
136  {
137  UT_Array<T>::stableSort(is_less);
138  }
139  void stableSort()
140  {
141  stableSort(std::less<T>());
142  }
143 
144  template <typename ComparatorBool>
145  T selectNthLargest(int idx, ComparatorBool is_less)
146  {
147  return UT_Array<T>::selectNthLargest(idx, is_less);
148  }
149  T selectNthLargest(int idx)
150  {
151  return selectNthLargest(idx, std::less<T>());
152  }
153 
154  exint uniqueSortedFind(const T &item, Comparator compare) const
155  {
156  return UT_Array<T>::uniqueSortedFind(item, compare);
157  }
158  template <typename ComparatorBool>
159  exint uniqueSortedFind(const T &item, ComparatorBool is_less) const
160  {
161  return UT_Array<T>::uniqueSortedFind(item, is_less);
162  }
163  exint uniqueSortedFind(const T &item) const
164  {
165  return uniqueSortedFind(item, std::less<T>());
166  }
167  exint uniqueSortedFindAscending(const T &item) const
168  {
169  return uniqueSortedFind(item);
170  }
171 
172  SYS_DEPRECATED_HDK(13.0)
173  exint sortedInsert(const T &t, Comparator compare)
174  {
175  return UT_Array<T>::sortedInsert(t, compare);
176  }
177  template <typename ComparatorBool>
178  SYS_DEPRECATED_HDK(13.0)
179  exint sortedInsert(const T &t, ComparatorBool is_less)
180  {
181  return UT_Array<T>::sortedInsert(t, is_less);
182  }
183  SYS_DEPRECATED_HDK(13.0)
184  exint sortedInsert(const T &t)
185  {
186  return sortedInsert(t, std::less<T>());
187  }
188 
189  SYS_DEPRECATED_HDK(13.0)
190  exint uniqueSortedInsert(const T &t, Comparator compare)
191  {
192  return UT_Array<T>::uniqueSortedInsert(t, compare);
193  }
194  template <typename ComparatorBool>
195  SYS_DEPRECATED_HDK(13.0)
196  exint uniqueSortedInsert(const T &t, ComparatorBool is_less)
197  {
198  return UT_Array<T>::uniqueSortedInsert(t, is_less);
199  }
200  SYS_DEPRECATED_HDK(13.0)
201  exint uniqueSortedInsert(const T &t)
202  {
203  return uniqueSortedInsert(t, std::less<T>());
204  }
205  exint SYS_DEPRECATED(13.0) uniqueSortedInsertAscending(const T &t)
206  {
207  return uniqueSortedInsert(t);
208  }
209 
210  template <typename ComparatorBool>
211  void merge(
212  const UT_Array<T> &other,
213  int direction,
214  bool allow_dups,
215  ComparatorBool is_less)
216  {
217  UT_Array<T>::merge(other, direction, allow_dups,
218  is_less);
219  }
220  void merge(
221  const UT_ValArray<T> &other,
222  int direction,
223  bool allow_dups)
224  {
225  merge(other, direction, allow_dups, std::less<T>());
226  }
227 
229  const UT_ValArray<T> &other,
230  Comparator compare = compareElements) const
231  {
232  return UT_Array<T>::hasSortedSubset(other, compare);
233  }
235  const UT_ValArray<T> &other,
236  Comparator compare = compareElements)
237  {
239  }
241  const UT_ValArray<T> &other,
242  UT_ValArray<T> &result,
243  Comparator compare = compareElements) const
244  {
245  UT_Array<T>::sortedUnion(other, result, compare);
246  }
248  const UT_ValArray<T> &other,
249  Comparator compare = compareElements)
250  {
252  }
254  const UT_ValArray<T> &other,
255  UT_ValArray<T> &result,
256  Comparator compare = compareElements) const
257  {
259  }
261  const UT_ValArray<T> &other,
262  Comparator compare = compareElements)
263  {
265  }
267  const UT_ValArray<T> &other,
268  UT_ValArray<T> &result,
269  Comparator compare = compareElements) const
270  {
272  }
273 
274  exint heapPush(const T &t, Comparator compare)
275  {
276  return UT_Array<T>::heapPush(t, compare);
277  }
278  T heapPop(Comparator compare)
279  {
280  return UT_Array<T>::heapPop(compare);
281  }
282 
283  static bool isElementZero(const T &a)
284  {
285  return !a;
286  }
287 
289  {
290  return this->removeIf(isElementZero);
291  }
292 
293  // Remove zeros and also sets the capacity of the array.
294  void collapse()
295  {
296  this->collapseIf(isElementZero);
297  }
298 
299  /// Functions which are only specialized for int{32,64}, fpreal{32,64}
300  // @{
301  /// Returns the sum of the entries in the array.
302  T sum() const
303  {
304  UT_ASSERT(!"Invalid function");
305  return T();
306  }
307  /// Prints the constents of the array
308  void display() const
309  {
310  printf("%" SYS_PRId64 " entries, contents not displayable.\n",
312  }
313  // @}
314 
315 private:
316 
317 };
318 
320 
321 template <>
322 inline int
323 UT_ValArray<fpreal32>::compareElements(const fpreal32 *a, const fpreal32 *b)
324 {
325  const float tol = UTgetCompareFloatsTolerance();
326  if( *a>*b+tol )
327  return 1;
328  if( *a<*b-tol )
329  return -1;
330  return 0;
331 }
332 
333 template <>
334 inline int
336 {
337  const float tol = UTgetCompareFloatsTolerance();
338  if( *a>*b+tol )
339  return 1;
340  if( *a<*b-tol )
341  return -1;
342  return 0;
343 }
344 
345 #define UT_DECL_ARITHMETIC_SPECIALIZATION(T) \
346  template <> UT_API T UT_ValArray<T>::sum() const; \
347  template <> UT_API void UT_ValArray<T>::display() const; \
348  /**/
353 #undef UT_DECL_ARITHMETIC_SPECIALIZATION
354 
355 template <>
356 inline int
357 UT_ValArray<const char *>::compareElements(const char * const *a,
358  const char * const *b)
359 {
360  if (*a && *b) return strcmp(*a, *b);
361  // Use the same comparison order for NULL vs non-NULL as UT_String.
362  // Don't think it's correct, however.
363  if (*a) return -1;
364  if (*b) return 1;
365  return 0;
366 }
367 
368 /// I don't know why this is needed, given that there's one for UT_Array,
369 /// but UTswap called on two UT_ValArray's was using the default "copy
370 /// to temp" function.
372 
374 
375 #endif // __UT_VALARRAY_H_INCLUDED__
UT_ValArray & operator=(UT_Array< T > &&src)
Definition: UT_ValArray.h:88
#define UT_DECL_ARITHMETIC_SPECIALIZATION(T)
Definition: UT_ValArray.h:345
UT_ValArray(exint capacity=0)
Definition: UT_ValArray.h:66
void merge(const UT_Array< T > &other, int direction, bool allow_dups, ComparatorBool is_less)
Definition: UT_ValArray.h:211
exint heapPush(const T &t, Comparator compare)
Definition: UT_ValArray.h:274
UT_ValArray & operator=(UT_ValArray< T > &&src)
Definition: UT_ValArray.h:83
UT_ValArray & operator=(const UT_Array< T > &src)
Definition: UT_ValArray.h:78
#define SYS_DEPRECATED(__V__)
UT_API int UTcompareFloats(const float *a, const float *b)
exint uniqueSortedFind(const T &item, Comparator compare) const
Definition: UT_ArrayImpl.h:338
void merge(const UT_Array< T > &other, int direction, bool allow_dups, ComparatorBool is_less)
Definition: UT_ArrayImpl.h:977
#define SYS_DEPRECATED_PUSH_DISABLE()
UT_Array< T > & operator=(const UT_Array< T > &a)
Definition: UT_ArrayImpl.h:824
IMF_EXPORT IMATH_NAMESPACE::V3f direction(const IMATH_NAMESPACE::Box2i &dataWindow, const IMATH_NAMESPACE::V2f &pixelPosition)
#define SYS_DEPRECATED_POP_DISABLE()
UT_API int UTcompareInts(const int *a, const int *b)
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
UT_ValArray< const char * > UT_StringList
Definition: UT_ValArray.h:373
void stdsort(ComparatorBool is_less)
Sort using std::sort. The ComparatorBool uses the less-than semantics.
Definition: UT_Array.h:296
UT_ValArray(UT_ValArray< T > &&src) SYS_NOEXCEPT
Definition: UT_ValArray.h:60
void sortedSetDifference(const UT_ValArray< T > &other, Comparator compare=compareElements)
Definition: UT_ValArray.h:260
#define UT_API
Definition: UT_API.h:12
exint uniqueSortedInsert(const T &t, Comparator compare)
Definition: UT_ArrayImpl.h:270
void sortedUnion(const UT_ValArray< T > &other, Comparator compare=compareElements)
Definition: UT_ValArray.h:234
int compare(const char *str, bool case_sensitive=true) const
Definition: UT_String.h:379
void collapse()
Definition: UT_ValArray.h:294
static bool compareElementsBool(const T &a, const T &b)
Definition: UT_ValArray.h:94
void sort()
Definition: UT_ValArray.h:125
T heapPop(Comparator compare)
Definition: UT_ValArray.h:278
void sortedSetDifference(const UT_ValArray< T > &other, UT_ValArray< T > &result, Comparator compare=compareElements) const
Definition: UT_ValArray.h:266
void stableSort()
Definition: UT_ValArray.h:139
void sortAscending()
Definition: UT_ValArray.h:129
long long int64
Definition: SYS_Types.h:100
UT_API float UTgetCompareFloatsTolerance()
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:102
bool hasSortedSubset(const UT_ValArray< T > &other, Comparator compare=compareElements) const
Definition: UT_ValArray.h:228
int64 exint
Definition: SYS_Types.h:109
exint uniqueSortedFind(const T &item, ComparatorBool is_less) const
Definition: UT_ValArray.h:159
UT_ValArray & operator=(const UT_ValArray< T > &src)
Definition: UT_ValArray.h:73
UT_ValArray(exint capacity, exint entries)
Definition: UT_ValArray.h:69
double fpreal64
Definition: SYS_Types.h:185
void sort(Comparator compare)
Definition: UT_ValArray.h:116
exint sortedInsert(const T &t, Comparator compare)
Definition: UT_ArrayImpl.h:219
UT_ValArray(const UT_ValArray< T > &src)
Definition: UT_ValArray.h:57
T sum() const
Functions which are only specialized for int{32,64}, fpreal{32,64}.
Definition: UT_ValArray.h:302
UT_SWAPPER_TEMPLATE(UT_ValArray)
void sortedIntersection(const UT_ValArray< T > &other, UT_ValArray< T > &result, Comparator compare=compareElements) const
Definition: UT_ValArray.h:253
static int compareElements(const T *a, const T *b)
Definition: UT_ValArray.h:107
#define SYS_DEPRECATED_HDK(__V__)
int int32
Definition: SYS_Types.h:28
exint sortedRemoveDuplicates()
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
static bool isElementZero(const T &a)
Definition: UT_ValArray.h:283
void stableSort(ComparatorBool is_less)
Definition: UT_ValArray.h:135
UT_API int UTcomparePointers(void *const *a, void *const *b)
#define SYS_PRId64
Definition: SYS_Types.h:60
bool hasSortedSubset(const UT_Array< T > &other, Comparator compare) const
void merge(const UT_ValArray< T > &other, int direction, bool allow_dups)
Definition: UT_ValArray.h:220
void stdsort(ComparatorBool is_less)
Definition: UT_ValArray.h:121
void sortedSetDifference(const UT_Array< T > &other, Comparator compare)
void sortedUnion(const UT_Array< T > &other, Comparator compare)
png_infop png_sPLT_tpp entries
Definition: png.h:2481
typedef int
Definition: png.h:1175
T selectNthLargest(exint idx, ComparatorBool is_less)
Definition: UT_ArrayImpl.h:734
#define SYS_NOEXCEPT
Definition: SYS_Compiler.h:49
void sortedIntersection(const UT_ValArray< T > &other, Comparator compare=compareElements)
Definition: UT_ValArray.h:247
#define UT_API_TMPL
Definition: UT_API.h:13
T selectNthLargest(int idx, ComparatorBool is_less)
Definition: UT_ValArray.h:145
void sort(Comparator compare)
Definition: UT_ArrayImpl.h:726
exint sortAndRemoveDuplicates()
Definition: UT_ValArray.h:101
exint uniqueSortedFind(const T &item) const
Definition: UT_ValArray.h:163
void sortedIntersection(const UT_Array< T > &other, Comparator compare)
exint uniqueSortedFindAscending(const T &item) const
Definition: UT_ValArray.h:167
#define const
Definition: zconf.h:214
void stableSort(ComparatorBool is_less)
Definition: UT_Array.h:310
exint uniqueSortedFind(const T &item, Comparator compare) const
Definition: UT_ValArray.h:154
T heapPop(Comparator compare)
Definition: UT_ArrayImpl.h:377
int removeZeros()
Definition: UT_ValArray.h:288
exint heapPush(const T &t, Comparator compare)
Definition: UT_ArrayImpl.h:360
UT_ValArray(const UT_Array< T > &src)
Definition: UT_ValArray.h:63
void display() const
Prints the constents of the array.
Definition: UT_ValArray.h:308
float fpreal32
Definition: SYS_Types.h:184
void sortedUnion(const UT_ValArray< T > &other, UT_ValArray< T > &result, Comparator compare=compareElements) const
Definition: UT_ValArray.h:240
UT_API void UTsetCompareFloatsTolerance(float tol)
T selectNthLargest(int idx)
Definition: UT_ValArray.h:149
GLenum src
Definition: glcorearb.h:1792