HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SIM_PtrArraySorted.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  */
7 
8 #ifndef __SIM_PtrArraySorted_h__
9 #define __SIM_PtrArraySorted_h__
10 
11 #include <UT/UT_ValArray.h>
12 
13 namespace
14 {
15  static inline int
16  comparePointers(void *const* a, void *const* b)
17  {
18  ptrdiff_t pa = ptrdiff_t(*a), pb = ptrdiff_t(*b);
19  if (pa < pb) return -1;
20  if (pa > pb) return 1;
21  return 0;
22  }
23 }
24 
26 template <class T>
28 {
29 public:
31  : myComparator(
32  typename UT_ValArray<T>::Comparator(
33  comparePointers))
34  { }
35 
37  typename UT_ValArray<T>::Comparator comparator)
38  : myComparator(comparator)
39  { }
40  SIM_PtrArraySorted(const SIM_PtrArraySorted &src)
41  {
42  set(src);
43  }
45  { }
46 
47  void setComparator(typename
49  { myComparator = cmp; myArray.sort(myComparator); }
50  void sort()
51  { myArray.sort(myComparator); }
52 
53  exint size() const
54  {
55  return myArray.size();
56  }
57  void setSize(exint newsize)
58  {
59  myArray.setSize(newsize);
60  }
61  void setCapacity(exint newcapacity)
62  {
63  myArray.setCapacity(newcapacity);
64  }
65  exint entries() const
66  { return size(); }
67  void entries(int newnumentries)
68  { setSize(newnumentries); }
69  void resize(int newsize)
70  { setCapacity(newsize); }
71 
72  T operator()(int index) const
73  { return myArray(index); }
74  T last() const
75  { return myArray.last(); }
76  int find(T ptr) const
77  { return myArray.sortedFind(ptr, myComparator); }
78 
79  bool operator==(const SIM_PtrArraySorted &cmp) const
80  {
81  T ptr, cmpptr;
82  int idx, num, diff;
83 
84  if( entries() != cmp.entries() )
85  return false;
86 
87  num = entries();
88  for( idx = 0; idx < num; idx++ )
89  {
90  ptr = myArray(idx);
91  cmpptr = cmp.myArray(idx);
92  diff = myComparator(&ptr, &cmpptr);
93 
94  if( diff != 0 )
95  return false;
96  }
97 
98  return true;
99  }
100 
101  bool containsAny(const SIM_PtrArraySorted &cmp) const
102  {
103  T ptr, cmpptr;
104  int idx, cmpidx, num, numcmp, diff;
105 
106  numcmp = cmp.entries();
107  num = entries();
108  for( idx = 0, cmpidx = 0;
109  idx < num && cmpidx < numcmp; )
110  {
111  ptr = myArray(idx);
112  cmpptr = cmp.myArray(cmpidx);
113  diff = myComparator(&ptr, &cmpptr);
114 
115  if( diff < 0 )
116  {
117  idx++;
118  }
119  else if( diff > 0 )
120  {
121  cmpidx++;
122  }
123  else
124  {
125  return true;
126  }
127  }
128 
129  return false;
130  }
131  bool containsFully(const SIM_PtrArraySorted &cmp) const
132  {
133  T ptr, cmpptr;
134  int idx, cmpidx, num, numcmp, diff;
135 
136  numcmp = cmp.entries();
137  num = entries();
138  for( idx = 0, cmpidx = 0;
139  idx < num && cmpidx < numcmp; )
140  {
141  ptr = myArray(idx);
142  cmpptr = cmp.myArray(cmpidx);
143  diff = myComparator(&ptr, &cmpptr);
144 
145  if( diff < 0 )
146  {
147  idx++;
148  }
149  else if( diff > 0 )
150  {
151  return false;
152  }
153  else
154  {
155  idx++;
156  cmpidx++;
157  }
158  }
159 
160  return (cmpidx >= numcmp) ? true : false;
161  }
162 
163  void remove(int index)
164  { myArray.removeIndex(index); }
165  void remove(T ptr)
166  { int pos = find(ptr); if( pos >= 0 ) remove(pos); }
167  void remove(const SIM_PtrArraySorted &src)
168  {
169  T srcptr, destptr;
170  int idx, srcidx, diff, numsrc;
171  bool removed = false;
172 
173  numsrc = src.entries();
174  for( idx = 0, srcidx = 0;
175  srcidx < numsrc && idx < entries(); )
176  {
177  destptr = myArray(idx);
178  srcptr = src.myArray(srcidx);
179  diff = myComparator(&destptr, &srcptr);
180 
181  if( diff < 0 )
182  {
183  idx++;
184  }
185  else
186  {
187  if( diff == 0 )
188  {
189  myArray(idx) = 0;
190  removed = true;
191  idx++;
192  }
193  srcidx++;
194  }
195  }
196  if( removed )
197  myArray.removeZeros();
198  }
199  void keepOnly(const SIM_PtrArraySorted &src)
200  {
201  T srcptr, destptr;
202  int idx, srcidx, diff, numsrc;
203  bool removed = false;
204 
205  numsrc = src.entries();
206  for( idx = 0, srcidx = 0; idx < entries(); )
207  {
208  if( srcidx < numsrc )
209  {
210  destptr = myArray(idx);
211  srcptr = src.myArray(srcidx);
212  diff = myComparator(&destptr, &srcptr);
213  }
214  else
215  diff = -1;
216 
217  if( diff < 0 )
218  {
219  myArray(idx) = 0;
220  removed = true;
221  idx++;
222  }
223  else
224  {
225  if( diff == 0 )
226  idx++;
227  srcidx++;
228  }
229  }
230  if( removed )
231  myArray.removeZeros();
232  }
233 
234  SIM_PtrArraySorted &operator=(const SIM_PtrArraySorted &src)
235  {
236  set(src);
237  return *this;
238  }
239  int add(T ptr)
240  {
241  return myArray.
242  uniqueSortedInsert(ptr, myComparator);
243  }
244  void set(const SIM_PtrArraySorted &src)
245  {
246  myComparator = src.myComparator;
247  myArray = src.myArray;
248  }
250  SIM_PtrArraySorted &duplicates)
251  {
252  T lastptr, nextptr;
253  int idx;
254 
255  myArray = src;
256  sort();
257  for( idx = 1; idx < entries(); idx++ )
258  {
259  lastptr = myArray(idx-1);
260  nextptr = myArray(idx);
261  if( myComparator(&lastptr, &nextptr) == 0 )
262  {
263  duplicates.add(lastptr);
264  myArray(idx-1) = 0;
265  }
266  }
267  if( duplicates.entries() )
268  myArray.removeZeros();
269  }
270  void merge(const SIM_PtrArraySorted &src)
271  {
272  // shift existing entries
273  int n_src = src.entries();
274  if(!n_src)
275  return;
276  int i = entries();
277  int n = i + n_src;
278  resize(n);
279  entries(n);
280  int idx = n;
281  T srcptr = src.myArray(0);
282  for(--i; i >= 0 && myComparator(&myArray(i), &srcptr) > 0; --i)
283  {
284  myArray(--idx) = myArray(i);
285  }
286  ++i;
287  // merge items into the hole at [i, i+n_src)
288  int merge_start = i;
289  int src_idx = 0;
290  for(;;)
291  {
292  if(idx < n)
293  {
294  if(src_idx == n_src)
295  break;
296  int cmp = myComparator(&myArray(idx), &src.myArray(src_idx));
297  if(cmp < 0)
298  myArray(i++) = myArray(idx++);
299  else
300  myArray(i++) = src.myArray(src_idx++);
301  continue;
302  }
303  while(src_idx < n_src)
304  myArray(i++) = src.myArray(src_idx++);
305  break;
306  }
307  // remove duplicates
308  if(!merge_start)
309  ++merge_start;
310  for(int i = merge_start; i < n; ++i)
311  {
312  if(myComparator(&myArray(i), &myArray(i - 1)))
313  myArray(merge_start++) = myArray(i);
314  }
315  entries(merge_start);
316  }
317  void SYS_DEPRECATED(13.0) mergeSelected(const SIM_PtrArraySorted &src,
318  bool (*selectfunc)(T))
319  {
320  T srcptr, destptr;
321  int idx, srcidx, diff, numsrc;
322 
323  numsrc = src.entries();
324  for( idx = 0, srcidx = 0; srcidx < numsrc; )
325  {
326  // Just skip over any source pointers that
327  // aren't accepted by the select function.
328  if( selectfunc(src.myArray(srcidx)) )
329  {
330  if( idx < entries() )
331  {
332  destptr = myArray(idx);
333  srcptr = src.myArray(srcidx);
334  diff = myComparator(&destptr, &srcptr);
335  }
336  else
337  diff = 1;
338  if( diff < 0 )
339  {
340  idx++;
341  }
342  else
343  {
344  if( diff > 0 )
345  myArray.insert(src(srcidx), idx);
346  idx++;
347  srcidx++;
348  }
349  }
350  else
351  srcidx++;
352  }
353  }
354 
355 
356  // Not that this function can ruin the sort order. Use with caution.
358  { myArray.append(ptr); }
359  // This function gets our contained UT_ValArray.
361  { return myArray; }
362  // This function gets our contained UT_ValArray.
364  { return myArray; }
365 
366 private:
367  typename UT_ValArray<T>::Comparator myComparator;
368  UT_ValArray<T> myArray;
369 };
371 
372 
373 #endif
void merge(const SIM_PtrArraySorted &src)
exint entries() const
#define SYS_DEPRECATED(__V__)
*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
#define SYS_DEPRECATED_PUSH_DISABLE()
#define SYS_DEPRECATED_POP_DISABLE()
int64 exint
Definition: SYS_Types.h:125
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
void resize(int newsize)
void setComparator(typename UT_ValArray< T >::Comparator cmp)
IMATH_HOSTDEVICE constexpr int cmp(T a, T b) IMATH_NOEXCEPT
Definition: ImathFun.h:84
SIM_PtrArraySorted(const SIM_PtrArraySorted &src)
GLdouble n
Definition: glcorearb.h:2008
void setUnsorted(const UT_ValArray< T > &src, SIM_PtrArraySorted &duplicates)
void entries(int newnumentries)
void set(const SIM_PtrArraySorted &src)
bool operator==(const SIM_PtrArraySorted &cmp) const
UT_ValArray< T > & getRawArray()
void setCapacity(exint newcapacity)
void setSize(exint newsize)
int find(T ptr) const
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GLsizeiptr size
Definition: glcorearb.h:664
bool containsAny(const SIM_PtrArraySorted &cmp) const
ImageBuf OIIO_API resize(const ImageBuf &src, string_view filtername="", float filterwidth=0.0f, ROI roi={}, int nthreads=0)
SIM_PtrArraySorted & operator=(const SIM_PtrArraySorted &src)
SIM_PtrArraySorted(typename UT_ValArray< T >::Comparator comparator)
GLuint index
Definition: glcorearb.h:786
auto ptr(T p) -> const void *
Definition: format.h:2448
void keepOnly(const SIM_PtrArraySorted &src)
T operator()(int index) const
void sort(I begin, I end, const Pred &pred)
Definition: pugixml.cpp:7334
bool containsFully(const SIM_PtrArraySorted &cmp) const
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2089
const UT_ValArray< T > & getRawArray() const
GLenum src
Definition: glcorearb.h:1793