29#ifndef CM_PRIORITY_QUEUE_H
30#define CM_PRIORITY_QUEUE_H
32#include "foundation/PxBasicTemplates.h"
33#include "foundation/PxAllocator.h"
34#include "foundation/PxMemory.h"
40 template<
class Element,
class Comparator = PxLess<Element> >
44 PriorityQueueBase(
const Comparator& less, Element* elements) : Comparator(less), mHeapSize(0), mDataPtr(elements)
67 return (mHeapSize == 0);
80 PxU32 parentIndex = parent(mHeapSize);
82 for (newIndex = mHeapSize; newIndex > 0 && compare(value, mDataPtr[parentIndex]); newIndex = parentIndex, parentIndex= parent(newIndex))
84 mDataPtr[ newIndex ] = mDataPtr[parentIndex];
86 mDataPtr[newIndex] = value;
94 PX_ASSERT(mHeapSize > 0);
97 PxU32 tempHs = mHeapSize-1;
99 Element min = mDataPtr[0];
100 Element last = mDataPtr[tempHs];
102 for (i = 0; (child = left(i)) < tempHs; i = child)
105 const PxU32 rightChild = child + 1;
107 child += ((rightChild < tempHs) & compare((mDataPtr[rightChild]), (mDataPtr[child]))) ? 1 : 0;
109 if(compare(last, mDataPtr[child]))
112 mDataPtr[i] = mDataPtr[child];
114 mDataPtr[ i ] = last;
123 const Element& min = mDataPtr[0];
124 for(PxU32 i=1; i<mHeapSize; ++i)
126 if(compare(mDataPtr[i], min))
146 return Comparator::operator()(a,b);
151 return (nodeIndex << 1) + 1;
156 return (nodeIndex - 1) >> 1;
159 PriorityQueueBase<Element, Comparator>& operator = (
const PriorityQueueBase<Element, Comparator>);
162 template <
typename Element, PxU32 Capacity,
typename Comparator>
165 Element mData[Capacity];
173 PX_ASSERT(this->mHeapSize < Capacity);
180 template <typename Element, typename Comparator, typename Alloc = typename physx::PxAllocatorTraits<Element>::Type>
185 PriorityQueue(
const Comparator& less = Comparator(), PxU32 initialCapacity = 0, Alloc alloc = Alloc())
188 if(initialCapacity > 0)
189 this->mDataPtr =
reinterpret_cast<Element*
>(Alloc::allocate(
sizeof(Element)*initialCapacity, __FILE__, __LINE__));
195 this->deallocate(this->mDataPtr);
200 if(this->mHeapSize == mCapacity)
202 reserve((this->mHeapSize+1)*2);
214 if(newCapacity > mCapacity)
216 Element* newElems =
reinterpret_cast<Element*
>(Alloc::allocate(
sizeof(Element)*newCapacity, __FILE__, __LINE__));
219 physx::PxMemCopy(newElems, this->mDataPtr,
sizeof(Element) * this->mHeapSize);
220 Alloc::deallocate(this->mDataPtr);
222 this->mDataPtr = newElems;
223 mCapacity = newCapacity;
Definition CmPriorityQueue.h:164
Definition CmPriorityQueue.h:42
PX_FORCE_INLINE const Element top() const
Get the element with the highest priority.
Definition CmPriorityQueue.h:53
bool valid() const
Make sure the priority queue sort all elements correctly.
Definition CmPriorityQueue.h:121
PX_FORCE_INLINE void clear()
Empty the priority queue.
Definition CmPriorityQueue.h:71
PX_FORCE_INLINE Element top()
Get the element with the highest priority.
Definition CmPriorityQueue.h:59
PX_FORCE_INLINE void push(const Element &value)
Insert a new element into the priority queue. Only valid when size() is less than Capacity.
Definition CmPriorityQueue.h:77
PX_FORCE_INLINE bool empty() const
Check to whether the priority queue is empty.
Definition CmPriorityQueue.h:65
PX_FORCE_INLINE Element pop()
Delete the highest priority element. Only valid when non-empty.
Definition CmPriorityQueue.h:92
PxU32 size() const
Return number of elements in the priority queue.
Definition CmPriorityQueue.h:134
Definition CmPriorityQueue.h:182
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PX_FORCE_INLINE void * PxMemCopy(void *dest, const void *src, PxU32 count)
Copies the bytes of one memory block to another. The memory blocks must not overlap.
Definition PxMemory.h:83