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