HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
iterator.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_USD_PCP_ITERATOR_H
8 #define PXR_USD_PCP_ITERATOR_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/usd/pcp/api.h"
12 #include "pxr/usd/pcp/node.h"
13 
14 #include "pxr/usd/sdf/primSpec.h"
16 #include "pxr/usd/sdf/site.h"
17 
18 #include "pxr/base/tf/iterator.h"
19 
20 #include <iterator>
21 
23 
24 class PcpPrimIndex;
25 class PcpPrimIndex_Graph;
26 class PcpPropertyIndex;
27 
28 /// \class PcpNodeIterator
29 ///
30 /// Object used to iterate over nodes in the prim index graph in strong-to-weak
31 /// order.
32 ///
34 {
35  class _PtrProxy {
36  public:
37  PcpNodeRef* operator->() { return &_nodeRef; }
38  private:
39  friend class PcpNodeIterator;
40  explicit _PtrProxy(const PcpNodeRef& nodeRef) : _nodeRef(nodeRef) {}
41  PcpNodeRef _nodeRef;
42  };
43 public:
44  using iterator_category = std::random_access_iterator_tag;
47  using pointer = _PtrProxy;
48  using difference_type = std::ptrdiff_t;
49 
50  /// Constructs an invalid iterator.
51  PcpNodeIterator() = default;
52 
53  // Returns a compressed Sd site. For internal use only.
54  Pcp_CompressedSdSite GetCompressedSdSite(size_t layerIndex) const
55  {
56  return Pcp_CompressedSdSite(_nodeIdx, layerIndex);
57  }
58 
59  // If the current node has a direct sibling, move this iterator to
60  // that node. Otherwise, move this iterator to the next sibling of
61  // the nearest ancestor node with siblings. If no such node exists,
62  // (i.e., the current node is the weakest node in the index), this
63  // iterator will become the end of the range of all nodes in the
64  // prim index.
66 
67  reference operator*() const { return dereference(); }
68  pointer operator->() const { return pointer(dereference()); }
70  PcpNodeIterator advanced(*this);
71  advanced.advance(index);
72  return advanced.dereference();
73  }
74 
76  return -distance_to(other);
77  }
78 
80  increment();
81  return *this;
82  }
83 
85  decrement();
86  return *this;
87  }
88 
90  PcpNodeIterator result(*this);
91  increment();
92  return result;
93  }
94 
96  PcpNodeIterator result(*this);
97  decrement();
98  return result;
99  }
100 
101  PcpNodeIterator operator+(const difference_type increment) const {
102  PcpNodeIterator result(*this);
103  result.advance(increment);
104  return result;
105  }
106 
107  PcpNodeIterator operator-(const difference_type decrement) const {
108  PcpNodeIterator result(*this);
109  result.advance(-decrement);
110  return result;
111  }
112 
114  advance(increment);
115  return *this;
116  }
117 
119  advance(-decrement);
120  return *this;
121  }
122 
123  bool operator==(const PcpNodeIterator& other) const {
124  return equal(other);
125  }
126 
127  bool operator!=(const PcpNodeIterator& other) const {
128  return !equal(other);
129  }
130 
131  bool operator<(const PcpNodeIterator& other) const {
132  TF_DEV_AXIOM(_graph == other._graph);
133  return _nodeIdx < other._nodeIdx;
134  }
135 
136  bool operator<=(const PcpNodeIterator& other) const {
137  TF_DEV_AXIOM(_graph == other._graph);
138  return _nodeIdx <= other._nodeIdx;
139  }
140 
141  bool operator>(const PcpNodeIterator& other) const {
142  TF_DEV_AXIOM(_graph == other._graph);
143  return _nodeIdx > other._nodeIdx;
144  }
145 
146  bool operator>=(const PcpNodeIterator& other) const {
147  TF_DEV_AXIOM(_graph == other._graph);
148  return _nodeIdx >= other._nodeIdx;
149  }
150 
151 private:
152  friend class PcpPrimIndex;
153  PcpNodeIterator(PcpPrimIndex_Graph* graph, size_t nodeIdx) :
154  _graph(graph), _nodeIdx(nodeIdx) {}
155 
156  void increment() { ++_nodeIdx; }
157  void decrement() { --_nodeIdx; }
158  void advance(difference_type n) { _nodeIdx += n; }
159  difference_type distance_to(const PcpNodeIterator& other) const {
160  return (difference_type)(other._nodeIdx) - _nodeIdx;
161  }
162  bool equal(const PcpNodeIterator& other) const {
163  return (_graph == other._graph) & (_nodeIdx == other._nodeIdx);
164  }
165  reference dereference() const {
166  return PcpNodeRef(_graph, _nodeIdx);
167  }
168 
169 private:
170  PcpPrimIndex_Graph* _graph = nullptr;
171  size_t _nodeIdx = PCP_INVALID_INDEX;
172 };
173 
174 /// \class PcpNodeReverseIterator
175 ///
176 /// Object used to iterate over nodes in the prim index graph in weak-to-strong
177 /// order.
178 ///
180  : public Tf_ProxyReferenceReverseIterator<PcpNodeIterator>
181 {
182 public:
186 };
187 
188 /// \class PcpPrimIterator
189 ///
190 /// Object used to iterate over prim specs in the prim index graph in
191 /// strong-to-weak order.
192 ///
194 {
195  class _PtrProxy {
196  public:
197  SdfSite* operator->() { return &_site; }
198  private:
199  friend class PcpPrimIterator;
200  explicit _PtrProxy(const SdfSite& site) : _site(site) {}
201  SdfSite _site;
202  };
203 public:
204  using iterator_category = std::random_access_iterator_tag;
207  using pointer = _PtrProxy;
208  using difference_type = std::ptrdiff_t;
209 
210  /// Constructs an invalid iterator.
211  PCP_API
212  PcpPrimIterator();
213 
214  /// Constructs a prim iterator beginning at position \p pos in the
215  /// prim stack of prim index \p primIndex.
216  PCP_API
217  PcpPrimIterator(const PcpPrimIndex* primIndex, size_t pos);
218 
219  /// Returns the PcpNode from which the current prim originated.
220  PCP_API
221  PcpNodeRef GetNode() const;
222 
223  // Returns the \c Pcp_SdSiteRef from which the current prim originated.
224  // For internal use only.
225  PCP_API
226  Pcp_SdSiteRef _GetSiteRef() const;
227 
228  reference operator*() const { return dereference(); }
229  pointer operator->() const { return pointer(dereference()); }
231  PcpPrimIterator advanced(*this);
232  advanced.advance(index);
233  return advanced.dereference();
234  }
235 
237  return -distance_to(other);
238  }
239 
241  increment();
242  return *this;
243  }
244 
246  decrement();
247  return *this;
248  }
249 
251  PcpPrimIterator result(*this);
252  increment();
253  return result;
254  }
255 
257  PcpPrimIterator result(*this);
258  decrement();
259  return result;
260  }
261 
262  PcpPrimIterator operator+(const difference_type increment) const {
263  PcpPrimIterator result(*this);
264  result.advance(increment);
265  return result;
266  }
267 
268  PcpPrimIterator operator-(const difference_type decrement) const {
269  PcpPrimIterator result(*this);
270  result.advance(-decrement);
271  return result;
272  }
273 
275  advance(increment);
276  return *this;
277  }
278 
280  advance(-decrement);
281  return *this;
282  }
283 
284  bool operator==(const PcpPrimIterator& other) const {
285  return equal(other);
286  }
287 
288  bool operator!=(const PcpPrimIterator& other) const {
289  return !equal(other);
290  }
291 
292  bool operator<(const PcpPrimIterator& other) const {
293  TF_DEV_AXIOM(_primIndex == other._primIndex);
294  return _pos < other._pos;
295  }
296 
297  bool operator<=(const PcpPrimIterator& other) const {
298  TF_DEV_AXIOM(_primIndex == other._primIndex);
299  return _pos <= other._pos;
300  }
301 
302  bool operator>(const PcpPrimIterator& other) const {
303  TF_DEV_AXIOM(_primIndex == other._primIndex);
304  return _pos > other._pos;
305  }
306 
307  bool operator>=(const PcpPrimIterator& other) const {
308  TF_DEV_AXIOM(_primIndex == other._primIndex);
309  return _pos >= other._pos;
310  }
311 
312 private:
313  PCP_API
314  void increment();
315  PCP_API
316  void decrement();
317  PCP_API
318  void advance(difference_type n);
319  PCP_API
320  difference_type distance_to(const PcpPrimIterator& other) const;
321  PCP_API
322  bool equal(const PcpPrimIterator& other) const;
323  PCP_API
324  reference dereference() const;
325 
326 private:
327  const PcpPrimIndex* _primIndex = nullptr;
328  size_t _pos = PCP_INVALID_INDEX;
329 };
330 
331 /// \class PcpPrimReverseIterator
332 ///
333 /// Object used to iterate over prim specs in the prim index graph in
334 /// weak-to-strong order.
335 ///
337  : public Tf_ProxyReferenceReverseIterator<PcpPrimIterator>
338 {
339 public:
343 
345  {
346  PcpPrimIterator tmp = base();
347  return (--tmp).GetNode();
348  }
349 
351  {
352  PcpPrimIterator tmp = base();
353  return (--tmp)._GetSiteRef();
354  }
355 };
356 
357 /// \class PcpPropertyIterator
358 ///
359 /// Object used to iterate over property specs in a property index in
360 /// strong-to-weak order.
361 ///
363 {
364 public:
365  using iterator_category = std::random_access_iterator_tag;
366  using value_type = const SdfPropertySpecHandle;
367  using reference = const SdfPropertySpecHandle&;
368  using pointer = const SdfPropertySpecHandle*;
369  using difference_type = std::ptrdiff_t;
370 
371  /// Constructs an invalid iterator.
372  PCP_API
374 
375  /// Constructs a property iterator for \p index beginning at position
376  /// \p pos in the property stack.
377  PCP_API
378  PcpPropertyIterator(const PcpPropertyIndex& index, size_t pos = 0);
379 
380  /// Returns the PcpNode from which the current property originated.
381  PCP_API
382  PcpNodeRef GetNode() const;
383 
384  /// Returns true if the current property is local to the owning
385  /// property index's layer stack, false otherwise.
386  PCP_API
387  bool IsLocal() const;
388 
389  reference operator*() const { return dereference(); }
390  pointer operator->() const { return &(dereference()); }
392  PcpPropertyIterator advanced(*this);
393  advanced.advance(index);
394  return advanced.dereference();
395  }
396 
398  return -distance_to(other);
399  }
400 
402  increment();
403  return *this;
404  }
405 
407  decrement();
408  return *this;
409  }
410 
413  increment();
414  return result;
415  }
416 
419  decrement();
420  return result;
421  }
422 
425  result.advance(increment);
426  return result;
427  }
428 
431  result.advance(-decrement);
432  return result;
433  }
434 
436  advance(increment);
437  return *this;
438  }
439 
441  advance(-decrement);
442  return *this;
443  }
444 
445  bool operator==(const PcpPropertyIterator& other) const {
446  return equal(other);
447  }
448 
449  bool operator!=(const PcpPropertyIterator& other) const {
450  return !equal(other);
451  }
452 
453  bool operator<(const PcpPropertyIterator& other) const {
454  TF_DEV_AXIOM(_propertyIndex == other._propertyIndex);
455  return _pos < other._pos;
456  }
457 
458  bool operator<=(const PcpPropertyIterator& other) const {
459  TF_DEV_AXIOM(_propertyIndex == other._propertyIndex);
460  return _pos <= other._pos;
461  }
462 
463  bool operator>(const PcpPropertyIterator& other) const {
464  TF_DEV_AXIOM(_propertyIndex == other._propertyIndex);
465  return _pos > other._pos;
466  }
467 
468  bool operator>=(const PcpPropertyIterator& other) const {
469  TF_DEV_AXIOM(_propertyIndex == other._propertyIndex);
470  return _pos >= other._pos;
471  }
472 
473 private:
474  PCP_API
475  void increment();
476  PCP_API
477  void decrement();
478  PCP_API
479  void advance(difference_type n);
480  PCP_API
481  difference_type distance_to(const PcpPropertyIterator& other) const;
482  PCP_API
483  bool equal(const PcpPropertyIterator& other) const;
484  PCP_API
485  reference dereference() const;
486 
487 private:
488  const PcpPropertyIndex* _propertyIndex = nullptr;
489  size_t _pos = 0;
490 };
491 
492 /// \class PcpPropertyReverseIterator
493 ///
494 /// Object used to iterate over property specs in a property index in
495 /// weak-to-strong order.
496 ///
498  : public std::reverse_iterator<PcpPropertyIterator>
499 {
500 public:
503  : std::reverse_iterator<PcpPropertyIterator>(iter) { }
504 
506  {
507  PcpPropertyIterator tmp = base();
508  return (--tmp).GetNode();
509  }
510 
511  bool IsLocal() const
512  {
513  PcpPropertyIterator tmp = base();
514  return (--tmp).IsLocal();
515  }
516 };
517 
518 // Helper macro for defining iterator ranges, which are simply pairs of
519 // iterators denoting the [start, end) of a series of values. These ranges
520 // may be used with TF_FOR_ALL and TF_REVERSE_FOR_ALL.
521 #define PCP_DEFINE_RANGE(Range, Iterator, ReverseIterator) \
522  typedef std::pair<Iterator, Iterator> Range; \
523  \
524  inline Iterator begin(Range &range) { return range.first; } \
525  inline Iterator begin(const Range &range) { return range.first; } \
526  inline Iterator end(Range &range) { return range.second; } \
527  inline Iterator end(const Range &range) { return range.second; } \
528  \
529  template <> \
530  struct Tf_IteratorInterface<Range, false> { \
531  typedef Iterator IteratorType; \
532  static IteratorType Begin(Range &c) { return c.first; } \
533  static IteratorType End(Range &c) { return c.second; } \
534  }; \
535  \
536  template <> \
537  struct Tf_IteratorInterface<const Range, false> { \
538  typedef Iterator IteratorType; \
539  static IteratorType Begin(Range const &c) { return c.first; } \
540  static IteratorType End(Range const &c) { return c.second; } \
541  }; \
542  \
543  template <> \
544  struct Tf_IteratorInterface<Range, true> { \
545  typedef ReverseIterator IteratorType; \
546  static IteratorType Begin(Range &c) \
547  { return IteratorType(c.second); } \
548  static IteratorType End(Range &c) \
549  { return IteratorType(c.first); } \
550  }; \
551  \
552  template <> \
553  struct Tf_IteratorInterface<const Range, true> { \
554  typedef ReverseIterator IteratorType; \
555  static IteratorType Begin(Range const &c) \
556  { return IteratorType(c.second); } \
557  static IteratorType End(Range const &c) \
558  { return IteratorType(c.first); } \
559  }; \
560  \
561  template <> \
562  struct Tf_ShouldIterateOverCopy<Range> : std::true_type {}; \
563  \
564  template <> \
565  struct Tf_ShouldIterateOverCopy<const Range> : std::true_type {}
566 
569 PCP_DEFINE_RANGE(PcpPropertyRange, PcpPropertyIterator,
571 
572 /// \class PcpIteratorTraits
573 ///
574 /// Traits class for retrieving useful characteristics about one of the
575 /// Pcp iterator types above.
576 ///
577 template <class Iterator> struct PcpIteratorTraits;
578 
579 template <>
581 {
582  typedef PcpNodeRange RangeType;
584 };
585 
586 template <>
588 {
589  typedef PcpPrimRange RangeType;
591 };
592 
593 template <>
595 {
596  typedef PcpPropertyRange RangeType;
598 };
599 
601 
602 #endif // PXR_USD_PCP_ITERATOR_H
bool operator<(const PcpPrimIterator &other) const
Definition: iterator.h:292
bool operator>=(const PcpNodeIterator &other) const
Definition: iterator.h:146
difference_type operator-(const PcpPrimIterator &other) const
Definition: iterator.h:236
PcpNodeIterator operator++(int)
Definition: iterator.h:89
PcpNodeIterator()=default
Constructs an invalid iterator.
PcpNodeIterator operator-(const difference_type decrement) const
Definition: iterator.h:107
PcpPrimIterator & operator-=(const difference_type decrement)
Definition: iterator.h:279
PcpPropertyReverseIterator(const PcpPropertyIterator &iter)
Definition: iterator.h:502
reference operator*() const
Definition: iterator.h:389
_PtrProxy pointer
Definition: iterator.h:47
PcpPropertyIterator & operator+=(const difference_type increment)
Definition: iterator.h:435
PCP_API PcpPropertyIterator()
Constructs an invalid iterator.
PcpNodeRef GetNode() const
Definition: iterator.h:505
bool operator!=(const PcpPropertyIterator &other) const
Definition: iterator.h:449
PcpPropertyIterator operator-(const difference_type decrement) const
Definition: iterator.h:429
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
PcpNodeIterator & operator++()
Definition: iterator.h:79
bool operator<(const PcpNodeIterator &other) const
Definition: iterator.h:131
PcpPrimIterator & operator++()
Definition: iterator.h:240
PcpPropertyIterator & operator--()
Definition: iterator.h:406
bool operator!=(const PcpNodeIterator &other) const
Definition: iterator.h:127
PcpNodeIterator & operator--()
Definition: iterator.h:84
PCP_API PcpNodeRef GetNode() const
Returns the PcpNode from which the current prim originated.
pointer operator->() const
Definition: iterator.h:229
bool operator==(const PcpPropertyIterator &other) const
Definition: iterator.h:445
IMATH_HOSTDEVICE constexpr bool equal(T1 a, T2 b, T3 t) IMATH_NOEXCEPT
Definition: ImathFun.h:105
PcpNodeIterator operator--(int)
Definition: iterator.h:95
bool operator<=(const PcpPropertyIterator &other) const
Definition: iterator.h:458
**But if you need a result
Definition: thread.h:622
bool operator!=(const PcpPrimIterator &other) const
Definition: iterator.h:288
bool operator<=(const PcpNodeIterator &other) const
Definition: iterator.h:136
difference_type operator-(const PcpNodeIterator &other) const
Definition: iterator.h:75
PcpPrimIterator & operator+=(const difference_type increment)
Definition: iterator.h:274
PcpNodeRef GetNode() const
Definition: iterator.h:344
PcpPrimIterator operator++(int)
Definition: iterator.h:250
const SdfPropertySpecHandle value_type
Definition: iterator.h:366
reference operator*() const
Definition: iterator.h:67
bool operator<=(const PcpPrimIterator &other) const
Definition: iterator.h:297
bool operator==(const PcpNodeIterator &other) const
Definition: iterator.h:123
std::random_access_iterator_tag iterator_category
Definition: iterator.h:204
Definition: site.h:25
GLdouble n
Definition: glcorearb.h:2008
PcpNodeIterator operator+(const difference_type increment) const
Definition: iterator.h:101
#define TF_DEV_AXIOM(cond)
const SdfPropertySpecHandle & reference
Definition: iterator.h:367
PcpPropertyReverseIterator ReverseIteratorType
Definition: iterator.h:597
std::ptrdiff_t difference_type
Definition: iterator.h:48
SdfSite reference
Definition: iterator.h:206
reference operator[](const difference_type index) const
Definition: iterator.h:230
PcpPrimIterator operator-(const difference_type decrement) const
Definition: iterator.h:268
PcpNodeRef reference
Definition: iterator.h:46
bool operator==(const PcpPrimIterator &other) const
Definition: iterator.h:284
PcpPropertyIterator & operator-=(const difference_type decrement)
Definition: iterator.h:440
#define PCP_DEFINE_RANGE(Range, Iterator, ReverseIterator)
Definition: iterator.h:521
PcpNodeIterator & operator-=(const difference_type decrement)
Definition: iterator.h:118
bool operator<(const PcpPropertyIterator &other) const
Definition: iterator.h:453
std::ptrdiff_t difference_type
Definition: iterator.h:369
PcpPrimReverseIterator(const PcpPrimIterator &iter)
Definition: iterator.h:341
reference operator[](const difference_type index) const
Definition: iterator.h:69
PcpPrimReverseIterator ReverseIteratorType
Definition: iterator.h:590
PcpPrimIterator & operator--()
Definition: iterator.h:245
constexpr size_t PCP_INVALID_INDEX
Definition: types.h:200
PCP_API PcpNodeIterator & MoveToNextSubtree()
difference_type operator-(const PcpPropertyIterator &other) const
Definition: iterator.h:397
PcpNodeReverseIterator ReverseIteratorType
Definition: iterator.h:583
PcpPropertyIterator operator++(int)
Definition: iterator.h:411
bool operator>(const PcpNodeIterator &other) const
Definition: iterator.h:141
pointer operator->() const
Definition: iterator.h:68
bool IsLocal() const
Definition: iterator.h:511
bool operator>(const PcpPrimIterator &other) const
Definition: iterator.h:302
Pcp_SdSiteRef _GetSiteRef() const
Definition: iterator.h:350
GLuint index
Definition: glcorearb.h:786
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
_PtrProxy pointer
Definition: iterator.h:207
PcpNodeIterator & operator+=(const difference_type increment)
Definition: iterator.h:113
bool operator>(const PcpPropertyIterator &other) const
Definition: iterator.h:463
PCP_API bool IsLocal() const
std::random_access_iterator_tag iterator_category
Definition: iterator.h:365
Pcp_CompressedSdSite GetCompressedSdSite(size_t layerIndex) const
Definition: iterator.h:54
reference operator[](const difference_type index) const
Definition: iterator.h:391
pointer operator->() const
Definition: iterator.h:390
reference operator*() const
Definition: iterator.h:228
std::ptrdiff_t difference_type
Definition: iterator.h:208
PcpNodeReverseIterator(const PcpNodeIterator &iter)
Definition: iterator.h:184
PCP_API PcpPrimIterator()
Constructs an invalid iterator.
PCP_API PcpNodeRef GetNode() const
Returns the PcpNode from which the current property originated.
PcpPropertyIterator operator+(const difference_type increment) const
Definition: iterator.h:423
const SdfPropertySpecHandle * pointer
Definition: iterator.h:368
PcpPrimIterator operator--(int)
Definition: iterator.h:256
std::random_access_iterator_tag iterator_category
Definition: iterator.h:44
PcpPropertyIterator operator--(int)
Definition: iterator.h:417
bool operator>=(const PcpPropertyIterator &other) const
Definition: iterator.h:468
PcpPropertyIterator & operator++()
Definition: iterator.h:401
PCP_API Pcp_SdSiteRef _GetSiteRef() const
PcpPrimIterator operator+(const difference_type increment) const
Definition: iterator.h:262
#define PCP_API
Definition: api.h:23
bool operator>=(const PcpPrimIterator &other) const
Definition: iterator.h:307