7#ifndef PXR_EXEC_VDF_WEIGHTED_ITERATOR_H
8#define PXR_EXEC_VDF_WEIGHTED_ITERATOR_H
23#include <initializer_list>
25PXR_NAMESPACE_OPEN_SCOPE
68 if (_begin != _local) {
84 _begin = numInputs > NUM_LOCAL_STORAGE
87 _end = _begin + numInputs;
143 static const size_t NUM_LOCAL_STORAGE = 1;
145 value_type _local[NUM_LOCAL_STORAGE];
154template<
class IteratorType>
169 template<
typename... Args>
174 _iterator(context,
std::forward<Args>(args)...) {
175 const TfToken *weightNamePtr = &weightName;
176 _Init(context, weightNamePtr, weightNamePtr + 1);
182 template<
typename... Args>
185 std::initializer_list<TfToken> weightNames,
187 _iterator(context,
std::forward<Args>(args)...) {
188 _Init(context, weightNames.begin(), weightNames.end());
193 template<
typename... Args>
196 const std::vector<TfToken> &weightNames,
198 _iterator(context,
std::forward<Args>(args)...) {
199 const TfToken *weightNamesBegin = weightNames.
data();
200 const TfToken *weightNamesEnd = weightNamesBegin + weightNames.
size();
201 _Init(context, weightNamesBegin, weightNamesEnd);
215 return _iterator.operator*();
221 return _iterator.IsAtEnd();
229 return Vdf_GetIteratorIndex(_iterator);
235 _iterator.AdvanceToEnd();
242 double GetWeight(
size_t slot = 0,
double defWeight = 0.0)
const {
243 _GetExplicitWeight(slot, &defWeight);
252 return _GetExplicitWeight(slot, &ret);
260 size_t slot = 0,
double defWeight = 0.0)
const;
265 return _slots.
size();
271 return (slot < _slots.
size()) ?
272 _slots[slot].weights->GetSize() : 0;
283 return Vdf_GetIteratorIndex(it._iterator);
294 void _AddWeightsInput(
301 bool _GetExplicitWeight(
size_t slot,
double *ret)
const;
311 void _AdvanceIterator();
316 IteratorType _iterator;
322template<
class IteratorType>
330 if (_iterator.IsAtEnd()) {
335 const std::ptrdiff_t numInputs = std::distance(begin, end);
339 _slots.Allocate(numInputs);
344 const VdfNode &node = _GetNode(context);
345 for (
const TfToken *it = begin; it != end; ++it, ++slot) {
349 "Can't find input '%s' on node %s",
353 _AddWeightsInput(slot, input, context);
357 if (numInputs == 0) {
368template<
class IteratorType>
378 const VdfVector &out = _GetRequiredInputValue(
379 context, connection, connection.
GetMask());
382 if (ARCH_LIKELY(out.
GetSize() == 1)) {
392 TF_CODING_ERROR(
"Weight input must have exactly one element (got %zu)",
398 TF_CODING_ERROR(
"Weight connector must have at most one input (got %zu)",
407template<
class IteratorType>
421 _iterator.operator++();
427template<
class IteratorType>
431 while (!_iterator.IsAtEnd()) {
434 const int currentIndex = Vdf_GetIteratorIndex(_iterator);
435 const int nextExplicitIndex =
436 _slots.AdvanceToNextExplicitIndex(currentIndex);
439 if (nextExplicitIndex == std::numeric_limits<int>::max()) {
441 _iterator.AdvanceToEnd();
444 }
else if (nextExplicitIndex != currentIndex) {
448 while (!_iterator.IsAtEnd() &&
449 Vdf_GetIteratorIndex(_iterator) < nextExplicitIndex) {
453 if (_iterator.IsAtEnd() ||
454 Vdf_GetIteratorIndex(_iterator) == nextExplicitIndex) {
477 int nextExplicitIndex = std::numeric_limits<int>::max();
481 const VdfIndexedWeights *w = p.
weights;
484 if (ARCH_UNLIKELY(!w || p.
currentIndex >= w->GetSize())) {
497 if (wIndexVal < nextExplicitIndex) {
498 nextExplicitIndex = wIndexVal;
503 return nextExplicitIndex;
506template<
class IteratorType>
507std::pair<bool, double>
509 size_t slot,
double defWeight)
const
511 bool hasExplicitWeight = _GetExplicitWeight(slot, &defWeight);
512 return std::pair<bool, double>(hasExplicitWeight, defWeight);
515template<
class IteratorType>
518 size_t slot,
double *ret)
const
522 if (slot < _slots.size()) {
527 Vdf_GetIteratorIndex(_iterator)) {
535PXR_NAMESPACE_CLOSE_SCOPE
Low-level utilities for informing users of various internal and external diagnostic conditions.
Token for efficient comparison, assignment, and hashing of known strings.
size_t size() const
Return the size of the string that this token represents.
char const * data() const
Synonym for GetText().
Array of weight slots for weighted iterators.
size_t size() const
Return the number of slots in the array.
const_iterator begin() const
Return an iterator to the first slot in the array.
reference operator[](size_t slot)
Access the slot at index slot.
const_reference operator[](size_t slot) const
Access the slot at index slot.
void Allocate(uint32_t numInputs)
Allocate storage for numInputs elements.
~Vdf_WeightSlotArray()
Destructor.
Vdf_WeightSlotArray()
Construct an empty array.
iterator end()
Return an iterator to the end of the array.
const_iterator end() const
Return an iterator to the end of the array.
iterator begin()
Return an iterator to the first slot in the array.
int AdvanceToNextExplicitIndex(int index)
Returns the next index that has a weight at or after index.
A class that fully represents a connection between two VdfNodes.
const VdfMask & GetMask() const
Returns the mask for this connection.
A context is the parameter bundle passed to callbacks of computations.
Base class for libVdf iterators.
This is the base class for all nodes in a VdfNetwork.
VDF_API const std::string GetDebugName() const
Returns the debug name for this node, if one is registered.
const VdfInput * GetInput(const TfToken &inputName) const
Returns the connector named inputName, returns NULL if no input of that name exists.
This class is used to abstract away knowledge of the cache data used for each node.
size_t GetSize() const
Returns the number of elements held in this vector.
ReadAccessor< TYPE > GetReadAccessor() const
GetReadAccessor() allows low level read-only access to the content of of the VdfVector via the Vdf_Ve...
This iterator can be used to iterate through an input that is weighted by one or more weight vectors.
std::pair< bool, double > GetExplicitWeight(size_t slot=0, double defWeight=0.0) const
Returns a pair (bool, double) indicating whether there is a weight explicitly present at the given sl...
bool HasExplicitWeight(size_t slot) const
Returns true if the weight at the current element is explicitly set at slot.
size_t GetNumSlots() const
Get the number of weight slots used.
bool IsAtEnd() const
Returns true if the iterator is done iterating and false otherwise.
void AdvanceToEnd()
Advance the iterator to the end.
VdfWeightedIterator(const VdfContext &context, const TfToken &weightName, Args &&... args)
Constructs a weighted iterator using a single weight name.
IteratorType::reference reference
Type of a reference to a value of this iterator.
size_t GetNumExplicitWeights(size_t slot=0) const
Returns the # of explicit weights for slot.
double GetWeight(size_t slot=0, double defWeight=0.0) const
Returns the weight at the current element.
~VdfWeightedIterator()=default
Destructor.
VdfWeightedIterator(const VdfContext &context, std::initializer_list< TfToken > weightNames, Args &&... args)
Constructs a weighted iterator using an initializer list for weight names.
VdfWeightedIterator(const VdfContext &context, const std::vector< TfToken > &weightNames, Args &&... args)
Constructs a weighted iterator using a vector for weight names.
VdfWeightedIterator & operator++()
Increment operator to point to the next element.
reference operator*() const
Returns reference to current element.
int GetCurrentIndex() const
Returns the current index for the current connection.
IteratorType::value_type value_type
Type of the elements this iterator gives access to.
#define TF_DEV_AXIOM(cond)
The same as TF_AXIOM, but compiled only in dev builds.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
size_t currentIndex
The current iterator index into the VdfIndexedWeights above.
const VdfIndexedWeights * weights
The vector of weights we are iterating over.
The information held per weight slot in a weighted iterator.