HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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_Compare.h"
20 #include "UT_Swap.h"
21 #include "UT_VectorTypes.h"
22 
23 #include <SYS/SYS_Compiler.h>
24 #include <SYS/SYS_Deprecated.h>
25 #include <SYS/SYS_Types.h>
26 
27 #include <initializer_list>
28 #include <stdio.h>
29 #include <string.h>
30 
31 
32 UT_API extern void UTsetCompareFloatsTolerance(float tol);
33 UT_API extern float UTgetCompareFloatsTolerance();
34 UT_API extern int UTcompareFloats(const float *a, const float *b);
35 UT_API extern int UTcompareInts(const int *a, const int *b);
36 UT_API extern int UTcomparePointers(void *const* a, void *const* b);
37 
38 
40 template <typename T>
42 {
43 public:
44  typedef int (*Comparator)(const T *, const T *);
45 
46  /// Copy constructor
47  /// Copy constructor. It duplicates the data.
48  /// It's marked explicit so that it's not accidentally passed by value.
49  /// You can always pass by reference and then copy it, if needed.
50  /// If you have a line like:
51  /// UT_ValArray<int> a = otherarray;
52  /// and it really does need to copy instead of referencing,
53  /// you can rewrite it as:
54  /// UT_ValArray<int> a(otherarray);
55  explicit UT_ValArray(const UT_ValArray<T> &src)
56  : UT_Array<T>::UT_Array(src)
57  {}
58  explicit UT_ValArray(const UT_Array<T> &src)
59  : UT_Array<T>::UT_Array(src)
60  {}
62  : UT_Array<T>::UT_Array(std::move(src))
63  {}
65  : UT_Array<T>::UT_Array(std::move(src))
66  {}
67  explicit UT_ValArray(exint capacity = 0)
68  : UT_Array<T>::UT_Array(capacity)
69  {}
70  UT_ValArray(exint capacity, exint entries)
71  : UT_Array<T>::UT_Array(capacity, entries)
72  {}
73  /// If you are wondering why we mark this as explicit...
74  /// Imagine you have the following:
75  /// void foo(int i); // 1
76  /// void foo(UT_ValArray<int>); // 2
77  /// Without explicit you can do this
78  /// foo({1})
79  /// and function 1 will be called when you probably meant
80  /// for function 2 to be called.
81  explicit UT_ValArray(std::initializer_list<T> init)
82  : UT_Array<T>::UT_Array(init)
83  {}
84 
86  {
88  return *this;
89  }
91  {
93  return *this;
94  }
95  UT_ValArray &operator=(std::initializer_list<T> src)
96  {
98  return *this;
99  }
101  {
102  UT_Array<T>::operator=(std::move(src));
103  return *this;
104  }
106  {
107  UT_Array<T>::operator=(std::move(src));
108  return *this;
109  }
110 
111  static bool compareElementsBool(const T &a, const T &b)
112  {
113  return a < b;
114  }
115 
116  /// Sort and then remove all duplicate entries. This returns the number of
117  /// elements removed.
119  {
120  stableSort(compareElementsBool);
122  }
123 
124  static int compareElements(const T *a, const T *b)
125  {
126  if (*a > *b)
127  return 1;
128  if (*a < *b)
129  return -1;
130  return 0;
131  }
132 
134  {
136  }
137 
138  template<typename ComparatorBool>
139  void stableSort(ComparatorBool is_less)
140  {
141  UT_Array<T>::stableSort(is_less);
142  }
143  void stableSort()
144  {
145  stableSort(Less());
146  }
147 
148  template <typename ComparatorBool>
149  T selectNthLargest(int idx, ComparatorBool is_less)
150  {
151  return UT_Array<T>::selectNthLargest(idx, is_less);
152  }
153  T selectNthLargest(int idx)
154  {
155  return selectNthLargest(idx, Less());
156  }
157 
158  exint uniqueSortedFind(const T &item, Comparator compare) const
159  {
160  return UT_Array<T>::uniqueSortedFind(item, compare);
161  }
162  template <typename ComparatorBool>
163  exint uniqueSortedFind(const T &item, ComparatorBool is_less) const
164  {
165  return UT_Array<T>::uniqueSortedFind(item, is_less);
166  }
167  exint uniqueSortedFind(const T &item) const
168  {
169  return uniqueSortedFind(item, Less());
170  }
171  exint uniqueSortedFindAscending(const T &item) const
172  {
173  return uniqueSortedFind(item);
174  }
175 
176  SYS_DEPRECATED_HDK(13.0)
177  exint sortedInsert(const T &t, Comparator compare)
178  {
179  return UT_Array<T>::sortedInsert(t, compare);
180  }
181  template <typename ComparatorBool>
182  SYS_DEPRECATED_HDK(13.0)
183  exint sortedInsert(const T &t, ComparatorBool is_less)
184  {
185  return UT_Array<T>::sortedInsert(t, is_less);
186  }
187  SYS_DEPRECATED_HDK(13.0)
188  exint sortedInsert(const T &t)
189  {
190  return sortedInsert(t, Less());
191  }
192 
193  SYS_DEPRECATED_HDK(13.0)
194  exint uniqueSortedInsert(const T &t, Comparator compare)
195  {
196  return UT_Array<T>::uniqueSortedInsert(t, compare);
197  }
198  template <typename ComparatorBool>
199  SYS_DEPRECATED_HDK(13.0)
200  exint uniqueSortedInsert(const T &t, ComparatorBool is_less)
201  {
202  return UT_Array<T>::uniqueSortedInsert(t, is_less);
203  }
204  SYS_DEPRECATED_HDK(13.0)
205  exint uniqueSortedInsert(const T &t)
206  {
207  return uniqueSortedInsert(t, Less());
208  }
209  exint SYS_DEPRECATED(13.0) uniqueSortedInsertAscending(const T &t)
210  {
211  return uniqueSortedInsert(t);
212  }
213 
214  template <typename ComparatorBool>
215  void merge(
216  const UT_Array<T> &other,
217  int direction,
218  bool allow_dups,
219  ComparatorBool is_less)
220  {
221  UT_Array<T>::merge(other, direction, allow_dups,
222  is_less);
223  }
224  void merge(
225  const UT_ValArray<T> &other,
226  int direction,
227  bool allow_dups)
228  {
229  merge(other, direction, allow_dups, Less());
230  }
231 
232 private:
233  // SFINAE constraint for bool comparators to avoid ambiguity
234  template <typename F>
235  using IsBoolComp = decltype(std::declval<F>()(std::declval<T>(),
236  std::declval<T>()),
237  void());
238 public:
239 
242 
243  SYS_DEPRECATED_HDK_REPLACE(19.5, "Use explicit ComparatorBool")
244  bool hasSortedSubset(const UT_ValArray<T> &other) const
245  {
247  other, compareElements);
248  }
249  SYS_DEPRECATED_HDK_REPLACE(19.5, "Use ComparatorBool variant")
250  bool hasSortedSubset(
251  const UT_ValArray<T> &other,
252  Comparator compare) const
253  {
254  return UT_Array<T>::hasSortedSubset(other, compare);
255  }
256  template <typename ComparatorBool, typename = IsBoolComp<ComparatorBool>>
257  bool hasSortedSubset(const UT_Array<T> &other,
258  ComparatorBool is_less) const
259  {
260  return UT_Array<T>::hasSortedSubset(other, is_less);
261  }
262 
263  SYS_DEPRECATED_HDK_REPLACE(19.5, "Use explicit ComparatorBool")
264  void sortedUnion(const UT_ValArray<T> &other)
265  {
266  UT_Array<T>::sortedUnion(other, compareElements);
267  }
268  SYS_DEPRECATED_HDK_REPLACE(19.5, "Use ComparatorBool variant")
269  void sortedUnion(
270  const UT_ValArray<T> &other,
271  Comparator compare)
272  {
273  UT_Array<T>::sortedUnion(other, compare);
274  }
275  template <typename ComparatorBool, typename = IsBoolComp<ComparatorBool>>
277  const UT_ValArray<T> &other,
278  ComparatorBool is_less)
279  {
280  UT_Array<T>::sortedUnion(other, is_less);
281  }
282  SYS_DEPRECATED_HDK_REPLACE(19.5, "Use explicit ComparatorBool")
283  void sortedUnion(
284  const UT_ValArray<T> &other,
285  UT_ValArray<T> &result) const
286  {
288  other, result, compareElements);
289  }
290  SYS_DEPRECATED_HDK_REPLACE(19.5, "Use ComparatorBool variant")
291  void sortedUnion(
292  const UT_ValArray<T> &other,
293  UT_ValArray<T> &result,
294  Comparator compare) const
295  {
296  UT_Array<T>::sortedUnion(other, result, compare);
297  }
298  template <typename ComparatorBool, typename = IsBoolComp<ComparatorBool>>
300  const UT_ValArray<T> &other,
302  ComparatorBool is_less) const
303  {
304  UT_Array<T>::sortedUnion(other, result, is_less);
305  }
306 
307  SYS_DEPRECATED_HDK_REPLACE(19.5, "Use explicit ComparatorBool")
308  void sortedIntersection(const UT_ValArray<T> &other)
309  {
310  UT_Array<T>::sortedIntersection(other, compareElements);
311  }
312  SYS_DEPRECATED_HDK_REPLACE(19.5, "Use ComparatorBool variant")
313  void sortedIntersection(
314  const UT_ValArray<T> &other,
315  Comparator compare)
316  {
317  UT_Array<T>::sortedIntersection(other, compare);
318  }
319  template <typename ComparatorBool, typename = IsBoolComp<ComparatorBool>>
321  const UT_ValArray<T> &other,
322  ComparatorBool is_less)
323  {
324  UT_Array<T>::sortedIntersection(other, is_less);
325  }
326 
327  SYS_DEPRECATED_HDK_REPLACE(19.5, "Use explicit ComparatorBool")
328  void sortedIntersection(
329  const UT_ValArray<T> &other,
330  UT_ValArray<T> &result) const
331  {
333  other, result, compareElements);
334  }
335  SYS_DEPRECATED_HDK_REPLACE(19.5, "Use ComparatorBool variant")
336  void sortedIntersection(
337  const UT_ValArray<T> &other,
338  UT_ValArray<T> &result,
339  Comparator compare) const
340  {
341  UT_Array<T>::sortedIntersection(other, result, compare);
342  }
343  template <typename ComparatorBool, typename = IsBoolComp<ComparatorBool>>
345  const UT_ValArray<T> &other,
347  ComparatorBool is_less) const
348  {
349  UT_Array<T>::sortedIntersection(other, result, is_less);
350  }
351 
352  SYS_DEPRECATED_HDK_REPLACE(19.5, "Use explicit ComparatorBool")
353  void sortedSetDifference(const UT_ValArray<T> &other)
354  {
356  other, compareElements);
357  }
358  SYS_DEPRECATED_HDK_REPLACE(19.5, "Use ComparatorBool variant")
359  void sortedSetDifference(
360  const UT_ValArray<T> &other,
361  Comparator compare)
362  {
363  UT_Array<T>::sortedSetDifference(other, compare);
364  }
365  template <typename ComparatorBool, typename = IsBoolComp<ComparatorBool>>
367  const UT_ValArray<T> &other,
368  ComparatorBool is_less)
369  {
370  UT_Array<T>::sortedSetDifference(other, is_less);
371  }
372 
373  SYS_DEPRECATED_HDK_REPLACE(19.5, "Use explicit ComparatorBool")
374  void sortedSetDifference(
375  const UT_ValArray<T> &other,
376  UT_ValArray<T> &result) const
377  {
379  other, result, compareElements);
380  }
381  SYS_DEPRECATED_HDK_REPLACE(19.5, "Use ComparatorBool variant")
382  void sortedSetDifference(
383  const UT_ValArray<T> &other,
384  UT_ValArray<T> &result,
385  Comparator compare) const
386  {
388  other, result, compare);
389  }
390  template <typename ComparatorBool, typename = IsBoolComp<ComparatorBool>>
392  const UT_ValArray<T> &other,
394  ComparatorBool is_less) const
395  {
397  other, result, is_less);
398  }
399 
401 
402  exint heapPush(const T &t, Comparator compare)
403  {
404  return UT_Array<T>::heapPush(t, compare);
405  }
406  T heapPop(Comparator compare)
407  {
408  return UT_Array<T>::heapPop(compare);
409  }
410 
411  static bool isElementZero(const T &a)
412  {
413  return !a;
414  }
415 
417  {
418  return this->removeIf(isElementZero);
419  }
420 
421  // Remove zeros and also sets the capacity of the array.
422  void collapse()
423  {
424  this->collapseIf(isElementZero);
425  }
426 
427  /// Functions which are only specialized for int{32,64}, fpreal{32,64}
428  // @{
429  /// Returns the sum of the entries in the array.
430  T sum() const
431  {
432  UT_ASSERT(!"Invalid function");
433  return T();
434  }
435  /// Prints the constents of the array
436  void display() const
437  {
438  printf("%" SYS_PRId64 " entries, contents not displayable.\n",
440  }
441  // @}
442 
443 private:
444  // manual wrapper for std functors to avoid including <functional>
445  struct Less
446  {
447  bool operator()(const T& lhs, const T& rhs) const
448  { return lhs < rhs; }
449  };
450 };
451 
453 
454 template <>
455 inline int
456 UT_ValArray<fpreal32>::compareElements(const fpreal32 *a, const fpreal32 *b)
457 {
458  const float tol = UTgetCompareFloatsTolerance();
459  if( *a>*b+tol )
460  return 1;
461  if( *a<*b-tol )
462  return -1;
463  return 0;
464 }
465 
466 template <>
467 inline int
469 {
470  const float tol = UTgetCompareFloatsTolerance();
471  if( *a>*b+tol )
472  return 1;
473  if( *a<*b-tol )
474  return -1;
475  return 0;
476 }
477 
478 #define UT_DECL_ARITHMETIC_SPECIALIZATION(T) \
479  template <> UT_API T UT_ValArray<T>::sum() const; \
480  template <> UT_API void UT_ValArray<T>::display() const; \
481  /**/
486 #undef UT_DECL_ARITHMETIC_SPECIALIZATION
487 
488 template <>
489 inline int
490 UT_ValArray<const char *>::compareElements(const char * const *a,
491  const char * const *b)
492 {
493  if (*a && *b) return strcmp(*a, *b);
494  // Use the same comparison order for NULL vs non-NULL as UT_String.
495  // Don't think it's correct, however.
496  if (*a) return -1;
497  if (*b) return 1;
498  return 0;
499 }
500 
501 /// I don't know why this is needed, given that there's one for UT_Array,
502 /// but UTswap called on two UT_ValArray's was using the default "copy
503 /// to temp" function.
505 
507 
508 // For UT::ArraySet.
509 namespace UT
510 {
511 template <typename T>
512 struct DefaultClearer;
513 
514 template <typename T>
516 {
517  static void clear(UT_ValArray<T> &v) { v.setCapacity(0); }
518  static bool isClear(const UT_ValArray<T> &v) { return v.capacity() == 0; }
520  {
521  new ((void *)p) UT_ValArray<T>();
522  }
523  static const bool clearNeedsDestruction = false;
524 };
525 } // namespace UT
526 
527 #endif // __UT_VALARRAY_H_INCLUDED__
UT_ValArray & operator=(UT_Array< T > &&src)
Definition: UT_ValArray.h:105
#define UT_DECL_ARITHMETIC_SPECIALIZATION(T)
Definition: UT_ValArray.h:478
UT_ValArray(exint capacity=0)
Definition: UT_ValArray.h:67
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
void merge(const UT_Array< T > &other, int direction, bool allow_dups, ComparatorBool is_less)
Definition: UT_ValArray.h:215
UT_ValArray & operator=(UT_ValArray< T > &&src)
Definition: UT_ValArray.h:100
void merge(const UT_Array< T > &other, int direction, bool allow_dups, ComparatorBool is_less={})
Definition: UT_ArrayImpl.h:970
void sortedSetDifference(const UT_ValArray< T > &other, UT_ValArray< T > &result, ComparatorBool is_less) const
Definition: UT_ValArray.h:391
UT_ValArray & operator=(std::initializer_list< T > src)
Definition: UT_ValArray.h:95
int int32
Definition: SYS_Types.h:39
UT_ValArray & operator=(const UT_Array< T > &src)
Definition: UT_ValArray.h:90
static void clearConstruct(UT_ValArray< T > *p)
Definition: UT_ValArray.h:519
#define SYS_DEPRECATED(__V__)
#define SYS_PRAGMA_PUSH_WARN()
Definition: SYS_Pragma.h:34
void stableSort(ComparatorBool is_less={})
Definition: UT_Array.h:448
UT_API int UTcompareFloats(const float *a, const float *b)
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:623
void sortedIntersection(const UT_ValArray< T > &other, UT_ValArray< T > &result, ComparatorBool is_less) const
Definition: UT_ValArray.h:344
void
Definition: png.h:1083
auto printf(const S &fmt, const T &...args) -> int
Definition: printf.h:626
#define SYS_DEPRECATED_PUSH_DISABLE()
UT_Array< T > & operator=(const UT_Array< T > &a)
Definition: UT_ArrayImpl.h:832
IMF_EXPORT IMATH_NAMESPACE::V3f direction(const IMATH_NAMESPACE::Box2i &dataWindow, const IMATH_NAMESPACE::V2f &pixelPosition)
#define SYS_DEPRECATED_POP_DISABLE()
#define SYS_DEPRECATED_HDK_REPLACE(__V__, __R__)
exint uniqueSortedFind(const T &item, ComparatorBool is_less={}) const
Definition: UT_ArrayImpl.h:305
int64 exint
Definition: SYS_Types.h:125
UT_API int UTcompareInts(const int *a, const int *b)
UT_ValArray< const char * > UT_StringList
Definition: UT_ValArray.h:506
#define UT_API
Definition: UT_API.h:14
exint uniqueSortedInsert(const T &t, Comparator compare)
Definition: UT_Array.h:196
GLenum src
Definition: glcorearb.h:1793
void collapse()
Definition: UT_ValArray.h:422
float fpreal32
Definition: SYS_Types.h:200
static bool compareElementsBool(const T &a, const T &b)
Definition: UT_ValArray.h:111
GLdouble GLdouble t
Definition: glew.h:1403
T heapPop(Comparator compare)
Definition: UT_ValArray.h:406
void sortedUnion(const UT_Array< T > &other, ComparatorBool is_less={})
#define SYS_PRAGMA_DISABLE_DEPRECATED()
Definition: SYS_Pragma.h:48
void stableSort()
Definition: UT_ValArray.h:143
double fpreal64
Definition: SYS_Types.h:201
void sortedUnion(const UT_ValArray< T > &other, UT_ValArray< T > &result, ComparatorBool is_less) const
Definition: UT_ValArray.h:299
void sortAscending()
Definition: UT_ValArray.h:133
CompareResults OIIO_API compare(const ImageBuf &A, const ImageBuf &B, float failthresh, float warnthresh, ROI roi={}, int nthreads=0)
static bool isClear(const UT_ValArray< T > &v)
Definition: UT_ValArray.h:518
void setCapacity(exint newcapacity)
Definition: UT_ArrayImpl.h:756
GLuint64EXT * result
Definition: glew.h:14311
UT_API float UTgetCompareFloatsTolerance()
void sort(ComparatorBool is_less={})
Sort using std::sort with bool comparator. Defaults to operator<().
Definition: UT_Array.h:419
exint uniqueSortedFind(const T &item, ComparatorBool is_less) const
Definition: UT_ValArray.h:163
UT_ValArray & operator=(const UT_ValArray< T > &src)
Definition: UT_ValArray.h:85
UT_ValArray(exint capacity, exint entries)
Definition: UT_ValArray.h:70
const GLdouble * v
Definition: glcorearb.h:837
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
exint capacity() const
Definition: UT_Array.h:606
exint sortedInsert(const T &t, Comparator compare)
Definition: UT_ArrayImpl.h:225
UT_ValArray(const UT_ValArray< T > &src)
Definition: UT_ValArray.h:55
T sum() const
Functions which are only specialized for int{32,64}, fpreal{32,64}.
Definition: UT_ValArray.h:430
UT_SWAPPER_TEMPLATE(UT_ValArray)
long long int64
Definition: SYS_Types.h:116
GLfloat GLfloat p
Definition: glew.h:16656
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
static int compareElements(const T *a, const T *b)
Definition: UT_ValArray.h:124
void sortedSetDifference(const UT_ValArray< T > &other, ComparatorBool is_less)
Definition: UT_ValArray.h:366
#define SYS_PRAGMA_POP_WARN()
Definition: SYS_Pragma.h:35
#define SYS_DEPRECATED_HDK(__V__)
exint sortedRemoveDuplicates()
void sortedSetDifference(const UT_Array< T > &other, ComparatorBool is_less={})
UT_ValArray(UT_Array< T > &&src) noexcept
Definition: UT_ValArray.h:64
void sortedIntersection(const UT_ValArray< T > &other, ComparatorBool is_less)
Definition: UT_ValArray.h:320
static bool isElementZero(const T &a)
Definition: UT_ValArray.h:411
void stableSort(ComparatorBool is_less)
Definition: UT_ValArray.h:139
UT_API int UTcomparePointers(void *const *a, void *const *b)
#define SYS_PRId64
Definition: SYS_Types.h:76
T selectNthLargest(exint idx, ComparatorBool is_less={})
Definition: UT_ArrayImpl.h:737
void merge(const UT_ValArray< T > &other, int direction, bool allow_dups)
Definition: UT_ValArray.h:224
bool hasSortedSubset(const UT_Array< T > &other, ComparatorBool is_less) const
Definition: UT_ValArray.h:257
#define UT_API_TMPL
Definition: UT_API.h:15
void sortedUnion(const UT_ValArray< T > &other, ComparatorBool is_less)
Definition: UT_ValArray.h:276
T selectNthLargest(int idx, ComparatorBool is_less)
Definition: UT_ValArray.h:149
exint sortAndRemoveDuplicates()
Definition: UT_ValArray.h:118
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:153
void sortedIntersection(const UT_Array< T > &other, ComparatorBool is_less={})
exint uniqueSortedFind(const T &item) const
Definition: UT_ValArray.h:167
exint uniqueSortedFindAscending(const T &item) const
Definition: UT_ValArray.h:171
#define const
Definition: zconf.h:214
exint uniqueSortedFind(const T &item, Comparator compare) const
Definition: UT_ValArray.h:158
static void clear(UT_ValArray< T > &v)
Definition: UT_ValArray.h:517
T heapPop(Comparator compare)
Definition: UT_ArrayImpl.h:351
UT_ValArray(UT_ValArray< T > &&src) noexcept
Definition: UT_ValArray.h:61
int removeZeros()
Definition: UT_ValArray.h:416
exint heapPush(const T &t, Comparator compare)
Definition: UT_ArrayImpl.h:334
UT_ValArray(const UT_Array< T > &src)
Definition: UT_ValArray.h:58
void display() const
Prints the constents of the array.
Definition: UT_ValArray.h:436
UT_ValArray(std::initializer_list< T > init)
Definition: UT_ValArray.h:81
UT_API void UTsetCompareFloatsTolerance(float tol)
bool hasSortedSubset(const UT_Array< T > &other, ComparatorBool is_less={}) const
T selectNthLargest(int idx)
Definition: UT_ValArray.h:153