Loading...
Searching...
No Matches
iterator.h
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
16#include "pxr/usd/sdf/site.h"
17
19
20#include <iterator>
21
22PXR_NAMESPACE_OPEN_SCOPE
23
24class PcpPrimIndex;
25class PcpPrimIndex_Graph;
27
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 };
43public:
44 using iterator_category = std::random_access_iterator_tag;
45 using value_type = PcpNodeRef;
46 using reference = PcpNodeRef;
47 using pointer = _PtrProxy;
48 using difference_type = std::ptrdiff_t;
49
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.
65 PCP_API PcpNodeIterator& MoveToNextSubtree();
66
67 reference operator*() const { return dereference(); }
68 pointer operator->() const { return pointer(dereference()); }
69 reference operator[](const difference_type index) const {
70 PcpNodeIterator advanced(*this);
71 advanced.advance(index);
72 return advanced.dereference();
73 }
74
75 difference_type operator-(const PcpNodeIterator& other) const {
76 return -distance_to(other);
77 }
78
79 PcpNodeIterator& operator++() {
80 increment();
81 return *this;
82 }
83
84 PcpNodeIterator& operator--() {
85 decrement();
86 return *this;
87 }
88
89 PcpNodeIterator operator++(int) {
90 PcpNodeIterator result(*this);
91 increment();
92 return result;
93 }
94
95 PcpNodeIterator operator--(int) {
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
113 PcpNodeIterator& operator+=(const difference_type increment) {
114 advance(increment);
115 return *this;
116 }
117
118 PcpNodeIterator& operator-=(const difference_type decrement) {
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
151private:
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
169private:
170 PcpPrimIndex_Graph* _graph = nullptr;
171 size_t _nodeIdx = PCP_INVALID_INDEX;
172};
173
180 : public Tf_ProxyReferenceReverseIterator<PcpNodeIterator>
181{
182public:
184 explicit PcpNodeReverseIterator(const PcpNodeIterator& iter)
185 : Tf_ProxyReferenceReverseIterator<PcpNodeIterator>(iter) {}
186};
187
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 };
203public:
204 using iterator_category = std::random_access_iterator_tag;
205 using value_type = SdfSite;
206 using reference = SdfSite;
207 using pointer = _PtrProxy;
208 using difference_type = std::ptrdiff_t;
209
211 PCP_API
213
216 PCP_API
217 PcpPrimIterator(const PcpPrimIndex* primIndex, size_t pos);
218
220 PCP_API
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()); }
230 reference operator[](const difference_type index) const {
231 PcpPrimIterator advanced(*this);
232 advanced.advance(index);
233 return advanced.dereference();
234 }
235
236 difference_type operator-(const PcpPrimIterator& other) const {
237 return -distance_to(other);
238 }
239
240 PcpPrimIterator& operator++() {
241 increment();
242 return *this;
243 }
244
245 PcpPrimIterator& operator--() {
246 decrement();
247 return *this;
248 }
249
250 PcpPrimIterator operator++(int) {
251 PcpPrimIterator result(*this);
252 increment();
253 return result;
254 }
255
256 PcpPrimIterator operator--(int) {
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
274 PcpPrimIterator& operator+=(const difference_type increment) {
275 advance(increment);
276 return *this;
277 }
278
279 PcpPrimIterator& operator-=(const difference_type decrement) {
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
312private:
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
326private:
327 const PcpPrimIndex* _primIndex = nullptr;
328 size_t _pos = PCP_INVALID_INDEX;
329};
330
337 : public Tf_ProxyReferenceReverseIterator<PcpPrimIterator>
338{
339public:
341 explicit PcpPrimReverseIterator(const PcpPrimIterator& iter)
342 : Tf_ProxyReferenceReverseIterator<PcpPrimIterator>(iter) { }
343
344 PcpNodeRef GetNode() const
345 {
346 PcpPrimIterator tmp = base();
347 return (--tmp).GetNode();
348 }
349
350 Pcp_SdSiteRef _GetSiteRef() const
351 {
352 PcpPrimIterator tmp = base();
353 return (--tmp)._GetSiteRef();
354 }
355};
356
363{
364public:
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
372 PCP_API
374
377 PCP_API
378 PcpPropertyIterator(const PcpPropertyIndex& index, size_t pos = 0);
379
381 PCP_API
383
386 PCP_API
387 bool IsLocal() const;
388
389 reference operator*() const { return dereference(); }
390 pointer operator->() const { return &(dereference()); }
391 reference operator[](const difference_type index) const {
392 PcpPropertyIterator advanced(*this);
393 advanced.advance(index);
394 return advanced.dereference();
395 }
396
397 difference_type operator-(const PcpPropertyIterator& other) const {
398 return -distance_to(other);
399 }
400
401 PcpPropertyIterator& operator++() {
402 increment();
403 return *this;
404 }
405
406 PcpPropertyIterator& operator--() {
407 decrement();
408 return *this;
409 }
410
411 PcpPropertyIterator operator++(int) {
412 PcpPropertyIterator result(*this);
413 increment();
414 return result;
415 }
416
417 PcpPropertyIterator operator--(int) {
418 PcpPropertyIterator result(*this);
419 decrement();
420 return result;
421 }
422
423 PcpPropertyIterator operator+(const difference_type increment) const {
424 PcpPropertyIterator result(*this);
425 result.advance(increment);
426 return result;
427 }
428
429 PcpPropertyIterator operator-(const difference_type decrement) const {
430 PcpPropertyIterator result(*this);
431 result.advance(-decrement);
432 return result;
433 }
434
435 PcpPropertyIterator& operator+=(const difference_type increment) {
436 advance(increment);
437 return *this;
438 }
439
440 PcpPropertyIterator& operator-=(const difference_type decrement) {
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
473private:
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
487private:
488 const PcpPropertyIndex* _propertyIndex = nullptr;
489 size_t _pos = 0;
490};
491
498 : public std::reverse_iterator<PcpPropertyIterator>
499{
500public:
503 : std::reverse_iterator<PcpPropertyIterator>(iter) { }
504
505 PcpNodeRef GetNode() const
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
567PCP_DEFINE_RANGE(PcpNodeRange, PcpNodeIterator, PcpNodeReverseIterator);
568PCP_DEFINE_RANGE(PcpPrimRange, PcpPrimIterator, PcpPrimReverseIterator);
569PCP_DEFINE_RANGE(PcpPropertyRange, PcpPropertyIterator,
571
577template <class Iterator> struct PcpIteratorTraits;
578
579template <>
581{
582 typedef PcpNodeRange RangeType;
583 typedef PcpNodeReverseIterator ReverseIteratorType;
584};
585
586template <>
588{
589 typedef PcpPrimRange RangeType;
590 typedef PcpPrimReverseIterator ReverseIteratorType;
591};
592
593template <>
595{
596 typedef PcpPropertyRange RangeType;
597 typedef PcpPropertyReverseIterator ReverseIteratorType;
598};
599
600PXR_NAMESPACE_CLOSE_SCOPE
601
602#endif // PXR_USD_PCP_ITERATOR_H
A simple iterator adapter for STL containers.
Object used to iterate over nodes in the prim index graph in strong-to-weak order.
Definition: iterator.h:34
PcpNodeIterator()=default
Constructs an invalid iterator.
PcpNode represents a node in an expression tree for compositing scene description.
Definition: node.h:47
Object used to iterate over nodes in the prim index graph in weak-to-strong order.
Definition: iterator.h:181
PcpPrimIndex is an index of the all sites of scene description that contribute opinions to a specific...
Definition: primIndex.h:62
Object used to iterate over prim specs in the prim index graph in strong-to-weak order.
Definition: iterator.h:194
PCP_API PcpPrimIterator()
Constructs an invalid iterator.
PCP_API PcpPrimIterator(const PcpPrimIndex *primIndex, size_t pos)
Constructs a prim iterator beginning at position pos in the prim stack of prim index primIndex.
PCP_API PcpNodeRef GetNode() const
Returns the PcpNode from which the current prim originated.
Object used to iterate over prim specs in the prim index graph in weak-to-strong order.
Definition: iterator.h:338
PcpPropertyIndex is an index of all sites in scene description that contribute opinions to a specific...
Definition: propertyIndex.h:49
Object used to iterate over property specs in a property index in strong-to-weak order.
Definition: iterator.h:363
PCP_API PcpPropertyIterator(const PcpPropertyIndex &index, size_t pos=0)
Constructs a property iterator for index beginning at position pos in the property stack.
PCP_API PcpPropertyIterator()
Constructs an invalid iterator.
PCP_API bool IsLocal() const
Returns true if the current property is local to the owning property index's layer stack,...
PCP_API PcpNodeRef GetNode() const
Returns the PcpNode from which the current property originated.
Object used to iterate over property specs in a property index in weak-to-strong order.
Definition: iterator.h:499
An SdfSite is a simple representation of a location in a layer where opinions may possibly be found.
Definition: site.h:26
#define TF_DEV_AXIOM(cond)
The same as TF_AXIOM, but compiled only in dev builds.
Definition: diagnostic.h:205
Traits class for retrieving useful characteristics about one of the Pcp iterator types above.
Definition: iterator.h:577
constexpr size_t PCP_INVALID_INDEX
A value which indicates an invalid index.
Definition: types.h:198