Loading...
Searching...
No Matches
request.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_REQUEST_H
8#define PXR_EXEC_VDF_REQUEST_H
9
11
12#include "pxr/pxr.h"
13
14#include "pxr/exec/vdf/api.h"
17
18#include "pxr/base/tf/bits.h"
19#include "pxr/base/tf/hash.h"
20
21#include <cstddef>
22#include <iterator>
23#include <memory>
24#include <vector>
25
26PXR_NAMESPACE_OPEN_SCOPE
27
28class VdfNetwork;
29
30class VdfRequest
31{
32public:
35 VDF_API
36 VdfRequest();
37
40 VDF_API
41 explicit VdfRequest(const VdfMaskedOutput& output);
42
47 VDF_API
48 explicit VdfRequest(const VdfMaskedOutputVector& vector);
49
55 VDF_API
56 explicit VdfRequest(VdfMaskedOutputVector&& vector);
57
60 VDF_API
61 ~VdfRequest();
62
64 // Queries
66
69 VDF_API
70 bool IsEmpty() const;
71
74 VDF_API
75 size_t GetSize() const;
76
81 VDF_API
82 const VdfNetwork* GetNetwork() const;
83
85 // Iterators
87
88 class const_iterator
89 {
90 public:
91 using iterator_category = std::forward_iterator_tag;
92 using value_type = const VdfMaskedOutput;
93 using reference = const VdfMaskedOutput &;
94 using pointer = const VdfMaskedOutput *;
95 using difference_type = std::ptrdiff_t;
96
97 VDF_API
98 const_iterator();
99
100 reference operator*() const { return _Dereference(); }
101 pointer operator->() const { return &(_Dereference()); }
102
103 const_iterator &operator++() {
104 _Increment();
105 return *this;
106 }
107
108 const_iterator operator++(int) {
109 const_iterator r(*this);
110 _Increment();
111 return r;
112 }
113
114 bool operator==(const const_iterator& rhs) const {
115 return _Equal(rhs);
116 }
117
118 bool operator!=(const const_iterator& rhs) const {
119 return !_Equal(rhs);
120 }
121
122 private:
123 friend class VdfRequest;
124
125 VDF_API
126 const_iterator(
127 const VdfMaskedOutput* mo,
128 const VdfMaskedOutput* first,
129 const TfBits* bits);
130
131 VDF_API
132 void _Increment();
133
134 bool _Equal(const const_iterator& rhs) const {
135 return _mo == rhs._mo;
136 }
137
138 const VdfMaskedOutput& _Dereference() const {
139 return *_mo;
140 }
141
142 VDF_API
143 size_t _ComputeIndex() const;
144
145 // Data members
146 const VdfMaskedOutput *_mo;
147 const VdfMaskedOutput *_firstMo;
148 const TfBits *_bits;
149 };
150
153 VDF_API
154 const_iterator begin() const;
155
158 VDF_API
159 const_iterator end() const;
160
162 // Random access using integer indices
164
165 class IndexedView
166 {
167 public:
170 IndexedView(const VdfRequest &request) : _r(&request) {}
171
174 size_t GetSize() const {
175 return _r->_request->size();
176 }
177
182 const VdfMaskedOutput *Get(const size_t i) const {
183 return !_r->_bits.GetSize() || _r->_bits.IsSet(i)
184 ? &(*_r->_request)[i]
185 : nullptr;
186 }
187
188 private:
189 const VdfRequest *_r;
190 };
191
193 // Request subset operators
195
199 VDF_API
200 void Add(const const_iterator& iterator);
201
205 VDF_API
206 void Remove(const const_iterator& iterator);
207
211 VDF_API
212 void AddAll();
213
218 VDF_API
219 void RemoveAll();
220
224 friend bool operator==(const VdfRequest& lhs, const VdfRequest& rhs)
225 {
226 return (lhs._request == rhs._request ||
227 *lhs._request == *rhs._request) &&
228 lhs._bits == rhs._bits;
229 }
230
234 friend bool operator!=(const VdfRequest& lhs, const VdfRequest& rhs)
235 {
236 return !(lhs == rhs);
237 }
238
239 struct Hash {
240 size_t operator()(const VdfRequest &request) const
241 {
242 size_t hash = TfBits::Hash()(request._bits);
243
244 // Instead of hashing on the complete request we just do it on the
245 // first three outputs (if any).
246 const size_t num = std::min<size_t>(request.GetSize(), 3);
247
248 const VdfMaskedOutputVector &outputs = *request._request;
249 for(size_t i = 0; i < num; i++) {
250 hash = TfHash::Combine(
251 hash, VdfMaskedOutput::Hash()(outputs[i]));
252 }
253
254 // Also add the last entry.
255 if (request.GetSize() > 3) {
256 hash = TfHash::Combine(
257 hash, VdfMaskedOutput::Hash()(outputs.back()));
258 }
259
260 return hash;
261 }
262 };
263
264private:
269 void _Add(size_t index);
270
275 void _Remove(size_t index);
276
277private:
278 // Internally held vector that is guaranteed to be sorted and uniqued.
279 std::shared_ptr<const VdfMaskedOutputVector> _request;
280
281 // Used for holding "subsets" without changing the vector.
282 // An empty bit set is a sentinel for a full vector (i.e. all the elements
283 // in the internally held vector are part of the request). This bit set
284 // should only ever be of size 0 or the size of the vector.
285 //
286 TfBits _bits;
287};
288
289PXR_NAMESPACE_CLOSE_SCOPE
290
291#endif
Fast bit array that keeps track of the number of bits set and can find the next set in a timely manne...
Definition: bits.h:49
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
Class to hold on to an externally owned output and a mask.
Definition: maskedOutput.h:32
A VdfNetwork is a collection of VdfNodes and their connections.
Definition: network.h:60
Hash for TfBits.
Definition: bits.h:61