(svn r19236) -Codechange: move method code into class definition (skidd13)

pull/155/head
yexo 15 years ago
parent 82be024308
commit 9ed0a64d04

@ -69,35 +69,16 @@ 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() {assert(!IsEmpty()); return *m_items[1];} FORCEINLINE Titem_& GetHead()
{
/** Insert new item into the priority queue, maintaining heap order. */ assert(!IsEmpty());
void Push(Titem_& new_item); return *m_items[1];
}
/** Remove and return the smallest item from the priority queue. */
FORCEINLINE Titem_& PopHead() {Titem_& ret = GetHead(); RemoveHead(); return ret;};
/** Remove the smallest item from the priority queue. */
void RemoveHead();
/** Remove item specified by index */
void RemoveByIdx(int idx);
/** return index of the item that matches (using &item1 == &item2) the given item. */
int FindLinear(const Titem_& item) const;
/** Make the priority queue empty.
* All remaining items will remain untouched. */
void Clear() {m_size = 0;};
/** verifies the heap consistency (added during first YAPF debug phase) */
void CheckConsistency();
};
template <class Titem_> /** Insert new item into the priority queue, maintaining heap order.
FORCEINLINE void CBinaryHeapT<Titem_>::Push(Titem_& new_item) * @return false if the queue is full. */
{ FORCEINLINE void Push(Titem_& 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<ItemPtr>(m_items, m_max_size + 1);
@ -110,11 +91,19 @@ FORCEINLINE void CBinaryHeapT<Titem_>::Push(Titem_& new_item)
m_items[gap] = m_items[parent]; m_items[gap] = m_items[parent];
m_items[gap] = &new_item; m_items[gap] = &new_item;
CheckConsistency(); CheckConsistency();
} }
template <class Titem_> /** Remove and return the smallest item from the priority queue. */
FORCEINLINE void CBinaryHeapT<Titem_>::RemoveHead() FORCEINLINE Titem_& PopHead()
{ {
Titem_& ret = GetHead();
RemoveHead();
return ret;
}
/** Remove the smallest item from the priority queue. */
FORCEINLINE void RemoveHead()
{
assert(!IsEmpty()); assert(!IsEmpty());
/* at index 1 we have a gap now */ /* at index 1 we have a gap now */
@ -148,11 +137,11 @@ FORCEINLINE void CBinaryHeapT<Titem_>::RemoveHead()
/* move last item to the proper place */ /* move last item to the proper place */
if (m_size > 0) m_items[gap] = &new_item; if (m_size > 0) m_items[gap] = &new_item;
CheckConsistency(); CheckConsistency();
} }
template <class Titem_> /** Remove item specified by index */
inline void CBinaryHeapT<Titem_>::RemoveByIdx(int idx) FORCEINLINE void RemoveByIdx(int idx)
{ {
/* at position idx we have a gap now */ /* at position idx we have a gap now */
int gap = idx; int gap = idx;
Titem_& last = *m_items[m_size]; Titem_& last = *m_items[m_size];
@ -198,11 +187,11 @@ inline void CBinaryHeapT<Titem_>::RemoveByIdx(int idx)
m_size--; m_size--;
} }
CheckConsistency(); CheckConsistency();
} }
template <class Titem_> /** return index of the item that matches (using &item1 == &item2) the given item. */
inline int CBinaryHeapT<Titem_>::FindLinear(const Titem_& item) const FORCEINLINE int FindLinear(const Titem_& item) const
{ {
if (IsEmpty()) return 0; if (IsEmpty()) return 0;
for (ItemPtr *ppI = m_items + 1, *ppLast = ppI + m_size; ppI <= ppLast; ppI++) { for (ItemPtr *ppI = m_items + 1, *ppLast = ppI + m_size; ppI <= ppLast; ppI++) {
if (*ppI == &item) { if (*ppI == &item) {
@ -210,11 +199,15 @@ inline int CBinaryHeapT<Titem_>::FindLinear(const Titem_& item) const
} }
} }
return 0; return 0;
} }
template <class Titem_> /** Make the priority queue empty.
FORCEINLINE void CBinaryHeapT<Titem_>::CheckConsistency() * All remaining items will remain untouched. */
{ FORCEINLINE void Clear() {m_size = 0;}
/** verifies the heap consistency (added during first YAPF debug phase) */
FORCEINLINE void CheckConsistency()
{
/* 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 (int child = 2; child <= m_size; child++) {
@ -222,7 +215,7 @@ FORCEINLINE void CBinaryHeapT<Titem_>::CheckConsistency()
assert(!(*m_items[child] < *m_items[parent])); assert(!(*m_items[child] < *m_items[parent]));
} }
#endif #endif
} }
};
#endif /* BINARYHEAP_HPP */ #endif /* BINARYHEAP_HPP */

Loading…
Cancel
Save