1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2008-2018 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
#ifndef PXFOUNDATION_PXSTRIDEITERATOR_H
#define PXFOUNDATION_PXSTRIDEITERATOR_H
#include "foundation/Px.h"
#include "foundation/PxAssert.h"
/** \addtogroup foundation
@{
*/
#if !PX_DOXYGEN
namespace physx
{
#endif
/**
\brief Iterator class for iterating over arrays of data that may be interleaved with other data.
This class is used for iterating over arrays of elements that may have a larger element to element
offset, called the stride, than the size of the element itself (non-contiguous).
The template parameter T denotes the type of the element accessed. The stride itself
is stored as a member field so multiple instances of a PxStrideIterator class can have
different strides. This is useful for cases were the stride depends on runtime configuration.
The stride iterator can be used for index based access, e.g.:
\code
PxStrideIterator<PxVec3> strideArray(...);
for (unsigned i = 0; i < 10; ++i)
{
PxVec3& vec = strideArray[i];
...
}
\endcode
or iteration by increment, e.g.:
\code
PxStrideIterator<PxVec3> strideBegin(...);
PxStrideIterator<PxVec3> strideEnd(strideBegin + 10);
for (PxStrideIterator<PxVec3> it = strideBegin; it < strideEnd; ++it)
{
PxVec3& vec = *it;
...
}
\endcode
Two special cases:
- A stride of sizeof(T) represents a regular c array of type T.
- A stride of 0 can be used to describe re-occurrence of the same element multiple times.
*/
template <typename T>
class PxStrideIterator
{
#if !PX_DOXYGEN
template <typename X>
struct StripConst
{
typedef X Type;
};
template <typename X>
struct StripConst<const X>
{
typedef X Type;
};
#endif
public:
/**
\brief Constructor.
Optionally takes a pointer to an element and a stride.
\param[in] ptr pointer to element, defaults to NULL.
\param[in] stride stride for accessing consecutive elements, defaults to the size of one element.
*/
explicit PX_INLINE PxStrideIterator(T* ptr = NULL, PxU32 stride = sizeof(T)) : mPtr(ptr), mStride(stride)
{
PX_ASSERT(mStride == 0 || sizeof(T) <= mStride);
}
/**
\brief Copy constructor.
\param[in] strideIterator PxStrideIterator to be copied.
*/
PX_INLINE PxStrideIterator(const PxStrideIterator<typename StripConst<T>::Type>& strideIterator)
: mPtr(strideIterator.ptr()), mStride(strideIterator.stride())
{
PX_ASSERT(mStride == 0 || sizeof(T) <= mStride);
}
/**
\brief Get pointer to element.
*/
PX_INLINE T* ptr() const
{
return mPtr;
}
/**
\brief Get stride.
*/
PX_INLINE PxU32 stride() const
{
return mStride;
}
/**
\brief Indirection operator.
*/
PX_INLINE T& operator*() const
{
return *mPtr;
}
/**
\brief Dereferencing operator.
*/
PX_INLINE T* operator->() const
{
return mPtr;
}
/**
\brief Indexing operator.
*/
PX_INLINE T& operator[](unsigned int i) const
{
return *byteAdd(mPtr, i * stride());
}
/**
\brief Pre-increment operator.
*/
PX_INLINE PxStrideIterator& operator++()
{
mPtr = byteAdd(mPtr, stride());
return *this;
}
/**
\brief Post-increment operator.
*/
PX_INLINE PxStrideIterator operator++(int)
{
PxStrideIterator tmp = *this;
mPtr = byteAdd(mPtr, stride());
return tmp;
}
/**
\brief Pre-decrement operator.
*/
PX_INLINE PxStrideIterator& operator--()
{
mPtr = byteSub(mPtr, stride());
return *this;
}
/**
\brief Post-decrement operator.
*/
PX_INLINE PxStrideIterator operator--(int)
{
PxStrideIterator tmp = *this;
mPtr = byteSub(mPtr, stride());
return tmp;
}
/**
\brief Addition operator.
*/
PX_INLINE PxStrideIterator operator+(unsigned int i) const
{
return PxStrideIterator(byteAdd(mPtr, i * stride()), stride());
}
/**
\brief Subtraction operator.
*/
PX_INLINE PxStrideIterator operator-(unsigned int i) const
{
return PxStrideIterator(byteSub(mPtr, i * stride()), stride());
}
/**
\brief Addition compound assignment operator.
*/
PX_INLINE PxStrideIterator& operator+=(unsigned int i)
{
mPtr = byteAdd(mPtr, i * stride());
return *this;
}
/**
\brief Subtraction compound assignment operator.
*/
PX_INLINE PxStrideIterator& operator-=(unsigned int i)
{
mPtr = byteSub(mPtr, i * stride());
return *this;
}
/**
\brief Iterator difference.
*/
PX_INLINE int operator-(const PxStrideIterator& other) const
{
PX_ASSERT(isCompatible(other));
int byteDiff = static_cast<int>(reinterpret_cast<const PxU8*>(mPtr) - reinterpret_cast<const PxU8*>(other.mPtr));
return byteDiff / static_cast<int>(stride());
}
/**
\brief Equality operator.
*/
PX_INLINE bool operator==(const PxStrideIterator& other) const
{
PX_ASSERT(isCompatible(other));
return mPtr == other.mPtr;
}
/**
\brief Inequality operator.
*/
PX_INLINE bool operator!=(const PxStrideIterator& other) const
{
PX_ASSERT(isCompatible(other));
return mPtr != other.mPtr;
}
/**
\brief Less than operator.
*/
PX_INLINE bool operator<(const PxStrideIterator& other) const
{
PX_ASSERT(isCompatible(other));
return mPtr < other.mPtr;
}
/**
\brief Greater than operator.
*/
PX_INLINE bool operator>(const PxStrideIterator& other) const
{
PX_ASSERT(isCompatible(other));
return mPtr > other.mPtr;
}
/**
\brief Less or equal than operator.
*/
PX_INLINE bool operator<=(const PxStrideIterator& other) const
{
PX_ASSERT(isCompatible(other));
return mPtr <= other.mPtr;
}
/**
\brief Greater or equal than operator.
*/
PX_INLINE bool operator>=(const PxStrideIterator& other) const
{
PX_ASSERT(isCompatible(other));
return mPtr >= other.mPtr;
}
private:
PX_INLINE static T* byteAdd(T* ptr, PxU32 bytes)
{
return const_cast<T*>(reinterpret_cast<const T*>(reinterpret_cast<const PxU8*>(ptr) + bytes));
}
PX_INLINE static T* byteSub(T* ptr, PxU32 bytes)
{
return const_cast<T*>(reinterpret_cast<const T*>(reinterpret_cast<const PxU8*>(ptr) - bytes));
}
PX_INLINE bool isCompatible(const PxStrideIterator& other) const
{
int byteDiff = static_cast<int>(reinterpret_cast<const PxU8*>(mPtr) - reinterpret_cast<const PxU8*>(other.mPtr));
return (stride() == other.stride()) && (abs(byteDiff) % stride() == 0);
}
T* mPtr;
PxU32 mStride;
};
/**
\brief Addition operator.
*/
template <typename T>
PX_INLINE PxStrideIterator<T> operator+(int i, PxStrideIterator<T> it)
{
it += i;
return it;
}
/**
\brief Stride iterator factory function which infers the iterator type.
*/
template <typename T>
PX_INLINE PxStrideIterator<T> PxMakeIterator(T* ptr, PxU32 stride = sizeof(T))
{
return PxStrideIterator<T>(ptr, stride);
}
/**
\brief Stride iterator factory function which infers the iterator type.
*/
template <typename T>
PX_INLINE PxStrideIterator<const T> PxMakeIterator(const T* ptr, PxU32 stride = sizeof(T))
{
return PxStrideIterator<const T>(ptr, stride);
}
#if !PX_DOXYGEN
} // namespace physx
#endif
/** @} */
#endif // PXFOUNDATION_PXSTRIDEITERATOR_H
|