RavEngine
Loading...
Searching...
No Matches
PxStrideIterator.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef PX_STRIDE_ITERATOR_H
30#define PX_STRIDE_ITERATOR_H
31
32#include "foundation/Px.h"
33#include "foundation/PxAssert.h"
34
39#if !PX_DOXYGEN
40namespace physx
41{
42#endif
43
79template <typename T>
81{
82
83#if !PX_DOXYGEN
84 template <typename X>
85 struct StripConst
86 {
87 typedef X Type;
88 };
89
90 template <typename X>
91 struct StripConst<const X>
92 {
93 typedef X Type;
94 };
95#endif
96
97 public:
106 explicit PX_INLINE PxStrideIterator(T* ptr = NULL, PxU32 stride = sizeof(T)) : mPtr(ptr), mStride(stride)
107 {
108 PX_ASSERT(mStride == 0 || sizeof(T) <= mStride);
109 }
110
116 PX_INLINE PxStrideIterator(const PxStrideIterator<typename StripConst<T>::Type>& strideIterator)
117 : mPtr(strideIterator.ptr()), mStride(strideIterator.stride())
118 {
119 PX_ASSERT(mStride == 0 || sizeof(T) <= mStride);
120 }
121
125 PX_INLINE T* ptr() const
126 {
127 return mPtr;
128 }
129
133 PX_INLINE PxU32 stride() const
134 {
135 return mStride;
136 }
137
142 {
143 return *mPtr;
144 }
145
150 {
151 return mPtr;
152 }
153
157 PX_INLINE T& operator[](unsigned int i) const
158 {
159 return *byteAdd(mPtr, i * stride());
160 }
161
166 {
167 mPtr = byteAdd(mPtr, stride());
168 return *this;
169 }
170
175 {
176 PxStrideIterator tmp = *this;
177 mPtr = byteAdd(mPtr, stride());
178 return tmp;
179 }
180
185 {
186 mPtr = byteSub(mPtr, stride());
187 return *this;
188 }
189
194 {
195 PxStrideIterator tmp = *this;
196 mPtr = byteSub(mPtr, stride());
197 return tmp;
198 }
199
204 {
205 return PxStrideIterator(byteAdd(mPtr, i * stride()), stride());
206 }
207
212 {
213 return PxStrideIterator(byteSub(mPtr, i * stride()), stride());
214 }
215
220 {
221 mPtr = byteAdd(mPtr, i * stride());
222 return *this;
223 }
224
229 {
230 mPtr = byteSub(mPtr, i * stride());
231 return *this;
232 }
233
237 PX_INLINE int operator-(const PxStrideIterator& other) const
238 {
239 PX_ASSERT(isCompatible(other));
240 int byteDiff = static_cast<int>(reinterpret_cast<const PxU8*>(mPtr) - reinterpret_cast<const PxU8*>(other.mPtr));
241 return byteDiff / static_cast<int>(stride());
242 }
243
247 PX_INLINE bool operator==(const PxStrideIterator& other) const
248 {
249 PX_ASSERT(isCompatible(other));
250 return mPtr == other.mPtr;
251 }
252
256 PX_INLINE bool operator!=(const PxStrideIterator& other) const
257 {
258 PX_ASSERT(isCompatible(other));
259 return mPtr != other.mPtr;
260 }
261
265 PX_INLINE bool operator<(const PxStrideIterator& other) const
266 {
267 PX_ASSERT(isCompatible(other));
268 return mPtr < other.mPtr;
269 }
270
274 PX_INLINE bool operator>(const PxStrideIterator& other) const
275 {
276 PX_ASSERT(isCompatible(other));
277 return mPtr > other.mPtr;
278 }
279
283 PX_INLINE bool operator<=(const PxStrideIterator& other) const
284 {
285 PX_ASSERT(isCompatible(other));
286 return mPtr <= other.mPtr;
287 }
288
292 PX_INLINE bool operator>=(const PxStrideIterator& other) const
293 {
294 PX_ASSERT(isCompatible(other));
295 return mPtr >= other.mPtr;
296 }
297
298 private:
299 PX_INLINE static T* byteAdd(T* ptr, PxU32 bytes)
300 {
301 return const_cast<T*>(reinterpret_cast<const T*>(reinterpret_cast<const PxU8*>(ptr) + bytes));
302 }
303
304 PX_INLINE static T* byteSub(T* ptr, PxU32 bytes)
305 {
306 return const_cast<T*>(reinterpret_cast<const T*>(reinterpret_cast<const PxU8*>(ptr) - bytes));
307 }
308
309 PX_INLINE bool isCompatible(const PxStrideIterator& other) const
310 {
311 int byteDiff = static_cast<int>(reinterpret_cast<const PxU8*>(mPtr) - reinterpret_cast<const PxU8*>(other.mPtr));
312 return (stride() == other.stride()) && (abs(byteDiff) % stride() == 0);
313 }
314
315 T* mPtr;
316 PxU32 mStride;
317};
318
322template <typename T>
324{
325 it += i;
326 return it;
327}
328
332template <typename T>
333PX_INLINE PxStrideIterator<T> PxMakeIterator(T* ptr, PxU32 stride = sizeof(T))
334{
335 return PxStrideIterator<T>(ptr, stride);
336}
337
341template <typename T>
342PX_INLINE PxStrideIterator<const T> PxMakeIterator(const T* ptr, PxU32 stride = sizeof(T))
343{
344 return PxStrideIterator<const T>(ptr, stride);
345}
346
347#if !PX_DOXYGEN
348} // namespace physx
349#endif
350
352#endif
353
Definition format.h:4067
Iterator class for iterating over arrays of data that may be interleaved with other data.
Definition PxStrideIterator.h:81
PX_INLINE PxStrideIterator operator--(int)
Post-decrement operator.
Definition PxStrideIterator.h:193
PX_INLINE PxStrideIterator operator+(unsigned int i) const
Addition operator.
Definition PxStrideIterator.h:203
PX_INLINE int operator-(const PxStrideIterator &other) const
Iterator difference.
Definition PxStrideIterator.h:237
PX_INLINE T & operator[](unsigned int i) const
Indexing operator.
Definition PxStrideIterator.h:157
PX_INLINE T * operator->() const
Dereferencing operator.
Definition PxStrideIterator.h:149
PX_INLINE bool operator==(const PxStrideIterator &other) const
Equality operator.
Definition PxStrideIterator.h:247
PX_INLINE PxStrideIterator & operator+=(unsigned int i)
Addition compound assignment operator.
Definition PxStrideIterator.h:219
PX_INLINE PxStrideIterator & operator--()
Pre-decrement operator.
Definition PxStrideIterator.h:184
PX_INLINE PxU32 stride() const
Get stride.
Definition PxStrideIterator.h:133
PX_INLINE T * ptr() const
Get pointer to element.
Definition PxStrideIterator.h:125
PX_INLINE bool operator>=(const PxStrideIterator &other) const
Greater or equal than operator.
Definition PxStrideIterator.h:292
PX_INLINE T & operator*() const
Indirection operator.
Definition PxStrideIterator.h:141
PX_INLINE PxStrideIterator(const PxStrideIterator< typename StripConst< T >::Type > &strideIterator)
Copy constructor.
Definition PxStrideIterator.h:116
PX_INLINE bool operator<(const PxStrideIterator &other) const
Less than operator.
Definition PxStrideIterator.h:265
PX_INLINE bool operator!=(const PxStrideIterator &other) const
Inequality operator.
Definition PxStrideIterator.h:256
PX_INLINE PxStrideIterator operator++(int)
Post-increment operator.
Definition PxStrideIterator.h:174
PX_INLINE bool operator<=(const PxStrideIterator &other) const
Less or equal than operator.
Definition PxStrideIterator.h:283
PX_INLINE PxStrideIterator & operator++()
Pre-increment operator.
Definition PxStrideIterator.h:165
PX_INLINE bool operator>(const PxStrideIterator &other) const
Greater than operator.
Definition PxStrideIterator.h:274
PX_INLINE PxStrideIterator operator-(unsigned int i) const
Subtraction operator.
Definition PxStrideIterator.h:211
PX_INLINE PxStrideIterator(T *ptr=NULL, PxU32 stride=sizeof(T))
Constructor.
Definition PxStrideIterator.h:106
PX_INLINE PxStrideIterator & operator-=(unsigned int i)
Subtraction compound assignment operator.
Definition PxStrideIterator.h:228
#define PX_INLINE
Definition PxPreprocessor.h:320
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PX_INLINE PxStrideIterator< T > operator+(int i, PxStrideIterator< T > it)
Addition operator.
Definition PxStrideIterator.h:323
PX_INLINE PxStrideIterator< T > PxMakeIterator(T *ptr, PxU32 stride=sizeof(T))
Stride iterator factory function which infers the iterator type.
Definition PxStrideIterator.h:333