Loading...
Searching...
No Matches
boxedContainer.h
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_BOXED_CONTAINER_H
8#define PXR_EXEC_VDF_BOXED_CONTAINER_H
9
10#include "pxr/pxr.h"
11
12#include "pxr/exec/vdf/boxedContainerTraits.h"
13
15
16PXR_NAMESPACE_OPEN_SCOPE
17
22{
23public:
27 struct Range
28 {
29 unsigned int begin;
30 unsigned int end;
31 };
32
35 Vdf_BoxedRanges() = default;
36
39 explicit Vdf_BoxedRanges(unsigned int n)
40 : _ranges(1, Range{0, n})
41 {}
42
45 unsigned int GetNumRanges() const {
46 return _ranges.size();
47 }
48
52 Range GetRange(unsigned int i) const {
53 return _ranges[i];
54 }
55
58 void AppendRange(unsigned int begin, unsigned int end) {
59 _ranges.push_back(Range{begin, end});
60 }
61
62 // Overload for ADL swap idiom
63 friend inline void swap(Vdf_BoxedRanges &lhs, Vdf_BoxedRanges &rhs) {
64 lhs._ranges.swap(rhs._ranges);
65 }
66
67private:
68 // The individual ranges stored in this vector denoted by the end element
69 // index of each range.
71};
72
83template<typename T>
85{
86 static_assert(
87 !Vdf_IsBoxedContainer<T>, "Recursive boxing is not allowed");
88
89 // Adjust the local capacity of our data so that we use as much local
90 // capacity as we can without growing sizeof(_DataVec). We do this so
91 // that the boxed container always fits in Vdf_VectorData::DataHolder.
92 // Otherwise, we would have to separately allocate the boxed container
93 // from the vector impl, which defeats the purpose of TfSmallVector's
94 // local storage.
95 static const TfSmallVectorBase::size_type N =
96 TfSmallVectorBase::ComputeSerendipitousLocalCapacity<T>();
98
99public:
100
104
108 explicit Vdf_BoxedContainer(unsigned int n) :
109 _data(n, _DataVec::DefaultInit),
110 _ranges(n)
111 {}
112
115 bool operator==(const Vdf_BoxedContainer &rhs) const {
116 return _data == rhs._data;
117 }
118
121 bool operator!=(const Vdf_BoxedContainer &rhs) const {
122 return !operator==(rhs);
123 }
124
127 bool empty() const {
128 return _data.empty();
129 }
130
133 size_t size() const {
134 return _data.size();
135 }
136
139 void reserve(unsigned int n) {
140 _data.reserve(n);
141 }
142
145 const T &operator[](unsigned int i) const {
146 return _data[i];
147 }
148
151 T &operator[](unsigned int i) {
152 return _data[i];
153 }
154
157 const T *data() const {
158 return _data.data();
159 }
160
163 T *data() {
164 return _data.data();
165 }
166
169 const Vdf_BoxedRanges &GetRanges() const {
170 return _ranges;
171 }
172
176 template<typename Iterator>
177 void AppendRange(Iterator begin, Iterator end);
178
179 // Overload for ADL swap idiom
180 friend inline void swap(Vdf_BoxedContainer &lhs, Vdf_BoxedContainer &rhs) {
181 lhs._data.swap(rhs._data);
182 swap(lhs._ranges, rhs._ranges);
183 }
184
185private:
186
187 // The elements stored in this container.
188 _DataVec _data;
189
190 // The individual ranges stored in this vector denoted by the end element
191 // index of each range.
192 Vdf_BoxedRanges _ranges;
193
194};
195
197
198template<typename T>
199template<typename Iterator>
200void
201Vdf_BoxedContainer<T>::AppendRange(Iterator begin, Iterator end) {
202 const unsigned int previousSize = _data.size();
203 _data.insert(_data.end(), begin, end);
204 _ranges.AppendRange(previousSize, _data.size());
205}
206
207PXR_NAMESPACE_CLOSE_SCOPE
208
209#endif /* PXR_EXEC_VDF_BOXED_CONTAINER_H */
This is a small-vector class with local storage optimization, the local storage can be specified via ...
Definition: smallVector.h:157
void reserve(size_type newCapacity)
Reserve storage for newCapacity entries.
Definition: smallVector.h:410
size_type size() const
Returns the current size of the vector.
Definition: smallVector.h:596
bool empty() const
Returns true if this vector is empty.
Definition: smallVector.h:608
void swap(TfSmallVector &rhs)
Swap two vector instances.
Definition: smallVector.h:298
value_type * data()
Direct access to the underlying array.
Definition: smallVector.h:735
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.
const T * data() const
Returns a pointer to the immutable data elements.
bool operator!=(const Vdf_BoxedContainer &rhs) const
Returns true if rhs does not compare equal with this container.
bool empty() const
Returns true if the container does not hold any elements.
bool operator==(const Vdf_BoxedContainer &rhs) const
Returns true if rhs compares equal with this container.
T & operator[](unsigned int i)
Returns the mutable value stored at index i.
void AppendRange(Iterator begin, Iterator end)
Appends the data elements [ begin, end ) to the end of the container, and adds a new group containing...
const Vdf_BoxedRanges & GetRanges() const
Returns the subranges of boxed data.
Vdf_BoxedContainer()=default
Constructs an empty container with no elements and no ranges.
const T & operator[](unsigned int i) const
Returns the immutable value stored at index i.
Vdf_BoxedContainer(unsigned int n)
Constructs a container with n elements and one range containing all elements.
T * data()
Returns a pointer to the mutable data elements.
void reserve(unsigned int n)
Reserves storage for n elements in this container.
Each range represents a logical group of elements stored in a Vdf_BoxedContainer.
void AppendRange(unsigned int begin, unsigned int end)
Appends a new group.
Range GetRange(unsigned int i) const
Returns the range at index i.
Vdf_BoxedRanges()=default
Constructs an empty set of boxed ranges.
Vdf_BoxedRanges(unsigned int n)
Constructs a set with one range containing all n elements.
unsigned int GetNumRanges() const
Returns the number of individual ranges stored in this container.
A range of data elements as denoted by [ begin, end ) indices.