Loading...
Searching...
No Matches
vectorIterator.h
1//
2// Copyright 2026 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_VECTOR_ITERATOR_H
8#define PXR_EXEC_VDF_VECTOR_ITERATOR_H
9
10#include "pxr/pxr.h"
11
12#include "pxr/exec/vdf/api.h"
13#include "pxr/exec/vdf/boxedContainer.h"
14#include "pxr/exec/vdf/compressedIndexMapping.h"
15#include "pxr/exec/vdf/vectorData.h"
16
17#include <cstddef>
18
19PXR_NAMESPACE_OPEN_SCOPE
20
23template <typename T>
25{
26public:
28 VdfVectorIterator() = default;
29
30 // Copyability is not provided because it is not currently required by
31 // clients. It would be non-trivial due to the potentially self-referential
32 // _mapping pointer.
33 VdfVectorIterator(const VdfVectorIterator&) = delete;
34 VdfVectorIterator& operator=(const VdfVectorIterator&) = delete;
35
37 bool IsAtEnd() const {
38 const size_t dataEndIndex = _end - _begin;
39 return _dataIndex == dataEndIndex;
40 }
41
44
46 const T& operator*() const {
48 return _begin[_dataIndex];
49 }
50
71 bool AdvanceTo(size_t idx);
72
73private:
74 friend class VdfVector;
75
76 explicit VdfVectorIterator(const Vdf_VectorData::Info& info);
77
78 // Returns the index of the current data element.
79 size_t _GetIndex() const {
80 return _mapping->logicalStartIndex + _logicalOffset;
81 }
82
83private:
84 // Offset from _begin of the current data element.
85 size_t _dataIndex = 0;
86 // Offset from _mapping->logicalStartIndex of the current data element.
87 size_t _logicalOffset = 0;
88 // Maps between logical and data indices in the current block. Points to
89 // _localMapping when iterating a non-compressed vector.
90 const Vdf_IndexBlockMapping *_mapping = nullptr;
91
92 // Semi-open range over all data elements stored by the vector.
93 const T *_begin = nullptr;
94 const T *_end = nullptr;
95 // Only vectors with a compressed layout, which are rare, have block
96 // mappings. For all other layouts, use a single block mapping that covers
97 // the entire range.
98 Vdf_IndexBlockMapping _localMapping = { 0, 0 };
99};
100
101template <typename T>
104{
105 ++_dataIndex;
106 ++_logicalOffset;
107 // Only iterators not at the end are allowed to be incremented so _mapping
108 // must be valid. When increment moves this iterator to the end, _mapping
109 // points past the end of its array. It cannot be accessed after it has
110 // been incremented.
111 if (_dataIndex == _mapping->dataEndIndex) {
112 // Reset the offset within the block when advancing to a new block.
113 _logicalOffset = 0;
114 ++_mapping;
115 }
116 return *this;
117}
118
119template <typename T>
120inline bool
122{
123 while (!IsAtEnd() && _GetIndex() < idx) {
124 ++(*this);
125 }
126
127 return !IsAtEnd() && _GetIndex() == idx;
128}
129
130template <typename T>
132 const Vdf_VectorData::Info &info)
133{
134 if (ARCH_UNLIKELY(info.compressedIndexMapping)) {
135 const Vdf_CompressedIndexMapping::_BlockMappings& mappings =
136 info.compressedIndexMapping->_blockMappings;
137 const size_t numValues = mappings.empty()
138 ? 0
139 : mappings.back().dataEndIndex;
140 _begin = reinterpret_cast<const T*>(info.data);
141 _end = reinterpret_cast<const T*>(info.data) + numValues;
142
143 _mapping = mappings.data();
144 }
145 else if (info.layout != Vdf_VectorData::Info::Layout::Boxed) {
146 const size_t numValues = info.size == 0
147 ? 0
148 : info.last - info.first + 1;
149 _begin = reinterpret_cast<const T*>(info.data);
150 _end = reinterpret_cast<const T*>(info.data) + numValues;
151
152 _localMapping.logicalStartIndex = info.first;
153 _localMapping.dataEndIndex = numValues;
154 _mapping = &_localMapping;
155 }
156 else {
157 using BoxedVectorType = Vdf_BoxedContainer<T>;
158 BoxedVectorType *boxedVector =
159 reinterpret_cast<BoxedVectorType*>(info.data);
160 const size_t numValues = boxedVector->size();
161 _begin = boxedVector->data();
162 _end = boxedVector->data() + numValues;
163
164 _localMapping.logicalStartIndex = 0;
165 _localMapping.dataEndIndex = numValues;
166 _mapping = &_localMapping;
167 }
168}
169
170PXR_NAMESPACE_CLOSE_SCOPE
171
172#endif
This simple container stores multiple values that flow through the network as a single data flow elem...
size_t size() const
Returns the number of elements stored in this container.
This class is used to abstract away knowledge of the cache data used for each node.
Definition vector.h:59
A read-only iterator over values held by a VdfVector.
bool AdvanceTo(size_t idx)
Advances this to a data element at or past idx.
const T & operator*() const
Returns the data element pointed to by this.
bool IsAtEnd() const
Returns true if this does not point to an element in the vector.
VdfVectorIterator()=default
Construct an iterator that is at end and not associated with a vector.
VdfVectorIterator & operator++()
Advances the iterator to the next data element.
#define TF_DEV_AXIOM(cond)
The same as TF_AXIOM, but compiled only in dev builds.
Definition diagnostic.h:206
A mapping that relates logical blocks of indices to actual stored data when the data is compressed by...