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