(svn r19241) -Cleanup: Move the HeapifyUp code into its own method (skidd13)

This commit is contained in:
yexo 2010-02-25 11:48:09 +00:00
parent 37b4da9f4b
commit a44f12ade5

View File

@ -79,6 +79,28 @@ protected:
return gap; return gap;
} }
/** Heapify (move gap) up */
FORCEINLINE uint HeapifyUp(uint gap, T *item)
{
assert(gap != 0);
uint parent;
while (gap > 1) {
/* compare [gap] with its parent */
parent = gap / 2;
if (!(*item <*m_items[parent])) {
/* we don't need to continue upstairs */
break;
}
m_items[gap] = m_items[parent];
gap = parent;
}
return gap;
}
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 */
@ -115,19 +137,7 @@ public:
} }
/* make place for new item */ /* make place for new item */
uint gap = ++m_size; uint gap = HeapifyUp(++m_size, &new_item);
/* Heapify up */
while (gap > 1) {
/* compare [gap] with its parent */
uint parent = gap / 2;
if (new_item < *m_items[parent]) {
m_items[gap] = m_items[parent];
gap = parent;
} else {
/* we don't need to continue upstairs */
break;
}
}
m_items[gap] = &new_item; m_items[gap] = &new_item;
CheckConsistency(); CheckConsistency();
} }
@ -157,27 +167,14 @@ public:
/** Remove item specified by index */ /** Remove item specified by index */
FORCEINLINE void RemoveByIdx(uint idx) FORCEINLINE void RemoveByIdx(uint idx)
{ {
/* at position idx we have a gap now */
uint gap = idx;
if (idx < m_size) { if (idx < m_size) {
assert(idx >= 1); assert(idx != 0);
m_size--; m_size--;
T *last = End(); /* at position idx we have a gap now */
/* and the candidate item for fixing this gap is our last item 'last'
* Move gap / last item up: */
while (gap > 1)
{
/* compare [gap] with its parent */
uint parent = gap / 2;
if (*last < *m_items[parent]) {
m_items[gap] = m_items[parent];
gap = parent;
} else {
/* we don't need to continue upstairs */
break;
}
}
T *last = End();
/* Fix binary tree up and downwards */
uint gap = HeapifyUp(idx, last);
gap = HeapifyDown(gap, last); gap = HeapifyDown(gap, last);
/* move last item to the proper place */ /* move last item to the proper place */
if (!IsEmpty()) m_items[gap] = last; if (!IsEmpty()) m_items[gap] = last;