HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OP_Dependency.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: OP_Dependency.h (C++)
7  *
8  * COMMENTS:
9  * OP_Dependency is used to keep track of op dependencies
10  */
11 
12 #ifndef _OP_Depdency_h_
13 #define _OP_Depdency_h_
14 
15 #include "OP_API.h"
16 
17 #include "OP_DataTypes.h"
18 #include "OP_Output.h"
19 
20 #include <PRM/PRM_RefId.h>
21 #include <UT/UT_Array.h>
22 #include <UT/UT_ArrayMap.h>
23 #include <UT/UT_Assert.h>
24 #include <UT/UT_UniquePtr.h>
25 
27 {
28 public:
30  {
31  myRefOpId = -1;
32  myRefCount = 0;
33  }
34  explicit OP_Reference( int ref_op_id )
35  {
36  myRefOpId = ref_op_id;
37  myRefCount = 1;
38  }
39 
40  void addRef()
41  {
42  UT_ASSERT_P( myRefCount >= 0 );
43  myRefCount++;
44  }
45  int removeRef(int count = 1)
46  {
47  UT_ASSERT_P( myRefCount >= count );
48  if( myRefCount >= count )
49  myRefCount -= count;
50  else
51  myRefCount = 0;
52  return myRefCount;
53  }
54 
55  void clearRefOpId() { myRefOpId = -1; }
56  int getRefOpId() const { return myRefOpId; }
57  void setRefOpId(int op_id) { myRefOpId = op_id; }
58 
59  int getRefCount() const { return myRefCount; }
60 
61  unsigned hash() const
62  {
63  return SYSwang_inthash(myRefOpId);
64  }
65  int operator==( const OP_Reference &other )
66  {
67  return myRefOpId == other.myRefOpId;
68  }
69 
70 private:
71  int myRefOpId;
72  int myRefCount;
73 };
75 
77 {
78 public:
80  {
81  myRefOpId = -1;
82  myInterestType = OP_INTEREST_NONE;
83  }
84 
85  // ref_op_id - The ID of the op who is dependent on our owner
86  // source_ref - A reference to a parm/channel in our owner on which
87  // ref_op_id is dependent
88  // ref - The parm/chan in the dependent that is dependent on our
89  // owner
90  // interest - The kind of interest the dependent will have
91  OP_Dependency(int ref_op_id, const PRM_RefId &source_ref,
92  const PRM_RefId &ref, OP_InterestType interest)
93  {
94  myRefOpId = ref_op_id;
95  myRef = ref;
96  myInterestType = interest;
97  mySourceRef = source_ref;
98  }
99 
100  OP_Dependency(const OP_Dependency &other) = default;
101  OP_Dependency &operator=(const OP_Dependency &other) = default;
102 
104  {
105  myInterestType = (OP_InterestType) ((int) myInterestType | type);
106  }
108  {
109  return myInterestType;
110  }
111 
112  int matches( int ref_op_id, const PRM_RefId &ref,
114  {
115  return (myRefOpId == ref_op_id && myRef.matches(ref)
116  && hasInterest(mask));
117  }
118  int matches( int ref_op_id, const PRM_RefId &source_ref,
119  const PRM_RefId &ref,
121  {
122  return myRefOpId == ref_op_id && mySourceRef == source_ref &&
123  myRef == ref && hasInterest(mask);
124  }
125  unsigned hash() const
126  {
127  uint h = SYSwang_inthash(myRefOpId);
128  h = h * 37 + SYSwang_inthash(myInterestType);
129  h = h * 37 + mySourceRef.hash();
130  h = h * 37 + myRef.hash();
131  return h;
132  }
133 
134  int operator==( const OP_Dependency &other ) const
135  {
136  return matches( other.myRefOpId, other.mySourceRef, other.myRef );
137  }
138 
139  // Returns true if the source ref matches exactly the removed parm index
140  bool fixRemovedSourceRefParmIndex(int removed_parm_idx)
141  {
142  if (!mySourceRef.isValid())
143  return false;
144 
145  if (mySourceRef.getParmRef() == removed_parm_idx)
146  {
147  UT_ASSERT_P(false);
148  mySourceRef.setParmRef(-1, -1);
149  return true;
150  }
151  else if (mySourceRef.getParmRef() > removed_parm_idx)
152  {
153  mySourceRef.setParmRef(mySourceRef.getParmRef() - 1,
154  mySourceRef.getParmSubIndex());
155  }
156  return false;
157  }
158 
159  int getRefOpId() const { return myRefOpId; }
160  void clearRefOpId() { myRefOpId = -1; }
161  void setRefOpId( int op_id ) { myRefOpId = op_id; }
162  const PRM_RefId &getRefId() const { return myRef; }
163  const PRM_RefId &getSourceRefId() const { return mySourceRef; }
164  OP_InterestType getInterestType() const { return myInterestType; }
165  int hasInterest(OP_InterestType interest) const
166  { return (myInterestType & interest) != 0; }
167 
168 private:
169  int myRefOpId;
170  PRM_RefId mySourceRef;
171  PRM_RefId myRef;
172  OP_InterestType myInterestType;
173 };
174 
175 // Storage for dependency information for a single node's dependents
177 {
178 public:
180  {
181  }
183  : myList(src.myList)
184  {
185  }
187  {
188  }
189 
190  bool isEmpty() const { return myList.entries() == 0; }
191  void clear() { myList.entries(0); }
192  void append(const OP_Dependency &v) { myList.append(v); }
193  int64 getMemoryUsage(bool inclusive) const
194  {
195  int64 mem = inclusive ? sizeof(*this) : 0;
196  mem += myList.getMemoryUsage(false);
197  return mem;
198  }
199 
203 
204  iterator begin() { return myList.begin(); }
205  iterator end() { return myList.end(); }
206  const_iterator begin() const { return myList.begin(); }
207  const_iterator end() const { return myList.end(); }
208  reverse_iterator rbegin() { return myList.rbegin(); }
209  reverse_iterator rend() { return myList.rend(); }
210 
211  void removeItem(const reverse_iterator &it)
212  {
213  myList.removeItem(it);
214  }
215 private:
216  // Called by OP_DependencyList
217  int getRefOpId() const
218  {
219  UT_ASSERT(myList.entries());
220  return myList(0).getRefOpId();
221  }
222  friend class OP_DependencyList;
224 };
225 
227 {
228 public:
230  : myEntries(0)
231  {
232  }
235  {
236  clear();
237  }
239  bool isEmpty() const { return myEntries == 0; }
240  void clear();
241  void append(const OP_Dependency &v);
242  int64 getMemoryUsage(bool inclusive) const;
243 
244  /// Find a dependency for the given criteria
245  OP_Dependency *find(int ref_op_id, const PRM_RefId &source_ref,
246  const PRM_RefId &ref_id) const;
247  OP_Dependency *find(int ref_op_id, const PRM_RefId &ref_id) const;
248 
249  /// @{
250  /// Remove all matching dependencies. Returns number of items removed
251  int removeMatching(int ref_op_id, OP_InterestType mask);
252  int removeMatching(int ref_op_id, const PRM_RefId &ref_id,
254  /// @}
255  /// Access to the hash table
257  {
259  public:
261  : myOwner(NULL)
262  , myNodeCache()
263  , myNodeIterator()
264  , myListIterator()
265  {
266  }
268  : myOwner(it.myOwner)
269  , myNodeCache(it.myNodeCache)
270  , myNodeIterator(it.myNodeIterator)
271  , myListIterator(it.myListIterator)
272  {
273  }
275 
277  {
278  return *myListIterator;
279  }
281  {
282  return *myListIterator;
283  }
285  {
286  return &getDependency();
287  }
288 
290  {
291  myOwner = cmp.myOwner;
292  myNodeCache = cmp.myNodeCache;
293  myNodeIterator = cmp.myNodeIterator;
294  myListIterator = cmp.myListIterator;
295  return *this;
296  }
298  {
299  advance();
300  return *this;
301  }
303  {
304  advance();
305  return *this;
306  }
307  bool operator==(const reverse_iterator &cmp) const
308  {
309  return myOwner == cmp.myOwner &&
310  myNodeIterator == cmp.myNodeIterator &&
311  myListIterator == cmp.myListIterator &&
312  myNodeCache == cmp.myNodeCache;
313  }
314  bool operator!=(const reverse_iterator &cmp) const
315  {
316  return !(*this == cmp);
317  }
318  void advance()
319  {
320  myListIterator.advance();
321  while (myListIterator.atEnd())
322  {
323  ++myNodeIterator;
324  if (myNodeIterator.atEnd())
325  break;
326  myListIterator = getDependencyNode()->rbegin();
327  }
328  }
329 
330  void rewind()
331  {
332  const auto &dep_map = myOwner->myDependencyMap;
333  myNodeCache.entries(0);
334  myNodeCache.setCapacityIfNeeded(dep_map.size());
335  for (const auto &node : dep_map.mapped_range())
336  {
337  if (!node->isEmpty())
338  myNodeCache.append(node.get());
339  }
340  myNodeIterator = myNodeCache.begin();
341  if (!myNodeIterator.atEnd())
342  {
343  myListIterator = getDependencyNode()->rbegin();
344  }
345  else
346  {
347  myListIterator = OP_DependencyNode::reverse_iterator();
348  }
349  }
350 
351  bool atEnd() const
352  {
353  return myNodeIterator.atEnd() && myListIterator.atEnd();
354  }
355  private:
356  reverse_iterator(const OP_DependencyList *owner)
357  : myOwner(owner)
358  , myNodeIterator()
359  , myListIterator()
360  {
361  rewind();
362  }
363 
364  // Use in private methods to OP_DependencyList
365  OP_DependencyNode *getDependencyNode() const
366  {
367  return myNodeIterator.item();
368  }
369  const OP_DependencyNode::reverse_iterator &getListIterator() const
370  {
371  return myListIterator;
372  }
373 
374 
375  const OP_DependencyList *myOwner;
376  NodeCache myNodeCache;
377  NodeCache::iterator myNodeIterator;
379  friend class OP_DependencyList;
380  };
382 
383  const_iterator begin() const { return const_iterator(this); }
384  const_iterator end() const { return const_iterator(); }
385  reverse_iterator rbegin() const { return reverse_iterator(this); }
386  reverse_iterator rend() const { return reverse_iterator(); }
387 
388  void removeItem(const reverse_iterator &it);
389 
390 private:
391  friend class reverse_iterator;
392  OP_DependencyNode *findNode(int op_id) const;
393 
395 
396  DependencyMap myDependencyMap;
397  exint myEntries;
398 };
399 
400 #endif // _OP_Depdency_h_
int operator==(const OP_Dependency &other) const
bool isEmpty() const
OP_Dependency(int ref_op_id, const PRM_RefId &source_ref, const PRM_RefId &ref, OP_InterestType interest)
Definition: OP_Dependency.h:91
reverse_iterator & operator++(int)
int getRefCount() const
Definition: OP_Dependency.h:59
const PRM_RefId & getRefId() const
int64 getMemoryUsage(bool inclusive) const
int matches(int ref_op_id, const PRM_RefId &ref, OP_InterestType mask=OP_INTEREST_ALL) const
friend class reverse_iterator
const GLdouble * v
Definition: glcorearb.h:837
void append(const OP_Dependency &v)
const_iterator end() const
reverse_iterator(const reverse_iterator &it)
reverse_iterator const_iterator
void setRefOpId(int op_id)
Definition: OP_Dependency.h:57
int64 exint
Definition: SYS_Types.h:125
int getRefOpId() const
int64 getMemoryUsage(bool inclusive=false) const
Definition: UT_Array.h:657
UT_Array< OP_Dependency >::reverse_iterator reverse_iterator
int hasInterest(OP_InterestType interest) const
const_iterator end() const
void clearRefOpId()
OP_Dependency * operator->() const
IMATH_HOSTDEVICE constexpr int cmp(T a, T b) IMATH_NOEXCEPT
Definition: ImathFun.h:84
void append(const OP_Dependency &v)
void setRefOpId(int op_id)
int removeMatching(int ref_op_id, OP_InterestType mask)
int removeRef(int count=1)
Definition: OP_Dependency.h:45
reverse_iterator rend()
OP_InterestType
Definition: OP_DataTypes.h:45
reverse_iterator rbegin()
Begin iterating over the array in reverse.
Definition: UT_Array.h:1031
OP_InterestType getInterestType() const
GLint ref
Definition: glcorearb.h:124
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:155
reverse_iterator rend() const
base_iterator< OP_DependencyNode *, true > iterator
Definition: UT_Array.h:998
OP_DependencyList & operator=(const OP_DependencyList &src)
GLint GLuint mask
Definition: glcorearb.h:124
iterator begin()
Definition: UT_Array.h:1006
long long int64
Definition: SYS_Types.h:116
OP_Dependency & operator*() const
void setCapacityIfNeeded(exint min_capacity)
Definition: UT_Array.h:603
int operator==(const OP_Reference &other)
Definition: OP_Dependency.h:65
void removeItem(const reverse_iterator &it)
unsigned hash() const
Definition: OP_Dependency.h:61
OP_InterestType getInterest() const
reverse_iterator rend()
End reverse iterator.
Definition: UT_Array.h:1037
bool fixRemovedSourceRefParmIndex(int removed_parm_idx)
int getRefOpId() const
Definition: OP_Dependency.h:56
exint append()
Definition: UT_Array.h:142
reverse_iterator & operator=(const reverse_iterator &cmp)
bool operator!=(const reverse_iterator &cmp) const
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:648
void addInterest(OP_InterestType type)
UT_Array< OP_Dependency >::iterator iterator
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
bool operator==(const reverse_iterator &cmp) const
reverse_iterator rbegin()
int64 getMemoryUsage(bool inclusive) const
reverse_iterator rbegin() const
OP_Reference(int ref_op_id)
Definition: OP_Dependency.h:34
LeafData & operator=(const LeafData &)=delete
#define OP_API
Definition: OP_API.h:10
OP_Dependency * find(int ref_op_id, const PRM_RefId &source_ref, const PRM_RefId &ref_id) const
Find a dependency for the given criteria.
void clearRefOpId()
Definition: OP_Dependency.h:55
void removeItem(const reverse_iterator &it)
Remove item specified by the reverse_iterator.
Definition: UT_Array.h:1064
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
OP_DependencyNode(const OP_DependencyNode &src)
OP_Dependency & getDependency() const
const PRM_RefId & getSourceRefId() const
const_iterator begin() const
UT_Array< OP_Dependency >::const_iterator const_iterator
type
Definition: core.h:1059
void removeItem(const reverse_iterator &it)
bool isEmpty() const
unsigned int uint
Definition: SYS_Types.h:45
UT_Array< OP_Reference > OP_ReferenceList
Definition: OP_Dependency.h:74
const_iterator begin() const
GLint GLsizei count
Definition: glcorearb.h:405
iterator end()
End iterator.
Definition: UT_Array.h:1011
int matches(int ref_op_id, const PRM_RefId &source_ref, const PRM_RefId &ref, OP_InterestType mask=OP_INTEREST_ALL) const
GLenum src
Definition: glcorearb.h:1793
unsigned hash() const