1
0
Fork 0

(svn r19237) -Codechange: use types directly and prefer uint instead of int (skidd13)

release/1.1
yexo 2010-02-25 11:46:20 +00:00
parent cc413b8f6e
commit 48ee211c1e
1 changed files with 27 additions and 29 deletions

View File

@ -22,7 +22,7 @@
* *
* 1) It allocates space for item pointers (array). Items are allocated elsewhere. * 1) It allocates space for item pointers (array). Items are allocated elsewhere.
* *
* 2) ItemPtr [0] is never used. Total array size is max_items + 1, because we * 2) T*[0] is never used. Total array size is max_items + 1, because we
* use indices 1..max_items instead of zero based C indexing. * use indices 1..max_items instead of zero based C indexing.
* *
* 3) Item of the binary heap should support these public members: * 3) Item of the binary heap should support these public members:
@ -30,21 +30,19 @@
* *
*/ */
template <class Titem_> template <class T>
class CBinaryHeapT { class CBinaryHeapT {
public:
typedef Titem_ *ItemPtr;
private: private:
int m_size; ///< Number of items in the heap uint m_size; ///< Number of items in the heap
int m_max_size; ///< Maximum number of items the heap can hold uint m_max_size; ///< Maximum number of items the heap can hold
ItemPtr *m_items; ///< The heap item pointers T **m_items; ///< The heap item pointers
public: public:
explicit CBinaryHeapT(int max_items) explicit CBinaryHeapT(uint max_items)
: m_size(0) : m_size(0)
, m_max_size(max_items) , m_max_size(max_items)
{ {
m_items = MallocT<ItemPtr>(max_items + 1); m_items = MallocT<T*>(max_items + 1);
} }
~CBinaryHeapT() ~CBinaryHeapT()
@ -57,7 +55,7 @@ public:
public: public:
/** Return the number of items stored in the priority queue. /** Return the number of items stored in the priority queue.
* @return number of items in the queue */ * @return number of items in the queue */
FORCEINLINE int Size() const {return m_size;}; FORCEINLINE uint Size() const {return m_size;};
/** Test if the priority queue is empty. /** Test if the priority queue is empty.
* @return true if empty */ * @return true if empty */
@ -69,7 +67,7 @@ public:
/** Find the smallest item in the priority queue. /** Find the smallest item in the priority queue.
* Return the smallest item, or throw assert if empty. */ * Return the smallest item, or throw assert if empty. */
FORCEINLINE Titem_& GetHead() FORCEINLINE T& GetHead()
{ {
assert(!IsEmpty()); assert(!IsEmpty());
return *m_items[1]; return *m_items[1];
@ -77,26 +75,26 @@ public:
/** Insert new item into the priority queue, maintaining heap order. /** Insert new item into the priority queue, maintaining heap order.
* @return false if the queue is full. */ * @return false if the queue is full. */
FORCEINLINE void Push(Titem_& new_item) FORCEINLINE void Push(T& new_item)
{ {
if (IsFull()) { if (IsFull()) {
m_max_size *= 2; m_max_size *= 2;
m_items = ReallocT<ItemPtr>(m_items, m_max_size + 1); m_items = ReallocT<T*>(m_items, m_max_size + 1);
} }
/* make place for new item */ /* make place for new item */
int gap = ++m_size; uint gap = ++m_size;
/* Heapify up */ /* Heapify up */
for (int parent = gap / 2; (parent > 0) && (new_item < *m_items[parent]); gap = parent, parent /= 2) for (uint parent = gap / 2; (parent > 0) && (new_item < *m_items[parent]); gap = parent, parent /= 2)
m_items[gap] = m_items[parent]; m_items[gap] = m_items[parent];
m_items[gap] = &new_item; m_items[gap] = &new_item;
CheckConsistency(); CheckConsistency();
} }
/** Remove and return the smallest item from the priority queue. */ /** Remove and return the smallest item from the priority queue. */
FORCEINLINE Titem_& PopHead() FORCEINLINE T& PopHead()
{ {
Titem_& ret = GetHead(); T& ret = GetHead();
RemoveHead(); RemoveHead();
return ret; return ret;
} }
@ -107,16 +105,16 @@ public:
assert(!IsEmpty()); assert(!IsEmpty());
/* at index 1 we have a gap now */ /* at index 1 we have a gap now */
int gap = 1; uint gap = 1;
/* Heapify down: /* Heapify down:
* last item becomes a candidate for the head. Call it new_item. */ * last item becomes a candidate for the head. Call it new_item. */
Titem_& new_item = *m_items[m_size--]; T& new_item = *m_items[m_size--];
/* now we must maintain relation between parent and its children: /* now we must maintain relation between parent and its children:
* parent <= any child * parent <= any child
* from head down to the tail */ * from head down to the tail */
int child = 2; // first child is at [parent * 2] uint child = 2; // first child is at [parent * 2]
/* while children are valid */ /* while children are valid */
while (child <= m_size) { while (child <= m_size) {
@ -140,11 +138,11 @@ public:
} }
/** Remove item specified by index */ /** Remove item specified by index */
FORCEINLINE void RemoveByIdx(int idx) FORCEINLINE void RemoveByIdx(uint idx)
{ {
/* at position idx we have a gap now */ /* at position idx we have a gap now */
int gap = idx; uint gap = idx;
Titem_& last = *m_items[m_size]; T& last = *m_items[m_size];
if (idx < m_size) { if (idx < m_size) {
assert(idx >= 1); assert(idx >= 1);
m_size--; m_size--;
@ -153,7 +151,7 @@ public:
while (gap > 1) while (gap > 1)
{ {
/* compare [gap] with its parent */ /* compare [gap] with its parent */
int parent = gap / 2; uint parent = gap / 2;
if (last < *m_items[parent]) { if (last < *m_items[parent]) {
m_items[gap] = m_items[parent]; m_items[gap] = m_items[parent];
gap = parent; gap = parent;
@ -166,7 +164,7 @@ public:
/* Heapify (move gap) down: */ /* Heapify (move gap) down: */
while (true) { while (true) {
/* where we do have our children? */ /* where we do have our children? */
int child = gap * 2; // first child is at [parent * 2] uint child = gap * 2; // first child is at [parent * 2]
if (child > m_size) break; if (child > m_size) break;
/* choose the smaller child */ /* choose the smaller child */
if (child < m_size && *m_items[child + 1] < *m_items[child]) if (child < m_size && *m_items[child + 1] < *m_items[child])
@ -190,10 +188,10 @@ public:
} }
/** return index of the item that matches (using &item1 == &item2) the given item. */ /** return index of the item that matches (using &item1 == &item2) the given item. */
FORCEINLINE int FindLinear(const Titem_& item) const FORCEINLINE uint FindLinear(const T& item) const
{ {
if (IsEmpty()) return 0; if (IsEmpty()) return 0;
for (ItemPtr *ppI = m_items + 1, *ppLast = ppI + m_size; ppI <= ppLast; ppI++) { for (T **ppI = m_items + 1, **ppLast = ppI + m_size; ppI <= ppLast; ppI++) {
if (*ppI == &item) { if (*ppI == &item) {
return ppI - m_items; return ppI - m_items;
} }
@ -210,8 +208,8 @@ public:
{ {
/* enable it if you suspect binary heap doesn't work well */ /* enable it if you suspect binary heap doesn't work well */
#if 0 #if 0
for (int child = 2; child <= m_size; child++) { for (uint child = 2; child <= m_size; child++) {
int parent = child / 2; uint parent = child / 2;
assert(!(*m_items[child] < *m_items[parent])); assert(!(*m_items[child] < *m_items[parent]));
} }
#endif #endif