HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
boxedContainer.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_BOXED_CONTAINER_H
8 #define PXR_EXEC_VDF_BOXED_CONTAINER_H
9 
10 #include "pxr/pxr.h"
11 
13 
15 
17 
18 /// Each range represents a logical group of elements stored in a
19 /// Vdf_BoxedContainer.
20 ///
22 {
23 public:
24  /// A range of data elements as denoted by [ begin, end ) indices. Each
25  /// range of elements represents a logical group of data elements.
26  ///
27  struct Range
28  {
29  unsigned int begin;
30  unsigned int end;
31  };
32 
33  /// Constructs an empty set of boxed ranges.
34  ///
35  Vdf_BoxedRanges() = default;
36 
37  /// Constructs a set with one range containing all \p n elements.
38  ///
39  explicit Vdf_BoxedRanges(unsigned int n)
40  : _ranges(1, Range{0, n})
41  {}
42 
43  /// Returns the number of individual ranges stored in this container.
44  ///
45  unsigned int GetNumRanges() const {
46  return _ranges.size();
47  }
48 
49  /// Returns the range at index \p i. Each range represents a logical group
50  /// of data elements.
51  ///
52  Range GetRange(unsigned int i) const {
53  return _ranges[i];
54  }
55 
56  /// Appends a new group.
57  ///
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 
67 private:
68  // The individual ranges stored in this vector denoted by the end element
69  // index of each range.
71 };
72 
73 ////////////////////////////////////////////////////////////////////////////////
74 ///
75 /// \class Vdf_BoxedContainer
76 ///
77 /// This simple container stores multiple values that flow through the network
78 /// as a single data flow element. It enables data flow of vectorized data
79 /// without encoding the length of that data in the topology of the network.
80 /// This container is transparent to client code, such that its contents can
81 /// be consumed just like any vectorized data.
82 ///
83 template<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 
99 public:
100 
101  /// Constructs an empty container with no elements and no ranges.
102  ///
103  Vdf_BoxedContainer() = default;
104 
105  /// Constructs a container with \p n elements and one range containing all
106  /// elements. Each element will be default initialized.
107  ///
108  explicit Vdf_BoxedContainer(unsigned int n) :
109  _data(n, _DataVec::DefaultInit),
110  _ranges(n)
111  {}
112 
113  /// Returns \c true if \p rhs compares equal with this container.
114  ///
115  bool operator==(const Vdf_BoxedContainer &rhs) const {
116  return _data == rhs._data;
117  }
118 
119  /// Returns \c true if \p rhs does not compare equal with this container.
120  ///
121  bool operator!=(const Vdf_BoxedContainer &rhs) const {
122  return !operator==(rhs);
123  }
124 
125  /// Returns \c true if the container does not hold any elements.
126  ///
127  bool empty() const {
128  return _data.empty();
129  }
130 
131  /// Returns the number of elements stored in this container.
132  ///
133  size_t size() const {
134  return _data.size();
135  }
136 
137  /// Reserves storage for \p n elements in this container.
138  ///
139  void reserve(unsigned int n) {
140  _data.reserve(n);
141  }
142 
143  /// Returns the immutable value stored at index \p i.
144  ///
145  const T &operator[](unsigned int i) const {
146  return _data[i];
147  }
148 
149  /// Returns the mutable value stored at index \p i.
150  ///
151  T &operator[](unsigned int i) {
152  return _data[i];
153  }
154 
155  /// Returns a pointer to the immutable data elements.
156  ///
157  const T *data() const {
158  return _data.data();
159  }
160 
161  /// Returns a pointer to the mutable data elements.
162  ///
163  T *data() {
164  return _data.data();
165  }
166 
167  /// Returns the subranges of boxed data.
168  ///
169  const Vdf_BoxedRanges &GetRanges() const {
170  return _ranges;
171  }
172 
173  /// Appends the data elements [ \p begin, \p end ) to the end of the
174  /// container, and adds a new group containing those same data elements.
175  ///
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 
185 private:
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 
196 ///////////////////////////////////////////////////////////////////////////////
197 
198 template<typename T>
199 template<typename Iterator>
200 void
202  const unsigned int previousSize = _data.size();
203  _data.insert(_data.end(), begin, end);
204  _ranges.AppendRange(previousSize, _data.size());
205 }
206 
208 
209 #endif /* PXR_EXEC_VDF_BOXED_CONTAINER_H */
Vdf_BoxedRanges(unsigned int n)
void reserve(unsigned int n)
Vdf_BoxedRanges()=default
Vdf_BoxedContainer(unsigned int n)
bool operator==(const Vdf_BoxedContainer &rhs) const
void reserve(size_type newCapacity)
Definition: smallVector.h:410
const Vdf_BoxedRanges & GetRanges() const
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
unsigned int GetNumRanges() const
size_type size() const
Definition: smallVector.h:596
bool empty() const
Definition: smallVector.h:608
bool operator!=(const Vdf_BoxedContainer &rhs) const
void AppendRange(Iterator begin, Iterator end)
GLdouble n
Definition: glcorearb.h:2008
bool empty() const
Range GetRange(unsigned int i) const
GLuint GLuint end
Definition: glcorearb.h:475
friend void swap(Vdf_BoxedRanges &lhs, Vdf_BoxedRanges &rhs)
void AppendRange(unsigned int begin, unsigned int end)
friend void swap(Vdf_BoxedContainer &lhs, Vdf_BoxedContainer &rhs)
Vdf_BoxedContainer()=default
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:587
const T & operator[](unsigned int i) const
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
size_t size() const
std::size_t size_type
Definition: smallVector.h:50
T & operator[](unsigned int i)
void swap(TfSmallVector &rhs)
Definition: smallVector.h:298
const T * data() const
value_type * data()
Definition: smallVector.h:735