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_Swap.h"
20 #include "UT_VectorTypes.h"
21 
22 #include <SYS/SYS_Compiler.h>
23 #include <SYS/SYS_Deprecated.h>
24 #include <SYS/SYS_Types.h>
25 
26 #include <initializer_list>
27 #include <stdio.h>
28 #include <string.h>
29 
30 
31 UT_API extern void UTsetCompareFloatsTolerance(float tol);
32 UT_API extern float UTgetCompareFloatsTolerance();
33 UT_API extern int UTcompareFloats(const float *a, const float *b);
34 UT_API extern int UTcompareInts(const int *a, const int *b);
35 UT_API extern int UTcomparePointers(void *const* a, void *const* b);
36 
37 
39 template <typename T>
40 class UT_API_TMPL UT_ValArray : public UT_Array<T>
41 {
42 public:
43  typedef int (*Comparator)(const T *, const T *);
44 
45  /// Copy constructor
46  /// Copy constructor. It duplicates the data.
47  /// It's marked explicit so that it's not accidentally passed by value.
48  /// You can always pass by reference and then copy it, if needed.
49  /// If you have a line like:
50  /// UT_ValArray<int> a = otherarray;
51  /// and it really does need to copy instead of referencing,
52  /// you can rewrite it as:
53  /// UT_ValArray<int> a(otherarray);
54  explicit UT_ValArray(const UT_ValArray<T> &src)
55  : UT_Array<T>::UT_Array(src)
56  {}
57  explicit UT_ValArray(const UT_Array<T> &src)
58  : UT_Array<T>::UT_Array(src)
59  {}
61  : UT_Array<T>::UT_Array(std::move(src))
62  {}
64  : UT_Array<T>::UT_Array(std::move(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  explicit UT_ValArray(std::initializer_list<T> init)
73  : UT_Array<T>::UT_Array(init)
74  {}
75 
77  {
79  return *this;
80  }
82  {
84  return *this;
85  }
86  UT_ValArray &operator=(std::initializer_list<T> src)
87  {
89  return *this;
90  }
92  {
93  UT_Array<T>::operator=(std::move(src));
94  return *this;
95  }
97  {
98  UT_Array<T>::operator=(std::move(src));
99  return *this;
100  }
101 
102  static bool compareElementsBool(const T &a, const T &b)
103  {
104  return a < b;
105  }
106 
107  /// Sort and then remove all duplicate entries. This returns the number of
108  /// elements removed.
110  {
111  stableSort(compareElementsBool);
113  }
114 
115  static int compareElements(const T *a, const T *b)
116  {
117  if (*a > *b)
118  return 1;
119  if (*a < *b)
120  return -1;
121  return 0;
122  }
123 
124  void sort(Comparator compare)
125  {
126  UT_Array<T>::sort(compare);
127  }
128  template <typename ComparatorBool>
129  void stdsort(ComparatorBool is_less)
130  {
131  UT_Array<T>::stdsort(is_less);
132  }
133  void sort()
134  {
135  stdsort(Less());
136  }
138  {
139  sort();
140  }
141 
142  template<typename ComparatorBool>
143  void stableSort(ComparatorBool is_less)
144  {
145  UT_Array<T>::stableSort(is_less);
146  }
147  void stableSort()
148  {
149  stableSort(Less());
150  }
151 
152  template <typename ComparatorBool>
153  T selectNthLargest(int idx, ComparatorBool is_less)
154  {
155  return UT_Array<T>::selectNthLargest(idx, is_less);
156  }
157  T selectNthLargest(int idx)
158  {
159  return selectNthLargest(idx, Less());
160  }
161 
162  exint uniqueSortedFind(const T &item, Comparator compare) const
163  {
164  return UT_Array<T>::uniqueSortedFind(item, compare);
165  }
166  template <typename ComparatorBool>
167  exint uniqueSortedFind(const T &item, ComparatorBool is_less) const
168  {
169  return UT_Array<T>::uniqueSortedFind(item, is_less);
170  }
171  exint uniqueSortedFind(const T &item) const
172  {
173  return uniqueSortedFind(item, Less());
174  }
175  exint uniqueSortedFindAscending(const T &item) const
176  {
177  return uniqueSortedFind(item);
178  }
179 
180  SYS_DEPRECATED_HDK(13.0)
181  exint sortedInsert(const T &t, Comparator compare)
182  {
183  return UT_Array<T>::sortedInsert(t, compare);
184  }
185  template <typename ComparatorBool>
186  SYS_DEPRECATED_HDK(13.0)
187  exint sortedInsert(const T &t, ComparatorBool is_less)
188  {
189  return UT_Array<T>::sortedInsert(t, is_less);
190  }
191  SYS_DEPRECATED_HDK(13.0)
192  exint sortedInsert(const T &t)
193  {
194  return sortedInsert(t, Less());
195  }
196 
197  SYS_DEPRECATED_HDK(13.0)
198  exint uniqueSortedInsert(const T &t, Comparator compare)
199  {
200  return UT_Array<T>::uniqueSortedInsert(t, compare);
201  }
202  template <typename ComparatorBool>
203  SYS_DEPRECATED_HDK(13.0)
204  exint uniqueSortedInsert(const T &t, ComparatorBool is_less)
205  {
206  return UT_Array<T>::uniqueSortedInsert(t, is_less);
207  }
208  SYS_DEPRECATED_HDK(13.0)
209  exint uniqueSortedInsert(const T &t)
210  {
211  return uniqueSortedInsert(t, Less());
212  }
213  exint SYS_DEPRECATED(13.0) uniqueSortedInsertAscending(const T &t)
214  {
215  return uniqueSortedInsert(t);
216  }
217 
218  template <typename ComparatorBool>
219  void merge(
220  const UT_Array<T> &other,
221  int direction,
222  bool allow_dups,
223  ComparatorBool is_less)
224  {
225  UT_Array<T>::merge(other, direction, allow_dups,
226  is_less);
227  }
228  void merge(
229  const UT_ValArray<T> &other,
230  int direction,
231  bool allow_dups)
232  {
233  merge(other, direction, allow_dups, Less());
234  }
235 
237  const UT_ValArray<T> &other,
238  Comparator compare = compareElements) const
239  {
240  return UT_Array<T>::hasSortedSubset(other, compare);
241  }
243  const UT_ValArray<T> &other,
244  Comparator compare = compareElements)
245  {
247  }
249  const UT_ValArray<T> &other,
250  UT_ValArray<T> &result,
251  Comparator compare = compareElements) const
252  {
253  UT_Array<T>::sortedUnion(other, result, compare);
254  }
256  const UT_ValArray<T> &other,
257  Comparator compare = compareElements)
258  {
260  }
262  const UT_ValArray<T> &other,
263  UT_ValArray<T> &result,
264  Comparator compare = compareElements) const
265  {
267  }
269  const UT_ValArray<T> &other,
270  Comparator compare = compareElements)
271  {
273  }
275  const UT_ValArray<T> &other,
276  UT_ValArray<T> &result,
277  Comparator compare = compareElements) const
278  {
280  }
281 
282  exint heapPush(const T &t, Comparator compare)
283  {
284  return UT_Array<T>::heapPush(t, compare);
285  }
286  T heapPop(Comparator compare)
287  {
288  return UT_Array<T>::heapPop(compare);
289  }
290 
291  static bool isElementZero(const T &a)
292  {
293  return !a;
294  }
295 
297  {
298  return this->removeIf(isElementZero);
299  }
300 
301  // Remove zeros and also sets the capacity of the array.
302  void collapse()
303  {
304  this->collapseIf(isElementZero);
305  }
306 
307  /// Functions which are only specialized for int{32,64}, fpreal{32,64}
308  // @{
309  /// Returns the sum of the entries in the array.
310  T sum() const
311  {
312  UT_ASSERT(!"Invalid function");
313  return T();
314  }
315  /// Prints the constents of the array
316  void display() const
317  {
318  printf("%" SYS_PRId64 " entries, contents not displayable.\n",
320  }
321  // @}
322 
323 private:
324  // manual wrapper for std functors to avoid including <functional>
325  struct Less
326  {
327  bool operator()(const T& lhs, const T& rhs) const
328  { return lhs < rhs; }
329  };
330 };
331 
333 
334 template <>
335 inline int
336 UT_ValArray<fpreal32>::compareElements(const fpreal32 *a, const fpreal32 *b)
337 {
338  const float tol = UTgetCompareFloatsTolerance();
339  if( *a>*b+tol )
340  return 1;
341  if( *a<*b-tol )
342  return -1;
343  return 0;
344 }
345 
346 template <>
347 inline int
349 {
350  const float tol = UTgetCompareFloatsTolerance();
351  if( *a>*b+tol )
352  return 1;
353  if( *a<*b-tol )
354  return -1;
355  return 0;
356 }
357 
358 #define UT_DECL_ARITHMETIC_SPECIALIZATION(T) \
359  template <> UT_API T UT_ValArray<T>::sum() const; \
360  template <> UT_API void UT_ValArray<T>::display() const; \
361  /**/
366 #undef UT_DECL_ARITHMETIC_SPECIALIZATION
367 
368 template <>
369 inline int
370 UT_ValArray<const char *>::compareElements(const char * const *a,
371  const char * const *b)
372 {
373  if (*a && *b) return strcmp(*a, *b);
374  // Use the same comparison order for NULL vs non-NULL as UT_String.
375  // Don't think it's correct, however.
376  if (*a) return -1;
377  if (*b) return 1;
378  return 0;
379 }
380 
381 /// I don't know why this is needed, given that there's one for UT_Array,
382 /// but UTswap called on two UT_ValArray's was using the default "copy
383 /// to temp" function.
385 
387 
388 // For UT::ArraySet.
389 namespace UT
390 {
391 template <typename T>
392 struct DefaultClearer;
393 
394 template <typename T>
396 {
397  static void clear(UT_ValArray<T> &v) { v.setCapacity(0); }
398  static bool isClear(const UT_ValArray<T> &v) { return v.capacity() == 0; }
400  {
401  new ((void *)p) UT_ValArray<T>();
402  }
403  static const bool clearNeedsDestruction = false;
404 };
405 } // namespace UT
406 
407 #endif // __UT_VALARRAY_H_INCLUDED__
UT_ValArray & operator=(UT_Array< T > &&src)
Definition: UT_ValArray.h:96
#define UT_DECL_ARITHMETIC_SPECIALIZATION(T)
Definition: UT_ValArray.h:358
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:219
exint heapPush(const T &t, Comparator compare)
Definition: UT_ValArray.h:282
UT_ValArray & operator=(UT_ValArray< T > &&src)
Definition: UT_ValArray.h:91
UT_ValArray & operator=(std::initializer_list< T > src)
Definition: UT_ValArray.h:86
UT_ValArray & operator=(const UT_Array< T > &src)
Definition: UT_ValArray.h:81
static void clearConstruct(UT_ValArray< T > *p)
Definition: UT_ValArray.h:399
#define SYS_DEPRECATED(__V__)
UT_ValArray(UT_Array< T > &&src) SYS_NOEXCEPT
Definition: UT_ValArray.h:63
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:959
#define SYS_DEPRECATED_PUSH_DISABLE()
const GLdouble * v
Definition: glcorearb.h:836
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:386
void stdsort(ComparatorBool is_less)
Sort using std::sort. The ComparatorBool uses the less-than semantics.
Definition: UT_Array.h:294
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:268
#define UT_API
Definition: UT_API.h:13
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:242
int compare(const char *str, bool case_sensitive=true) const
Definition: UT_String.h:379
void collapse()
Definition: UT_ValArray.h:302
static bool compareElementsBool(const T &a, const T &b)
Definition: UT_ValArray.h:102
void sort()
Definition: UT_ValArray.h:133
T heapPop(Comparator compare)
Definition: UT_ValArray.h:286
void sortedSetDifference(const UT_ValArray< T > &other, UT_ValArray< T > &result, Comparator compare=compareElements) const
Definition: UT_ValArray.h:274
void stableSort()
Definition: UT_ValArray.h:147
void sortAscending()
Definition: UT_ValArray.h:137
long long int64
Definition: SYS_Types.h:107
static bool isClear(const UT_ValArray< T > &v)
Definition: UT_ValArray.h:398
void setCapacity(exint newcapacity)
Definition: UT_ArrayImpl.h:751
UT_API float UTgetCompareFloatsTolerance()
bool hasSortedSubset(const UT_ValArray< T > &other, Comparator compare=compareElements) const
Definition: UT_ValArray.h:236
int64 exint
Definition: SYS_Types.h:116
exint uniqueSortedFind(const T &item, ComparatorBool is_less) const
Definition: UT_ValArray.h:167
UT_ValArray & operator=(const UT_ValArray< T > &src)
Definition: UT_ValArray.h:76
UT_ValArray(exint capacity, exint entries)
Definition: UT_ValArray.h:69
double fpreal64
Definition: SYS_Types.h:192
void sort(Comparator compare)
Definition: UT_ValArray.h:124
exint capacity() const
Definition: UT_Array.h:448
exint sortedInsert(const T &t, Comparator compare)
Definition: UT_ArrayImpl.h:219
UT_ValArray(const UT_ValArray< T > &src)
Definition: UT_ValArray.h:54
T sum() const
Functions which are only specialized for int{32,64}, fpreal{32,64}.
Definition: UT_ValArray.h:310
UT_SWAPPER_TEMPLATE(UT_ValArray)
void sortedIntersection(const UT_ValArray< T > &other, UT_ValArray< T > &result, Comparator compare=compareElements) const
Definition: UT_ValArray.h:261
static int compareElements(const T *a, const T *b)
Definition: UT_ValArray.h:115
#define SYS_DEPRECATED_HDK(__V__)
int int32
Definition: SYS_Types.h:35
exint sortedRemoveDuplicates()
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
static bool isElementZero(const T &a)
Definition: UT_ValArray.h:291
void stableSort(ComparatorBool is_less)
Definition: UT_ValArray.h:143
UT_API int UTcomparePointers(void *const *a, void *const *b)
#define SYS_PRId64
Definition: SYS_Types.h:67
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:228
void stdsort(ComparatorBool is_less)
Definition: UT_ValArray.h:129
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:55
void sortedIntersection(const UT_ValArray< T > &other, Comparator compare=compareElements)
Definition: UT_ValArray.h:255
#define UT_API_TMPL
Definition: UT_API.h:14
T selectNthLargest(int idx, ComparatorBool is_less)
Definition: UT_ValArray.h:153
void sort(Comparator compare)
Definition: UT_ArrayImpl.h:726
exint sortAndRemoveDuplicates()
Definition: UT_ValArray.h:109
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:126
exint uniqueSortedFind(const T &item) const
Definition: UT_ValArray.h:171
void sortedIntersection(const UT_Array< T > &other, Comparator compare)
exint uniqueSortedFindAscending(const T &item) const
Definition: UT_ValArray.h:175
#define const
Definition: zconf.h:214
void stableSort(ComparatorBool is_less)
Definition: UT_Array.h:308
exint uniqueSortedFind(const T &item, Comparator compare) const
Definition: UT_ValArray.h:162
static void clear(UT_ValArray< T > &v)
Definition: UT_ValArray.h:397
T heapPop(Comparator compare)
Definition: UT_ArrayImpl.h:377
int removeZeros()
Definition: UT_ValArray.h:296
exint heapPush(const T &t, Comparator compare)
Definition: UT_ArrayImpl.h:360
UT_ValArray(const UT_Array< T > &src)
Definition: UT_ValArray.h:57
void display() const
Prints the constents of the array.
Definition: UT_ValArray.h:316
float fpreal32
Definition: SYS_Types.h:191
UT_ValArray(std::initializer_list< T > init)
Definition: UT_ValArray.h:72
void sortedUnion(const UT_ValArray< T > &other, UT_ValArray< T > &result, Comparator compare=compareElements) const
Definition: UT_ValArray.h:248
UT_API void UTsetCompareFloatsTolerance(float tol)
T selectNthLargest(int idx)
Definition: UT_ValArray.h:157
GLenum src
Definition: glcorearb.h:1792