Loading...
Searching...
No Matches
maskedIterator.h
Go to the documentation of this file.
1//
2// Copyright 2025 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_EXEC_VDF_MASKED_ITERATOR_H
8#define PXR_EXEC_VDF_MASKED_ITERATOR_H
9
11
12#include "pxr/pxr.h"
13
15#include "pxr/exec/vdf/mask.h"
16
17PXR_NAMESPACE_OPEN_SCOPE
18
24{
27 VisitUnset = 0,
28
32};
33
42template<class IteratorType, VdfMaskedIteratorMode mode>
43class VdfMaskedIterator final : public VdfIterator
44{
45public:
46
49 typedef typename IteratorType::value_type value_type;
50
53 typedef typename IteratorType::reference reference;
54
57 template<typename... Args>
59 const VdfContext &context, const VdfMask &visitMask, Args&&... args);
60
64
68 return _iterator.operator*();
69 }
70
73 bool IsAtEnd() const {
74 return _iterator.IsAtEnd();
75 }
76
79 void AdvanceToEnd() {
80 _iterator.AdvanceToEnd();
81 }
82
83private:
84
85 // Returns the current index into the data source.
86 friend int Vdf_GetIteratorIndex(const VdfMaskedIterator &it) {
87 return Vdf_GetIteratorIndex(it._iterator);
88 }
89
90 // Advance the underlying iterator as needed.
91 void _AdvanceToIndexWithVisitMask();
92
93 // The underlying iterator.
94 IteratorType _iterator;
95
96 // Mask of elements that are skipped during iteration.
97 VdfMask::iterator _visitMaskIterator;
98};
99
101
102template<class IteratorType, VdfMaskedIteratorMode mode>
103template<typename... Args>
105 const VdfContext &context, const VdfMask &visitMask, Args&&... args) :
106 _iterator(context, std::forward<Args>(args)...),
107 _visitMaskIterator(visitMask.begin())
108{
109 // If we get an empty mask passed in, advance the iterator to the end.
110 if (visitMask.GetSize() == 0) {
111 _iterator.AdvanceToEnd();
112 }
113
114 _AdvanceToIndexWithVisitMask();
115}
116
117template<class IteratorType, VdfMaskedIteratorMode mode>
120{
121 _iterator.operator++();
122 _AdvanceToIndexWithVisitMask();
123 return *this;
124}
125
126template<class IteratorType, VdfMaskedIteratorMode mode>
127void
129{
130 // XXX: this could be more efficient by
131 // - exposing a method to advance to the next element after a given
132 // index in the base iterator.
133 // - using those functions in the below while loop so that we skip one
134 // contiguous block of elements in visitMask at a time.
135 // - this scheme also makes it hard to set the masked iterator to
136 // "IsAtEnd" in the constructor, because advancing the mask iterator to
137 // the end (none set) doesn't mean that this iterator is at end due to
138 // the logic below. That in turn makes it hard to check if the mask size
139 // and iterator data matches up.
140
141 while (!_iterator.IsAtEnd()) {
142 // Pull the visit mask iterator forward.
143 const VdfMask::iterator::value_type idx(
144 Vdf_GetIteratorIndex(_iterator));
145 if (*_visitMaskIterator < idx) {
146 _visitMaskIterator.AdvanceTo(idx);
147 }
148
149 const VdfMask::iterator::value_type visitIdx = *_visitMaskIterator;
151 // If we hit an element we don't want to skip, stop. Also stop,
152 // if we were unable to pull the visit mask forward (invalid visit
153 // mask case).
154 if (visitIdx > idx || visitIdx < idx) {
155 break;
156 }
157 } else {
158 // If we hit an element we want to visit, stop. Also stop, if we
159 // were unable to pull the visit mask forward (invalid visit mask
160 // case).
161 if (visitIdx == idx || visitIdx < idx) {
162 break;
163 }
164 }
165 // Otherwise continue iterating.
166 _iterator.operator++();
167 }
168}
169
170PXR_NAMESPACE_CLOSE_SCOPE
171
172#endif
A context is the parameter bundle passed to callbacks of computations.
Definition: context.h:40
Base class for libVdf iterators.
Definition: iterator.h:36
Iterator class used to iterate through the elements of the mask.
Definition: mask.h:399
A VdfMask is placed on connections to specify the data flowing through them.
Definition: mask.h:37
size_t GetSize() const
Returns the size of the mask.
Definition: mask.h:158
An iterator that can be used to refine the given iterator IteratorType to iterate over a given mask b...
bool IsAtEnd() const
Returns true if the iterator is done iterating and false otherwise.
void AdvanceToEnd()
Advance the iterator to the end.
IteratorType::reference reference
The dereference type of this iterator.
VdfMaskedIterator(const VdfContext &context, const VdfMask &visitMask, Args &&... args)
Constructor.
reference operator*() const
Returns reference to current element.
IteratorType::value_type value_type
The value type of this iterator.
VdfMaskedIterator & operator++()
Increment operator to point to the next element.
VdfMaskedIteratorMode
Enum to specify the behavior of VdfMaskedIterator as template parameter.
@ VisitUnset
The elements in the visitMask are skipped (default).
@ VisitSet
Visit the elements in the visitMask instead of skipping them.
STL namespace.