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_Assert.h>
22 #include <UT/UT_Array.h>
23 #include <UT/UT_Map.h>
24 #include <UT/UT_ValArray.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 {
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 DependencyMap &dep_map = myOwner->myDependencyMap;
333  myNodeCache.entries(0);
334  myNodeCache.setCapacityIfNeeded(dep_map.size());
335  for (OP_DependencyNode *node : dep_map.mapped_range())
336  {
337  if (!node->isEmpty())
338  myNodeCache.append(node);
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 
394  UT_Map<int, OP_DependencyNode *> myDependencyMap;
395  exint myEntries;
396 };
397 
398 #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
void append(const OP_Dependency &v)
const GLdouble * v
Definition: glcorearb.h:836
const_iterator end() const
reverse_iterator(const reverse_iterator &it)
reverse_iterator const_iterator
void setRefOpId(int op_id)
Definition: OP_Dependency.h:57
int getRefOpId() const
int64 getMemoryUsage(bool inclusive=false) const
Definition: UT_Array.h:462
UT_Array< OP_Dependency >::reverse_iterator reverse_iterator
GLint GLuint mask
Definition: glcorearb.h:123
int hasInterest(OP_InterestType interest) const
const_iterator end() const
void clearRefOpId()
OP_Dependency * operator->() const
void append(const OP_Dependency &v)
void setRefOpId(int op_id)
long long int64
Definition: SYS_Types.h:107
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:804
OP_InterestType getInterestType() const
GLint ref
Definition: glcorearb.h:123
int64 exint
Definition: SYS_Types.h:116
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:125
reverse_iterator rend() const
base_iterator< OP_DependencyNode *, true > iterator
Definition: UT_Array.h:771
OP_DependencyList & operator=(const OP_DependencyList &src)
UT_IteratorRange< mapped_iterator > mapped_range()
Definition: UT_Map.h:231
iterator begin()
Definition: UT_Array.h:779
OP_Dependency & operator*() const
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:810
unsigned int uint
Definition: SYS_Types.h:40
bool fixRemovedSourceRefParmIndex(int removed_parm_idx)
int getRefOpId() const
Definition: OP_Dependency.h:56
GLint GLsizei count
Definition: glcorearb.h:404
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:453
void addInterest(OP_InterestType type)
UT_Array< OP_Dependency >::iterator iterator
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2001
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.
exint append(void)
Definition: UT_Array.h:95
void clearRefOpId()
Definition: OP_Dependency.h:55
void setCapacityIfNeeded(exint mincapacity)
Definition: UT_Array.h:408
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
void removeItem(const reverse_iterator &it)
Remove item specified by the reverse_iterator.
Definition: UT_Array.h:837
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:126
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
UT_Array< OP_Reference > OP_ReferenceList
Definition: OP_Dependency.h:74
const_iterator begin() const
iterator end()
End iterator.
Definition: UT_Array.h:784
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:1792
unsigned hash() const