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