RavEngine
Loading...
Searching...
No Matches
PxSortInternals.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_SORT_INTERNALS_H
30#define PX_SORT_INTERNALS_H
31
36#include "foundation/PxAssert.h"
37#include "foundation/PxMathIntrinsics.h"
38#include "foundation/PxBasicTemplates.h"
39#include "foundation/PxUserAllocated.h"
40
41#if !PX_DOXYGEN
42namespace physx
43{
44#endif
45template <class T, class Predicate>
46PX_INLINE void PxMedian3(T* elements, int32_t first, int32_t last, Predicate& compare)
47{
48 /*
49 This creates sentinels because we know there is an element at the start minimum(or equal)
50 than the pivot and an element at the end greater(or equal) than the pivot. Plus the
51 median of 3 reduces the chance of degenerate behavour.
52 */
53
54 int32_t mid = (first + last) / 2;
55
56 if(compare(elements[mid], elements[first]))
57 PxSwap(elements[first], elements[mid]);
58
59 if(compare(elements[last], elements[first]))
60 PxSwap(elements[first], elements[last]);
61
62 if(compare(elements[last], elements[mid]))
63 PxSwap(elements[mid], elements[last]);
64
65 // keep the pivot at last-1
66 PxSwap(elements[mid], elements[last - 1]);
67}
68
69template <class T, class Predicate>
70PX_INLINE int32_t PxPartition(T* elements, int32_t first, int32_t last, Predicate& compare)
71{
72 PxMedian3(elements, first, last, compare);
73
74 /*
75 WARNING: using the line:
76
77 T partValue = elements[last-1];
78
79 and changing the scan loops to:
80
81 while(comparator.greater(partValue, elements[++i]));
82 while(comparator.greater(elements[--j], partValue);
83
84 triggers a compiler optimizer bug on xenon where it stores a double to the stack for partValue
85 then loads it as a single...:-(
86 */
87
88 int32_t i = first; // we know first is less than pivot(but i gets pre incremented)
89 int32_t j = last - 1; // pivot is in last-1 (but j gets pre decremented)
90
91 for(;;)
92 {
93 while(compare(elements[++i], elements[last - 1]))
94 ;
95 while(compare(elements[last - 1], elements[--j]))
96 ;
97
98 if(i >= j)
99 break;
100
101 PX_ASSERT(i <= last && j >= first);
102 PxSwap(elements[i], elements[j]);
103 }
104 // put the pivot in place
105
106 PX_ASSERT(i <= last && first <= (last - 1));
107 PxSwap(elements[i], elements[last - 1]);
108
109 return i;
110}
111
112template <class T, class Predicate>
113PX_INLINE void PxSmallSort(T* elements, int32_t first, int32_t last, Predicate& compare)
114{
115 // selection sort - could reduce to fsel on 360 with floats.
116
117 for(int32_t i = first; i < last; i++)
118 {
119 int32_t m = i;
120 for(int32_t j = i + 1; j <= last; j++)
121 if(compare(elements[j], elements[m]))
122 m = j;
123
124 if(m != i)
125 PxSwap(elements[m], elements[i]);
126 }
127}
128
129template <class PxAllocator>
131{
132 PxAllocator mAllocator;
133 uint32_t mSize, mCapacity;
134 int32_t* mMemory;
135 bool mRealloc;
136
137 public:
138 PxStack(int32_t* memory, uint32_t capacity, const PxAllocator& inAllocator)
139 : mAllocator(inAllocator), mSize(0), mCapacity(capacity), mMemory(memory), mRealloc(false)
140 {
141 }
142 ~PxStack()
143 {
144 if(mRealloc)
145 mAllocator.deallocate(mMemory);
146 }
147
148 void grow()
149 {
150 mCapacity *= 2;
151 int32_t* newMem =
152 reinterpret_cast<int32_t*>(mAllocator.allocate(sizeof(int32_t) * mCapacity, __FILE__, __LINE__));
153 intrinsics::memCopy(newMem, mMemory, mSize * sizeof(int32_t));
154 if(mRealloc)
155 mAllocator.deallocate(mMemory);
156 mRealloc = true;
157 mMemory = newMem;
158 }
159
160 PX_INLINE void push(int32_t start, int32_t end)
161 {
162 if(mSize >= mCapacity - 1)
163 grow();
164 mMemory[mSize++] = start;
165 mMemory[mSize++] = end;
166 }
167
168 PX_INLINE void pop(int32_t& start, int32_t& end)
169 {
170 PX_ASSERT(!empty());
171 end = mMemory[--mSize];
172 start = mMemory[--mSize];
173 }
174
175 PX_INLINE bool empty()
176 {
177 return mSize == 0;
178 }
179};
180
181#if !PX_DOXYGEN
182} // namespace physx
183#endif
185#endif
186
Definition PxAllocator.h:97
Definition PxSortInternals.h:131
#define PX_INLINE
Definition PxPreprocessor.h:320
int32 int32_t
Definition fwd.hpp:71
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39