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 &operator=(const OP_Dependency &other) = default;
101 
103  {
104  myInterestType = (OP_InterestType) ((int) myInterestType | type);
105  }
107  {
108  return myInterestType;
109  }
110 
111  int matches( int ref_op_id, const PRM_RefId &ref,
113  {
114  return (myRefOpId == ref_op_id && myRef.matches(ref)
115  && hasInterest(mask));
116  }
117  int matches( int ref_op_id, const PRM_RefId &source_ref,
118  const PRM_RefId &ref,
120  {
121  return myRefOpId == ref_op_id && mySourceRef == source_ref &&
122  myRef == ref && hasInterest(mask);
123  }
124  unsigned hash() const
125  {
126  uint h = SYSwang_inthash(myRefOpId);
127  h = h * 37 + SYSwang_inthash(myInterestType);
128  h = h * 37 + mySourceRef.hash();
129  h = h * 37 + myRef.hash();
130  return h;
131  }
132 
133  int operator==( const OP_Dependency &other ) const
134  {
135  return matches( other.myRefOpId, other.mySourceRef, other.myRef );
136  }
137 
138  // Returns true if the source ref matches exactly the removed parm index
139  bool fixRemovedSourceRefParmIndex(int removed_parm_idx)
140  {
141  if (!mySourceRef.isValid())
142  return false;
143 
144  if (mySourceRef.getParmRef() == removed_parm_idx)
145  {
146  UT_ASSERT_P(false);
147  mySourceRef.setParmRef(-1, -1);
148  return true;
149  }
150  else if (mySourceRef.getParmRef() > removed_parm_idx)
151  {
152  mySourceRef.setParmRef(mySourceRef.getParmRef() - 1,
153  mySourceRef.getParmSubIndex());
154  }
155  return false;
156  }
157 
158  int getRefOpId() const { return myRefOpId; }
159  void clearRefOpId() { myRefOpId = -1; }
160  void setRefOpId( int op_id ) { myRefOpId = op_id; }
161  const PRM_RefId &getRefId() const { return myRef; }
162  const PRM_RefId &getSourceRefId() const { return mySourceRef; }
163  OP_InterestType getInterestType() const { return myInterestType; }
164  int hasInterest(OP_InterestType interest) const
165  { return (myInterestType & interest) != 0; }
166 
167 private:
168  int myRefOpId;
169  PRM_RefId mySourceRef;
170  PRM_RefId myRef;
171  OP_InterestType myInterestType;
172 };
173 
174 // Storage for dependency information for a single node's dependents
176 {
177 public:
179  {
180  }
182  : myList(src.myList)
183  {
184  }
186  {
187  }
188 
189  bool isEmpty() const { return myList.entries() == 0; }
190  void clear() { myList.entries(0); }
191  void append(const OP_Dependency &v) { myList.append(v); }
192  int64 getMemoryUsage(bool inclusive) const
193  {
194  int64 mem = inclusive ? sizeof(*this) : 0;
195  mem += myList.getMemoryUsage(false);
196  return mem;
197  }
198 
202 
203  iterator begin() { return myList.begin(); }
204  iterator end() { return myList.end(); }
205  const_iterator begin() const { return myList.begin(); }
206  const_iterator end() const { return myList.end(); }
207  reverse_iterator rbegin() { return myList.rbegin(); }
208  reverse_iterator rend() { return myList.rend(); }
209 
210  void removeItem(const reverse_iterator &it)
211  {
212  myList.removeItem(it);
213  }
214 private:
215  // Called by OP_DependencyList
216  int getRefOpId() const
217  {
218  UT_ASSERT(myList.entries());
219  return myList(0).getRefOpId();
220  }
221  friend class OP_DependencyList;
223 };
224 
226 {
227 public:
229  : myEntries(0)
230  {
231  }
234  {
235  clear();
236  }
238  bool isEmpty() const { return myEntries == 0; }
239  void clear();
240  void append(const OP_Dependency &v);
241  int64 getMemoryUsage(bool inclusive) const;
242 
243  /// Find a dependency for the given criteria
244  OP_Dependency *find(int ref_op_id, const PRM_RefId &source_ref,
245  const PRM_RefId &ref_id) const;
246  OP_Dependency *find(int ref_op_id, const PRM_RefId &ref_id) const;
247 
248  /// @{
249  /// Remove all matching dependencies. Returns number of items removed
250  int removeMatching(int ref_op_id, OP_InterestType mask);
251  int removeMatching(int ref_op_id, const PRM_RefId &ref_id,
253  /// @}
254  /// Access to the hash table
256  {
258  public:
260  : myOwner(NULL)
261  , myNodeCache()
262  , myNodeIterator()
263  , myListIterator()
264  {
265  }
267  : myOwner(it.myOwner)
268  , myNodeCache(it.myNodeCache)
269  , myNodeIterator(it.myNodeIterator)
270  , myListIterator(it.myListIterator)
271  {
272  }
274 
276  {
277  return *myListIterator;
278  }
280  {
281  return *myListIterator;
282  }
284  {
285  return &getDependency();
286  }
287 
289  {
290  myOwner = cmp.myOwner;
291  myNodeCache = cmp.myNodeCache;
292  myNodeIterator = cmp.myNodeIterator;
293  myListIterator = cmp.myListIterator;
294  return *this;
295  }
297  {
298  advance();
299  return *this;
300  }
302  {
303  advance();
304  return *this;
305  }
306  bool operator==(const reverse_iterator &cmp) const
307  {
308  return myOwner == cmp.myOwner &&
309  myNodeIterator == cmp.myNodeIterator &&
310  myListIterator == cmp.myListIterator &&
311  myNodeCache == cmp.myNodeCache;
312  }
313  bool operator!=(const reverse_iterator &cmp) const
314  {
315  return !(*this == cmp);
316  }
317  void advance()
318  {
319  myListIterator.advance();
320  while (myListIterator.atEnd())
321  {
322  ++myNodeIterator;
323  if (myNodeIterator.atEnd())
324  break;
325  myListIterator = getDependencyNode()->rbegin();
326  }
327  }
328 
329  void rewind()
330  {
331  const auto &dep_map = myOwner->myDependencyMap;
332  myNodeCache.entries(0);
333  myNodeCache.setCapacityIfNeeded(dep_map.size());
334  for (const auto &node : dep_map.mapped_range())
335  {
336  if (!node->isEmpty())
337  myNodeCache.append(node.get());
338  }
339  myNodeIterator = myNodeCache.begin();
340  if (!myNodeIterator.atEnd())
341  {
342  myListIterator = getDependencyNode()->rbegin();
343  }
344  else
345  {
346  myListIterator = OP_DependencyNode::reverse_iterator();
347  }
348  }
349 
350  bool atEnd() const
351  {
352  return myNodeIterator.atEnd() && myListIterator.atEnd();
353  }
354  private:
355  reverse_iterator(const OP_DependencyList *owner)
356  : myOwner(owner)
357  , myNodeIterator()
358  , myListIterator()
359  {
360  rewind();
361  }
362 
363  // Use in private methods to OP_DependencyList
364  OP_DependencyNode *getDependencyNode() const
365  {
366  return myNodeIterator.item();
367  }
368  const OP_DependencyNode::reverse_iterator &getListIterator() const
369  {
370  return myListIterator;
371  }
372 
373 
374  const OP_DependencyList *myOwner;
375  NodeCache myNodeCache;
376  NodeCache::iterator myNodeIterator;
378  friend class OP_DependencyList;
379  };
381 
382  const_iterator begin() const { return const_iterator(this); }
383  const_iterator end() const { return const_iterator(); }
384  reverse_iterator rbegin() const { return reverse_iterator(this); }
385  reverse_iterator rend() const { return reverse_iterator(); }
386 
387  void removeItem(const reverse_iterator &it);
388 
389 private:
390  friend class reverse_iterator;
391  OP_DependencyNode *findNode(int op_id) const;
392 
394 
395  DependencyMap myDependencyMap;
396  exint myEntries;
397 };
398 
399 #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)
GLenum src
Definition: glew.h:2410
GLenum GLint ref
Definition: glew.h:1845
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
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:469
UT_Array< OP_Dependency >::reverse_iterator reverse_iterator
int hasInterest(OP_InterestType interest) const
const GLdouble * v
Definition: glew.h:1391
GLenum GLint GLuint mask
Definition: glew.h:1845
const_iterator end() const
void clearRefOpId()
OP_Dependency * operator->() const
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:44
reverse_iterator rbegin()
Begin iterating over the array in reverse.
Definition: UT_Array.h:821
OP_InterestType getInterestType() const
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:134
reverse_iterator rend() const
base_iterator< OP_DependencyNode *, true > iterator
Definition: UT_Array.h:788
OP_DependencyList & operator=(const OP_DependencyList &src)
iterator begin()
Definition: UT_Array.h:796
long long int64
Definition: SYS_Types.h:116
OP_Dependency & operator*() const
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:8011
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
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1253
reverse_iterator rend()
End reverse iterator.
Definition: UT_Array.h:827
bool fixRemovedSourceRefParmIndex(int removed_parm_idx)
int getRefOpId() const
Definition: OP_Dependency.h:56
exint append()
Definition: UT_Array.h:95
reverse_iterator & operator=(const reverse_iterator &cmp)
bool operator!=(const reverse_iterator &cmp) const
int cmp(T a, T b)
Definition: ImathFun.h:119
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:460
void addInterest(OP_InterestType type)
UT_Array< OP_Dependency >::iterator iterator
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
#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.
GLuint GLuint GLsizei count
Definition: glew.h:1253
void clearRefOpId()
Definition: OP_Dependency.h:55
void setCapacityIfNeeded(exint mincapacity)
Definition: UT_Array.h:415
void removeItem(const reverse_iterator &it)
Remove item specified by the reverse_iterator.
Definition: UT_Array.h:854
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:135
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
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
iterator end()
End iterator.
Definition: UT_Array.h:801
type
Definition: core.h:528
int matches(int ref_op_id, const PRM_RefId &source_ref, const PRM_RefId &ref, OP_InterestType mask=OP_INTEREST_ALL) const
unsigned hash() const