29#ifndef PX_SORT_INTERNALS_H
30#define PX_SORT_INTERNALS_H
36#include "foundation/PxAssert.h"
37#include "foundation/PxMathIntrinsics.h"
38#include "foundation/PxBasicTemplates.h"
39#include "foundation/PxUserAllocated.h"
45template <
class T,
class Predicate>
46PX_INLINE void PxMedian3(T* elements, int32_t first, int32_t last, Predicate& compare)
54 int32_t mid = (first + last) / 2;
56 if(compare(elements[mid], elements[first]))
57 PxSwap(elements[first], elements[mid]);
59 if(compare(elements[last], elements[first]))
60 PxSwap(elements[first], elements[last]);
62 if(compare(elements[last], elements[mid]))
63 PxSwap(elements[mid], elements[last]);
66 PxSwap(elements[mid], elements[last - 1]);
69template <
class T,
class Predicate>
70PX_INLINE int32_t PxPartition(T* elements, int32_t first, int32_t last, Predicate& compare)
72 PxMedian3(elements, first, last, compare);
93 while(compare(elements[++i], elements[last - 1]))
95 while(compare(elements[last - 1], elements[--j]))
101 PX_ASSERT(i <= last && j >= first);
102 PxSwap(elements[i], elements[j]);
106 PX_ASSERT(i <= last && first <= (last - 1));
107 PxSwap(elements[i], elements[last - 1]);
112template <
class T,
class Predicate>
113PX_INLINE void PxSmallSort(T* elements, int32_t first, int32_t last, Predicate& compare)
117 for(int32_t i = first; i < last; i++)
120 for(int32_t j = i + 1; j <= last; j++)
121 if(compare(elements[j], elements[m]))
125 PxSwap(elements[m], elements[i]);
129template <
class PxAllocator>
133 uint32_t mSize, mCapacity;
139 : mAllocator(inAllocator), mSize(0), mCapacity(capacity), mMemory(memory), mRealloc(
false)
145 mAllocator.deallocate(mMemory);
152 reinterpret_cast<int32_t*
>(mAllocator.allocate(
sizeof(int32_t) * mCapacity, __FILE__, __LINE__));
153 intrinsics::memCopy(newMem, mMemory, mSize *
sizeof(int32_t));
155 mAllocator.deallocate(mMemory);
160 PX_INLINE void push(int32_t start, int32_t end)
162 if(mSize >= mCapacity - 1)
164 mMemory[mSize++] = start;
165 mMemory[mSize++] = end;
168 PX_INLINE void pop(int32_t& start, int32_t& end)
171 end = mMemory[--mSize];
172 start = mMemory[--mSize];
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