Loading...
Searching...
No Matches
functions.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the Apache License, Version 2.0 (the "Apache License")
5// with the following modification; you may not use this file except in
6// compliance with the Apache License and the following modification to it:
7// Section 6. Trademarks. is deleted and replaced with:
8//
9// 6. Trademarks. This License does not grant permission to use the trade
10// names, trademarks, service marks, or product names of the Licensor
11// and its affiliates, except as required to comply with Section 4(c) of
12// the License and to reproduce the content of the NOTICE file.
13//
14// You may obtain a copy of the Apache License at
15//
16// http://www.apache.org/licenses/LICENSE-2.0
17//
18// Unless required by applicable law or agreed to in writing, software
19// distributed under the Apache License with the above modification is
20// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21// KIND, either express or implied. See the Apache License for the specific
22// language governing permissions and limitations under the Apache License.
23//
24#if !BOOST_PP_IS_ITERATING
25
26#ifndef PXR_BASE_VT_FUNCTIONS_H
27#define PXR_BASE_VT_FUNCTIONS_H
28
30
31#include "pxr/pxr.h"
32#include "pxr/base/vt/api.h"
33#include "pxr/base/vt/array.h"
34#include <boost/preprocessor/enum_params.hpp>
35#include <boost/preprocessor/iterate.hpp>
36#include <boost/preprocessor/repeat.hpp>
37#include <vector>
38
39PXR_NAMESPACE_OPEN_SCOPE
40
41#define VT_FUNCTIONS_MAX_ARGS 6
42
43// Set up preprocessor iterations to allow various functions to accept multiple
44// arguments.
45
46// VtCat
47#define BOOST_PP_ITERATION_PARAMS_1 (4, \
48 (0, VT_FUNCTIONS_MAX_ARGS, "pxr/base/vt/functions.h", 0))
49#include BOOST_PP_ITERATE()
50
51// ****************************************************************************
52// Doc headers for functions that are generated through macros. These get
53// decent doxygen (and epydoc) output even though we can't put them on the
54// real functions because they're expanded through boost or cpp macros.
55// ****************************************************************************
56
57// documentation for bool-result array comparison functions
58#ifdef doxygen
59
67template<typename T>
69VtEqual( VtArray<T> const &a, VtArray<T> const &b );
70template<typename T>
72VtEqual( T const &a, VtArray<T> const &b );
73template<typename T>
75VtEqual( VtArray<T> const &a, T const &b );
76
84template<typename T>
86VtNotEqual( VtArray<T> const &a, VtArray<T> const &b );
87template<typename T>
89VtNotEqual( T const &a, VtArray<T> const &b );
90template<typename T>
92VtNotEqual( VtArray<T> const &a, T const &b );
93
101template<typename T>
103VtGreater( VtArray<T> const &a, VtArray<T> const &b );
104template<typename T>
106VtGreater( T const &a, VtArray<T> const &b );
107template<typename T>
109VtGreater( VtArray<T> const &a, T const &b );
110
118template<typename T>
120VtLess( VtArray<T> const &a, VtArray<T> const &b );
121template<typename T>
123VtLess( T const &a, VtArray<T> const &b );
124template<typename T>
126VtLess( VtArray<T> const &a, T const &b );
127
136template<typename T>
139template<typename T>
141VtGreaterOrEqual( T const &a, VtArray<T> const &b );
142template<typename T>
144VtGreaterOrEqual( VtArray<T> const &a, T const &b );
145
153template<typename T>
155VtLessOrEqual( VtArray<T> const &a, VtArray<T> const &b );
156template<typename T>
158VtLessOrEqual( T const &a, VtArray<T> const &b );
159template<typename T>
161VtLessOrEqual( VtArray<T> const &a, T const &b );
162
163#endif
164
165// provide documentation for functions with variable numbers of inputs
166#ifdef doxygen
167
172template<typename T>
174VtCat( VtArray<T> const &a0, VtArray<T> const &a1, ... VtArray<T> const &aN);
175
176#endif
177
178// ****************************************************************************
179// Fixed-number-of-arguments functions go here (no preprocessor iteration to
180// handle multiple args)
181// ****************************************************************************
182
194template<typename T>
195bool VtAnyTrue(VtArray<T> const &a)
196{
197 if (a.empty())
198 return false;
199
200 for (size_t i = 0; i != a.size(); ++i) {
201 if (a[i] != VtZero<T>())
202 return true;
203 }
204
205 return false;
206}
207
208
219template<typename T>
220bool
222{
223 if (a.empty())
224 return false;
225
226 for (size_t i = 0; i != a.size(); ++i) {
227 if (a[i] == VtZero<T>())
228 return false;
229 }
230
231 return true;
232}
233
234// Macro defining functions for element-by-element comparison
235// operators (i.e. Equal, etc). There are three versions; each returns
236// a VtBoolArray of the same shape as the largest input.
237// *) two input arrays:
238// If one array contains a single element, it is compared to all the elements
239// in the other array. Otherwise both arrays must have the same shape.
240// *) scalar and array:
241// The scalar is compared to all the elements in the array.
242// *) array and scalar:
243// The same as scalar and array.
244#define VTFUNCTION_BOOL(funcname,op) \
245template<typename T> \
246VtArray<bool> \
247funcname(T const &scalar, VtArray<T> const &vec) { \
248 VtArray<bool> ret(vec.size()); \
249 for (size_t i = 0, n = vec.size(); i != n; ++i) { \
250 ret[i] = (scalar op vec[i]); \
251 } \
252 return ret; \
253} \
254template<typename T> \
255VtArray<bool> \
256funcname(VtArray<T> const &vec, T const &scalar) { \
257 VtArray<bool> ret(vec.size()); \
258 for (size_t i = 0, n = vec.size(); i != n; ++i) { \
259 ret[i] = (vec[i] op scalar); \
260 } \
261 return ret; \
262} \
263template<typename T> \
264VtArray<bool> \
265funcname(VtArray<T> const &a, VtArray<T> const &b) \
266{ \
267 if (a.empty() || b.empty()) { \
268 return VtArray<bool>(); \
269 } \
270 \
271 if (a.size() == 1) { \
272 return funcname(a[0], b); \
273 } else if (b.size() == 1) { \
274 return funcname(a, b[0]); \
275 } else if (a.size() == b.size()) { \
276 VtArray<bool> ret(a.size()); \
277 for (size_t i = 0, n = a.size(); i != n; ++i) { \
278 ret[i] = (a[i] op b[i]); \
279 } \
280 return ret; \
281 } else { \
282 TF_CODING_ERROR("Non-conforming inputs."); \
283 return VtArray<bool>(); \
284 } \
285}
286
287VTFUNCTION_BOOL(VtEqual,==)
288VTFUNCTION_BOOL(VtNotEqual,!=)
289VTFUNCTION_BOOL(VtGreater,>)
290VTFUNCTION_BOOL(VtLess,<)
291VTFUNCTION_BOOL(VtGreaterOrEqual,>=)
292VTFUNCTION_BOOL(VtLessOrEqual,<=)
293
294PXR_NAMESPACE_CLOSE_SCOPE
295
296#endif // PXR_BASE_VT_FUNCTIONS_H
297
298#else // BOOST_PP_IS_ITERATING
299
300// ****************************************************************************
301// Variable-number-of-arguments functions go here; preprocessor iteration
302// includes this file again and again, but turns off the pieces we don't
303// want to use for a particular function.
304// ****************************************************************************
305
306#define N BOOST_PP_ITERATION()
307
308#if BOOST_PP_ITERATION_FLAGS() == 0 // VtCat
309
310#define VtCat_SIZE(dummy, n, dummy2) newSize += s##n.size();
311#define VtCat_COPY(dummy, n, dummy2) \
312 for (size_t i = 0; i < s##n.size(); ++i) \
313 ret[offset+i] = s##n[i]; \
314 offset += s##n.size();
315
316// real documentation is above (for doxygen purposes)
317template<typename T>
319VtCat( BOOST_PP_ENUM_PARAMS(N, VtArray<T> const &s) )
320{
321 size_t newSize = 0;
322
323 BOOST_PP_REPEAT( N, VtCat_SIZE, ignored )
324
325 if (newSize == 0)
326 return VtArray<T>();
327
328 // new array with flattened size
329 VtArray<T> ret(newSize);
330
331 // fill it with data from old arrays
332#if N > 0
333 size_t offset = 0;
334 BOOST_PP_REPEAT( N, VtCat_COPY, ignored )
335#endif
336
337 return ret;
338}
339
340#undef VtCat_SIZE
341#undef VtCat_COPY
342
343#endif // BOOST_PP_ITERATION_FLAGS
344
345#undef N
346
347#endif // BOOST_PP_IS_ITERATING
348
Represents an arbitrary dimensional rectangular container class.
Definition: array.h:228
bool VtAllTrue(VtArray< T > const &a)
Returns true if every element of input array is not VtZero, else false.
Definition: functions.h:221
VtArray< T > VtCat(VtArray< T > const &a0, VtArray< T > const &a1,... VtArray< T > const &aN)
Concatenates arrays.
bool VtAnyTrue(VtArray< T > const &a)
Returns true if any element of input array is not VtZero, else false.
Definition: functions.h:195
VtArray< T > VtLessOrEqual(VtArray< T > const &a, VtArray< T > const &b)
Returns a bool array specifying, element-by-element, if the first input contains values less than or ...
VtArray< T > VtGreaterOrEqual(VtArray< T > const &a, VtArray< T > const &b)
Returns a bool array specifying, element-by-element, if the first input contains values greater than ...
VtArray< T > VtNotEqual(VtArray< T > const &a, VtArray< T > const &b)
Returns a bool array specifying, element-by-element, if the two inputs contain inequal values.
VtArray< T > VtEqual(VtArray< T > const &a, VtArray< T > const &b)
Returns a bool array specifying, element-by-element, if the two inputs contain equal values.
VtArray< T > VtLess(VtArray< T > const &a, VtArray< T > const &b)
Returns a bool array specifying, element-by-element, if the first input contains values less than tho...
VtArray< T > VtGreater(VtArray< T > const &a, VtArray< T > const &b)
Returns a bool array specifying, element-by-element, if the first input contains values greater than ...
size_t size() const
Return the total number of elements in this array.
Definition: array.h:489
bool empty() const
Return true if this array contains no elements, false otherwise.
Definition: array.h:515