(svn r19242) -Codechange: Perfer pointer instead of reference (skidd13)

-Cleanup: merge PopHead() and RemoveHead() into Shift()
pull/155/head
yexo 15 years ago
parent a44f12ade5
commit 4b6c04585e

@ -116,10 +116,10 @@ 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 T& GetHead() FORCEINLINE T *Begin()
{ {
assert(!IsEmpty()); assert(!IsEmpty());
return *m_items[1]; return m_items[1];
} }
FORCEINLINE T *End() FORCEINLINE T *End()
@ -129,7 +129,7 @@ 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(T& new_item) FORCEINLINE void Push(T *new_item)
{ {
if (IsFull()) { if (IsFull()) {
m_max_size *= 2; m_max_size *= 2;
@ -137,31 +137,27 @@ public:
} }
/* make place for new item */ /* make place for new item */
uint gap = HeapifyUp(++m_size, &new_item); uint gap = HeapifyUp(++m_size, new_item);
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 T& PopHead() FORCEINLINE T *Shift()
{
T& ret = GetHead();
RemoveHead();
return ret;
}
/** Remove the smallest item from the priority queue. */
FORCEINLINE void RemoveHead()
{ {
assert(!IsEmpty()); assert(!IsEmpty());
T *first = Begin();
m_size--; m_size--;
/* at index 1 we have a gap now */ /* at index 1 we have a gap now */
T *last = End(); T *last = End();
uint gap = HeapifyDown(1, last); uint gap = HeapifyDown(1, 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;
CheckConsistency(); CheckConsistency();
return first;
} }
/** Remove item specified by index */ /** Remove item specified by index */

@ -93,7 +93,7 @@ public:
{ {
assert(m_closed.Find(item.GetKey()) == NULL); assert(m_closed.Find(item.GetKey()) == NULL);
m_open.Push(item); m_open.Push(item);
m_open_queue.Push(item); m_open_queue.Push(&item);
if (&item == m_new_node) { if (&item == m_new_node) {
m_new_node = NULL; m_new_node = NULL;
} }
@ -103,8 +103,7 @@ public:
FORCEINLINE Titem_ *GetBestOpenNode() FORCEINLINE Titem_ *GetBestOpenNode()
{ {
if (!m_open_queue.IsEmpty()) { if (!m_open_queue.IsEmpty()) {
Titem_& item = m_open_queue.GetHead(); return m_open_queue.Begin();
return &item;
} }
return NULL; return NULL;
} }
@ -113,9 +112,9 @@ public:
FORCEINLINE Titem_ *PopBestOpenNode() FORCEINLINE Titem_ *PopBestOpenNode()
{ {
if (!m_open_queue.IsEmpty()) { if (!m_open_queue.IsEmpty()) {
Titem_& item = m_open_queue.PopHead(); Titem_ *item = m_open_queue.Shift();
m_open.Pop(item); m_open.Pop(*item);
return &item; return item;
} }
return NULL; return NULL;
} }

Loading…
Cancel
Save