HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UN_Iterator.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: UN_Iterator.h ( UN Library, C++)
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __UN_Iterator_h__
13 #define __UN_Iterator_h__
14 
15 #include "UN_API.h"
16 #include "UN_Types.h"
17 #include <UT/UT_Array.h> // for UN_WireArrayPortIterator
18 #include <SYS/SYS_Types.h>
19 #include <iterator>
20 
21 class UN_GraphData;
22 class UN_Node;
23 class UN_Port;
24 class UN_Wire;
25 
26 
27 // ============================================================================
28 /// Iterator for traversing the given data index array.
29 /// The iterator dereferences as a handle to the data object.
30 /// Used for iterating children of a node, parameters of a node, etc.
31 
32 template <typename HANDLE, typename INDEX_ARRAY, typename ID_ARRAY>
34 {
35 public:
36  using iterator_category = std::input_iterator_tag;
37  using value_type = HANDLE;
38  using difference_type = std::ptrdiff_t;
39  using pointer = value_type*;
41 
42  /// Constructor for the start iterator of the given data array.
43  UN_DataArrayIterator( UN_GraphData *graph_data,
44  INDEX_ARRAY &&data_indices,
45  ID_ARRAY &&data_ids,
46  exint begin = 0 )
47  : myGraphData( graph_data )
48  , myDataIndices( data_indices )
49  , myDataIDs( data_ids )
50  , myCurrent( begin )
51  {}
52 
53  /// Constructor for the end iterator.
54  UN_DataArrayIterator( UN_GraphData *graph_data, exint end )
55  : myGraphData( graph_data )
56  , myCurrent( end )
57  {}
58 
59  /// Increment operator.
61  {
62  ++myCurrent;
63  return *this;
64  }
65 
66  /// Comparison operator.
67  bool operator !=( const UN_DataArrayIterator &other ) const
68  {
69  return myCurrent != other.myCurrent
70  || myGraphData != other.myGraphData;
71  }
72 
73  /// Dereferencing operator, returns the handle to the current data object.
74  HANDLE operator*() const
75  {
76  return handle();
77  }
78 
79  /// Returns the handle to the current data object.
80  HANDLE handle() const
81  {
82  return myDataIndices.isValidIndex( myCurrent )
83  ? HANDLE( myGraphData,
84  myDataIndices[ myCurrent ], myDataIDs[ myCurrent ])
85  : HANDLE();
86  }
87 
88 
89 private:
90  /// The graph that owns the data objects.
91  UN_GraphData * myGraphData;
92 
93  /// The IDs of the data objects that are being iterated over.
94  INDEX_ARRAY myDataIndices;
95 
96  /// The ID numbers of the data ojects that are bing iterated over.
97  ID_ARRAY myDataIDs;
98 
99  /// The current data object when iterating.
100  exint myCurrent;
101 };
102 
103 
104 // ============================================================================
105 /// Iterator for traversing the input or output ports of the wire objects
106 /// in the given wire index array.
107 /// The iterator dereferences as a handle to the port data object.
108 /// Used for iterating connected ports.
109 template <bool IS_SOURCE_ITERATOR>
111  public UN_DataArrayIterator<UN_Wire, UN_WireIndexArray, UN_WireIDArray>
112 {
113 public:
114  using iterator_category = std::input_iterator_tag;
116  using difference_type = std::ptrdiff_t;
117  using pointer = value_type*;
119 
120  /// Constructor for the start iterator of the given data array.
121  UN_WireArrayPortIterator( UN_GraphData *graph_data,
122  UN_WireIndexArray &&data_indices,
123  UN_WireIDArray &&data_ids,
124  exint begin = 0 )
126  graph_data, std::move(data_indices), std::move(data_ids), begin)
127  {}
128 
129  /// Constructor for the end iterator.
130  UN_WireArrayPortIterator( UN_GraphData *graph_data, exint end )
132  graph_data, end )
133  {}
134 
135  /// Dereferencing operator, returns the handle to the connected port
136  /// of the data object.
137  UN_Port operator*() const;
138 };
139 
140 // These are implemented in .C file.
143 
144 
145 // ============================================================================
146 /// Iterator for traversing the node data objects that are arranged in
147 /// a parental tree hierarchy.
148 /// The iterator dereferences as a handle to node data object.
149 /// Used for iterating connected nodes in nested subnetworks.
151 {
152 public:
153  using iterator_category = std::input_iterator_tag;
155  using difference_type = std::ptrdiff_t;
156  using pointer = value_type*;
158 
159  /// Constructor for the start iterator of the given node ancestral tree.
160  UN_NodeDescendantIterator( UN_GraphData *graph_data,
161  UN_NodeIndex node_index,
162  UN_NodeID node_id )
163  : myGraphData( graph_data )
164  {
165  pushChildData( node_index, node_id );
166  }
167 
168  /// Constructor for the end iterator.
169  UN_NodeDescendantIterator( UN_GraphData *graph_data )
170  : myGraphData( graph_data )
171  {}
172 
173  /// Increment operator.
175  {
176  UN_NodeIndex node_index;
177  UN_NodeID node_id;
178 
179  popData( node_index, node_id );
180  pushChildData( node_index, node_id );
181  return *this;
182  }
183 
184  /// Comparison operator.
185  bool operator !=( const UN_NodeDescendantIterator &other ) const
186  {
187  // Loops compare iterators only against the 'end' iterator,
188  // which has an empty array, so comparing sizes is enough.
189  return myDataIndices.size() != other.myDataIndices.size()
190  || myGraphData != other.myGraphData;
191  }
192 
193  /// Dereferencing operator, returns the handle to the current node data obj.
194  UN_Node operator*() const;
195 
196 private:
197  /// Adds the children of the given node to the stack for traversal.
198  void pushChildData( UN_NodeIndex parent_index, UN_NodeID parent_id );
199 
200  /// Removes and returns the index the top child on the traversal stack.
201  void popData( UN_NodeIndex &node_index, UN_NodeID &node_id );
202 
203 private:
204  /// The graph that owns the node objects.
205  UN_GraphData * myGraphData;
206 
207  /// The stack of pending node indices to traverse.
208  UN_NodeIndexArray myDataIndices;
209 
210  /// The stack of pending node IDs to traverse.
211  UN_NodeIDArray myDataIDs;
212 };
213 
214 
215 // ============================================================================
216 /// Iterator for traversing the wires inside the given node
217 /// The iterator dereferences as a handle to the wire data object.
218 /// Used for iterating wires inside a node.
219 template <bool CHILD_ONLY>
221 {
222 public:
223  using iterator_category = std::input_iterator_tag;
225  using difference_type = std::ptrdiff_t;
226  using pointer = value_type*;
228 
229  /// Constructor for the start and end iterator for node's wires.
230  UN_NodeWireIterator( UN_GraphData *graph_data,
231  UN_NodeIndex node_index,
232  UN_NodeID node_id,
234  UN_WireIndex end )
235  : myGraphData( graph_data )
236  , myNodeIndex( node_index )
237  , myNodeID( node_id )
238  , myCurrent( index )
239  , myEnd( end )
240  {
241  if( !isCurrentValid() )
242  advance();
243  }
244 
245  /// Increment operator.
247  {
248  advance();
249  return *this;
250  }
251 
252  /// Comparison operator.
253  bool operator !=( const UN_NodeWireIterator &other ) const
254  {
255  return myCurrent != other.myCurrent
256  || myGraphData != other.myGraphData;
257  }
258 
259  /// Dereferencing operator, returns the handle to the wire inside the node.
260  UN_Wire operator*() const;
261 
262 private:
263  /// Returns true if the current node satisfies this iterator type.
264  bool isCurrentValid() const;
265 
266  /// Moves the iterator forward to the next valid wire, if possible.
267  void advance();
268 
269 private:
270  /// The graph that owns the node objects.
271  UN_GraphData * myGraphData;
272 
273  /// The index of the node inside which the iterated wires exist.
274  UN_NodeIndex myNodeIndex;
275 
276  /// The ID of the node inside which the iterated wires exist.
277  UN_NodeID myNodeID;
278 
279  /// The current wire when iterating.
280  UN_WireIndex myCurrent;
281 
282  /// The end of range to iterate.
283  UN_WireIndex myEnd;
284 };
285 
286 
287 
288 #endif
289 
UN_WireArrayPortIterator(UN_GraphData *graph_data, UN_WireIndexArray &&data_indices, UN_WireIDArray &&data_ids, exint begin=0)
Constructor for the start iterator of the given data array.
Definition: UN_Iterator.h:121
UN_NodeWireIterator(UN_GraphData *graph_data, UN_NodeIndex node_index, UN_NodeID node_id, UN_WireIndex index, UN_WireIndex end)
Constructor for the start and end iterator for node's wires.
Definition: UN_Iterator.h:230
UN_Port operator*() const
UN_NodeDescendantIterator & operator++()
Increment operator.
Definition: UN_Iterator.h:174
int64 exint
Definition: SYS_Types.h:125
HANDLE handle() const
Returns the handle to the current data object.
Definition: UN_Iterator.h:80
UN_DataArrayIterator(UN_GraphData *graph_data, INDEX_ARRAY &&data_indices, ID_ARRAY &&data_ids, exint begin=0)
Constructor for the start iterator of the given data array.
Definition: UN_Iterator.h:43
std::ptrdiff_t difference_type
Definition: UN_Iterator.h:116
#define UN_API
Definition: UN_API.h:11
exint size() const
Definition: UT_Array.h:646
std::input_iterator_tag iterator_category
Definition: UN_Iterator.h:114
std::ptrdiff_t difference_type
Definition: UN_Iterator.h:155
GLuint GLuint end
Definition: glcorearb.h:475
IMATH_HOSTDEVICE constexpr Color4< T > operator*(S a, const Color4< T > &v) IMATH_NOEXCEPT
Reverse multiplication: S * Color4.
Definition: ImathColor.h:732
UN_NodeWireIterator & operator++()
Increment operator.
Definition: UN_Iterator.h:246
UN_NodeDescendantIterator(UN_GraphData *graph_data)
Constructor for the end iterator.
Definition: UN_Iterator.h:169
UN_WireArrayPortIterator(UN_GraphData *graph_data, exint end)
Constructor for the end iterator.
Definition: UN_Iterator.h:130
std::input_iterator_tag iterator_category
Definition: UN_Iterator.h:153
std::ptrdiff_t difference_type
Definition: UN_Iterator.h:225
std::input_iterator_tag iterator_category
Definition: UN_Iterator.h:223
HANDLE operator*() const
Dereferencing operator, returns the handle to the current data object.
Definition: UN_Iterator.h:74
GLuint index
Definition: glcorearb.h:786
UN_NodeDescendantIterator(UN_GraphData *graph_data, UN_NodeIndex node_index, UN_NodeID node_id)
Constructor for the start iterator of the given node ancestral tree.
Definition: UN_Iterator.h:160
UN_DataArrayIterator(UN_GraphData *graph_data, exint end)
Constructor for the end iterator.
Definition: UN_Iterator.h:54
UN_DataArrayIterator & operator++()
Increment operator.
Definition: UN_Iterator.h:60
bool operator!=(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:165
An node ID.
Definition: UN_Types.h:273
bool operator!=(const UN_DataArrayIterator &other) const
Comparison operator.
Definition: UN_Iterator.h:67
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:483