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 #include <SYS/SYS_TypeTraits.h>
27 
28 #include <initializer_list>
29 #include <stdio.h>
30 #include <string.h>
31 
32 
33 UT_API extern void UTsetCompareFloatsTolerance(float tol);
34 UT_API extern float UTgetCompareFloatsTolerance();
35 UT_API extern int UTcompareFloats(const float *a, const float *b);
36 UT_API extern int UTcompareInts(const int *a, const int *b);
37 UT_API extern int UTcomparePointers(void *const* a, void *const* b);
38 
39 
41 template <typename T>
43 {
44 public:
45  typedef int (*Comparator)(const T *, const T *);
46 
47  /// Copy constructor
48  /// Copy constructor. It duplicates the data.
49  /// It's marked explicit so that it's not accidentally passed by value.
50  /// You can always pass by reference and then copy it, if needed.
51  /// If you have a line like:
52  /// UT_ValArray<int> a = otherarray;
53  /// and it really does need to copy instead of referencing,
54  /// you can rewrite it as:
55  /// UT_ValArray<int> a(otherarray);
56  explicit UT_ValArray(const UT_ValArray<T> &src)
57  : UT_Array<T>::UT_Array(src)
58  {}
59  explicit UT_ValArray(const UT_Array<T> &src)
60  : UT_Array<T>::UT_Array(src)
61  {}
63  : UT_Array<T>::UT_Array(std::move(src))
64  {}
66  : UT_Array<T>::UT_Array(std::move(src))
67  {}
68  explicit UT_ValArray(exint capacity = 0)
69  : UT_Array<T>::UT_Array(capacity)
70  {}
71  UT_ValArray(exint capacity, exint entries)
72  : UT_Array<T>::UT_Array(capacity, entries)
73  {}
74  /// If you are wondering why we mark this as explicit...
75  /// Imagine you have the following:
76  /// void foo(int i); // 1
77  /// void foo(UT_ValArray<int>); // 2
78  /// Without explicit you can do this
79  /// foo({1})
80  /// and function 1 will be called when you probably meant
81  /// for function 2 to be called.
82  explicit UT_ValArray(std::initializer_list<T> init)
83  : UT_Array<T>::UT_Array(init)
84  {}
85 
87  {
89  return *this;
90  }
92  {
94  return *this;
95  }
96  UT_ValArray &operator=(std::initializer_list<T> src)
97  {
99  return *this;
100  }
102  {
103  UT_Array<T>::operator=(std::move(src));
104  return *this;
105  }
107  {
108  UT_Array<T>::operator=(std::move(src));
109  return *this;
110  }
111 
112  static bool compareElementsBool(const T &a, const T &b)
113  {
114  return a < b;
115  }
116 
117  static int compareElements(const T *a, const T *b)
118  {
119  if (*a > *b)
120  return 1;
121  if (*a < *b)
122  return -1;
123  return 0;
124  }
125 
127  {
129  }
130 
131  exint uniqueSortedFindAscending(const T &item) const
132  {
133  return UT_Array<T>::uniqueSortedFind(item);
134  }
135 
136  exint SYS_DEPRECATED(13.0) uniqueSortedInsertAscending(const T &t)
137  {
139  }
140 
141 private:
142  // SFINAE constraint for bool comparators to avoid ambiguity
143  template <typename F>
144  using IsBoolComp = decltype(std::declval<F>()(std::declval<T>(),
145  std::declval<T>()),
146  void());
147  template <typename Y>
148  using AllowSortedSet = std::enable_if_t<!(
149  SYS_IsSame_v<Y, fpreal32>
150  || SYS_IsSame_v<Y, fpreal64>
151  || SYS_IsSame_v<Y, const char *>)>;
152 public:
153 
156 
157  template <typename T_ = T, typename = AllowSortedSet<T_>>
158  SYS_DEPRECATED_REPLACE(19.5, "Use explicit ComparatorBool")
159  bool hasSortedSubset(const UT_ValArray<T> &other) const
160  {
162  other, compareElements);
163  }
164  SYS_DEPRECATED_HDK_REPLACE(19.5, "Use ComparatorBool variant")
165  bool hasSortedSubset(
166  const UT_ValArray<T> &other,
167  Comparator compare) const
168  {
169  return UT_Array<T>::hasSortedSubset(other, compare);
170  }
171  template <typename ComparatorBool, typename = IsBoolComp<ComparatorBool>>
172  bool hasSortedSubset(const UT_Array<T> &other,
173  ComparatorBool is_less) const
174  {
175  return UT_Array<T>::hasSortedSubset(other, is_less);
176  }
177 
178  template <typename T_ = T, typename = AllowSortedSet<T_>>
179  SYS_DEPRECATED_REPLACE(19.5, "Use explicit ComparatorBool")
180  void sortedUnion(const UT_ValArray<T> &other)
181  {
182  UT_Array<T>::sortedUnion(other, compareElements);
183  }
184  SYS_DEPRECATED_HDK_REPLACE(19.5, "Use ComparatorBool variant")
185  void sortedUnion(
186  const UT_ValArray<T> &other,
187  Comparator compare)
188  {
189  UT_Array<T>::sortedUnion(other, compare);
190  }
191  template <typename ComparatorBool, typename = IsBoolComp<ComparatorBool>>
193  const UT_ValArray<T> &other,
194  ComparatorBool is_less)
195  {
196  UT_Array<T>::sortedUnion(other, is_less);
197  }
198  template <typename T_ = T, typename = AllowSortedSet<T_>>
199  SYS_DEPRECATED_REPLACE(19.5, "Use explicit ComparatorBool")
200  void sortedUnion(
201  const UT_ValArray<T> &other,
202  UT_ValArray<T> &result) const
203  {
205  other, result, compareElements);
206  }
207  SYS_DEPRECATED_HDK_REPLACE(19.5, "Use ComparatorBool variant")
208  void sortedUnion(
209  const UT_ValArray<T> &other,
210  UT_ValArray<T> &result,
211  Comparator compare) const
212  {
213  UT_Array<T>::sortedUnion(other, result, compare);
214  }
215  template <typename ComparatorBool, typename = IsBoolComp<ComparatorBool>>
217  const UT_ValArray<T> &other,
219  ComparatorBool is_less) const
220  {
221  UT_Array<T>::sortedUnion(other, result, is_less);
222  }
223 
224  template <typename T_ = T, typename = AllowSortedSet<T_>>
225  SYS_DEPRECATED_REPLACE(19.5, "Use explicit ComparatorBool")
226  void sortedIntersection(const UT_ValArray<T> &other)
227  {
228  UT_Array<T>::sortedIntersection(other, compareElements);
229  }
230  SYS_DEPRECATED_HDK_REPLACE(19.5, "Use ComparatorBool variant")
231  void sortedIntersection(
232  const UT_ValArray<T> &other,
233  Comparator compare)
234  {
235  UT_Array<T>::sortedIntersection(other, compare);
236  }
237  template <typename ComparatorBool, typename = IsBoolComp<ComparatorBool>>
239  const UT_ValArray<T> &other,
240  ComparatorBool is_less)
241  {
242  UT_Array<T>::sortedIntersection(other, is_less);
243  }
244 
245  template <typename T_ = T, typename = AllowSortedSet<T_>>
246  SYS_DEPRECATED_REPLACE(19.5, "Use explicit ComparatorBool")
247  void sortedIntersection(
248  const UT_ValArray<T> &other,
249  UT_ValArray<T> &result) const
250  {
252  other, result, compareElements);
253  }
254  SYS_DEPRECATED_HDK_REPLACE(19.5, "Use ComparatorBool variant")
255  void sortedIntersection(
256  const UT_ValArray<T> &other,
257  UT_ValArray<T> &result,
258  Comparator compare) const
259  {
260  UT_Array<T>::sortedIntersection(other, result, compare);
261  }
262  template <typename ComparatorBool, typename = IsBoolComp<ComparatorBool>>
264  const UT_ValArray<T> &other,
266  ComparatorBool is_less) const
267  {
268  UT_Array<T>::sortedIntersection(other, result, is_less);
269  }
270 
271  template <typename T_ = T, typename = AllowSortedSet<T_>>
272  SYS_DEPRECATED_REPLACE(19.5, "Use explicit ComparatorBool")
273  void sortedSetDifference(const UT_ValArray<T> &other)
274  {
276  other, compareElements);
277  }
278  SYS_DEPRECATED_HDK_REPLACE(19.5, "Use ComparatorBool variant")
279  void sortedSetDifference(
280  const UT_ValArray<T> &other,
281  Comparator compare)
282  {
283  UT_Array<T>::sortedSetDifference(other, compare);
284  }
285  template <typename ComparatorBool, typename = IsBoolComp<ComparatorBool>>
287  const UT_ValArray<T> &other,
288  ComparatorBool is_less)
289  {
290  UT_Array<T>::sortedSetDifference(other, is_less);
291  }
292 
293  template <typename T_ = T, typename = AllowSortedSet<T_>>
294  SYS_DEPRECATED_REPLACE(19.5, "Use explicit ComparatorBool")
295  void sortedSetDifference(
296  const UT_ValArray<T> &other,
297  UT_ValArray<T> &result) const
298  {
300  other, result, compareElements);
301  }
302  SYS_DEPRECATED_HDK_REPLACE(19.5, "Use ComparatorBool variant")
303  void sortedSetDifference(
304  const UT_ValArray<T> &other,
305  UT_ValArray<T> &result,
306  Comparator compare) const
307  {
309  other, result, compare);
310  }
311  template <typename ComparatorBool, typename = IsBoolComp<ComparatorBool>>
313  const UT_ValArray<T> &other,
315  ComparatorBool is_less) const
316  {
318  other, result, is_less);
319  }
320 
322 
323  static bool isElementZero(const T &a)
324  {
325  return !a;
326  }
327 
329  {
330  return this->removeIf(isElementZero);
331  }
332 
333  // Remove zeros and also sets the capacity of the array.
334  void collapse()
335  {
336  this->collapseIf(isElementZero);
337  }
338 
339  /// Functions which are only specialized for int{32,64}, fpreal{32,64}
340  // @{
341  /// Returns the sum of the entries in the array.
342  T sum() const
343  {
344  UT_ASSERT(!"Invalid function");
345  return T();
346  }
347  /// Prints the constents of the array
348  void display() const
349  {
350  printf("%" SYS_PRId64 " entries, contents not displayable.\n",
352  }
353  // @}
354 };
355 
357 
358 template <>
359 inline int
360 UT_ValArray<fpreal32>::compareElements(const fpreal32 *a, const fpreal32 *b)
361 {
362  const float tol = UTgetCompareFloatsTolerance();
363  if( *a>*b+tol )
364  return 1;
365  if( *a<*b-tol )
366  return -1;
367  return 0;
368 }
369 
370 template <>
371 inline int
373 {
374  const float tol = UTgetCompareFloatsTolerance();
375  if( *a>*b+tol )
376  return 1;
377  if( *a<*b-tol )
378  return -1;
379  return 0;
380 }
381 
382 #define UT_DECL_ARITHMETIC_SPECIALIZATION(T) \
383  template <> UT_API T UT_ValArray<T>::sum() const; \
384  template <> UT_API void UT_ValArray<T>::display() const; \
385  /**/
390 #undef UT_DECL_ARITHMETIC_SPECIALIZATION
391 
392 template <>
393 inline int
394 UT_ValArray<const char *>::compareElements(const char * const *a,
395  const char * const *b)
396 {
397  if (*a && *b) return strcmp(*a, *b);
398  // Use the same comparison order for NULL vs non-NULL as UT_String.
399  // Don't think it's correct, however.
400  if (*a) return -1;
401  if (*b) return 1;
402  return 0;
403 }
404 
405 // I don't know why this is needed, given that there's one for UT_Array,
406 // but UTswap called on two UT_ValArray's was using the default "copy
407 // to temp" function.
409 
411 
412 // For UT::ArraySet.
413 namespace UT
414 {
415 template <typename T>
416 struct DefaultClearer;
417 
418 template <typename T>
420 {
421  static void clear(UT_ValArray<T> &v) { v.setCapacity(0); }
422  static bool isClear(const UT_ValArray<T> &v) { return v.capacity() == 0; }
424  {
425  new ((void *)p) UT_ValArray<T>();
426  }
427  static const bool clearNeedsDestruction = false;
428 };
429 } // namespace UT
430 
431 #endif // __UT_VALARRAY_H_INCLUDED__
UT_ValArray & operator=(UT_Array< T > &&src)
Definition: UT_ValArray.h:106
#define UT_DECL_ARITHMETIC_SPECIALIZATION(T)
Definition: UT_ValArray.h:382
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
UT_ValArray(exint capacity=0)
Definition: UT_ValArray.h:68
typename std::enable_if< B, T >::type enable_if_t
Define Imath::enable_if_t to be std for C++14, equivalent for C++11.
UT_ValArray & operator=(UT_ValArray< T > &&src)
Definition: UT_ValArray.h:101
void sortedSetDifference(const UT_ValArray< T > &other, UT_ValArray< T > &result, ComparatorBool is_less) const
Definition: UT_ValArray.h:312
UT_ValArray & operator=(std::initializer_list< T > src)
Definition: UT_ValArray.h:96
int int32
Definition: SYS_Types.h:39
UT_ValArray & operator=(const UT_Array< T > &src)
Definition: UT_ValArray.h:91
static void clearConstruct(UT_ValArray< T > *p)
Definition: UT_ValArray.h:423
#define SYS_DEPRECATED(__V__)
#define SYS_PRAGMA_PUSH_WARN()
Definition: SYS_Pragma.h:34
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:263
void
Definition: png.h:1083
const GLdouble * v
Definition: glcorearb.h:837
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)
#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:899
int64 exint
Definition: SYS_Types.h:125
UT_API int UTcompareInts(const int *a, const int *b)
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
UT_ValArray< const char * > UT_StringList
Definition: UT_ValArray.h:410
#define UT_API
Definition: UT_API.h:14
void setCapacity(exint new_capacity)
**But if you need a result
Definition: thread.h:613
exint uniqueSortedInsert(const T &t, Comparator compare)
Definition: UT_Array.h:233
void collapse()
Definition: UT_ValArray.h:334
float fpreal32
Definition: SYS_Types.h:200
static bool compareElementsBool(const T &a, const T &b)
Definition: UT_ValArray.h:112
void sortedUnion(const UT_Array< T > &other, ComparatorBool is_less={})
#define SYS_PRAGMA_DISABLE_DEPRECATED()
Definition: SYS_Pragma.h:48
double fpreal64
Definition: SYS_Types.h:201
#define SYS_DEPRECATED_REPLACE(__V__, __R__)
void sortedUnion(const UT_ValArray< T > &other, UT_ValArray< T > &result, ComparatorBool is_less) const
Definition: UT_ValArray.h:216
void sortAscending()
Definition: UT_ValArray.h:126
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:422
UT_API float UTgetCompareFloatsTolerance()
void sort(ComparatorBool is_less={})
Sort using std::sort with bool comparator. Defaults to operator<().
Definition: UT_Array.h:456
UT_ValArray & operator=(const UT_ValArray< T > &src)
Definition: UT_ValArray.h:86
UT_ValArray(exint capacity, exint entries)
Definition: UT_ValArray.h:71
exint capacity() const
Definition: UT_ArrayImpl.h:143
UT_ValArray(const UT_ValArray< T > &src)
Definition: UT_ValArray.h:56
T sum() const
Functions which are only specialized for int{32,64}, fpreal{32,64}.
Definition: UT_ValArray.h:342
UT_SWAPPER_TEMPLATE(UT_ValArray)
long long int64
Definition: SYS_Types.h:116
static int compareElements(const T *a, const T *b)
Definition: UT_ValArray.h:117
void sortedSetDifference(const UT_ValArray< T > &other, ComparatorBool is_less)
Definition: UT_ValArray.h:286
#define SYS_PRAGMA_POP_WARN()
Definition: SYS_Pragma.h:35
void sortedSetDifference(const UT_Array< T > &other, ComparatorBool is_less={})
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
UT_ValArray(UT_Array< T > &&src) noexcept
Definition: UT_ValArray.h:65
GLdouble t
Definition: glad.h:2397
void sortedIntersection(const UT_ValArray< T > &other, ComparatorBool is_less)
Definition: UT_ValArray.h:238
UT_API int UTcomparePointers(void *const *a, void *const *b)
#define SYS_PRId64
Definition: SYS_Types.h:76
bool hasSortedSubset(const UT_Array< T > &other, ComparatorBool is_less) const
Definition: UT_ValArray.h:172
#define UT_API_TMPL
Definition: UT_API.h:15
void sortedUnion(const UT_ValArray< T > &other, ComparatorBool is_less)
Definition: UT_ValArray.h:192
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
void sortedIntersection(const UT_Array< T > &other, ComparatorBool is_less={})
exint uniqueSortedFindAscending(const T &item) const
Definition: UT_ValArray.h:131
static void clear(UT_ValArray< T > &v)
Definition: UT_ValArray.h:421
UT_ValArray(UT_ValArray< T > &&src) noexcept
Definition: UT_ValArray.h:62
int removeZeros()
Definition: UT_ValArray.h:328
UT_ValArray(const UT_Array< T > &src)
Definition: UT_ValArray.h:59
void display() const
Prints the constents of the array.
Definition: UT_ValArray.h:348
UT_ValArray(std::initializer_list< T > init)
Definition: UT_ValArray.h:82
UT_API void UTsetCompareFloatsTolerance(float tol)
bool hasSortedSubset(const UT_Array< T > &other, ComparatorBool is_less={}) const
GLenum src
Definition: glcorearb.h:1793