(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.
* Return the smallest item, or throw assert if empty. */
FORCEINLINE Titem_& GetHead() {assert(!IsEmpty()); return *m_items[1];}
/** Insert new item into the priority queue, maintaining heap order. */
void Push(Titem_& new_item);
/** 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();
};
FORCEINLINE Titem_& GetHead()
{
assert(!IsEmpty());
return *m_items[1];
}
template <class Titem_>
FORCEINLINE void CBinaryHeapT<Titem_>::Push(Titem_& new_item)
{
/** Insert new item into the priority queue, maintaining heap order.
* @return false if the queue is full. */
FORCEINLINE void Push(Titem_& new_item)
{
if (IsFull()) {
m_max_size *= 2;
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] = &new_item;
CheckConsistency();
}
}
template <class Titem_>
FORCEINLINE void CBinaryHeapT<Titem_>::RemoveHead()
{
/** 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. */
FORCEINLINE void RemoveHead()
{
assert(!IsEmpty());
/* at index 1 we have a gap now */
@ -148,11 +137,11 @@ FORCEINLINE void CBinaryHeapT<Titem_>::RemoveHead()
/* move last item to the proper place */
if (m_size > 0) m_items[gap] = &new_item;
CheckConsistency();
}
}
template <class Titem_>
inline void CBinaryHeapT<Titem_>::RemoveByIdx(int idx)
{
/** Remove item specified by index */
FORCEINLINE void RemoveByIdx(int idx)
{
/* at position idx we have a gap now */
int gap = idx;
Titem_& last = *m_items[m_size];
@ -198,11 +187,11 @@ inline void CBinaryHeapT<Titem_>::RemoveByIdx(int idx)
m_size--;
}
CheckConsistency();
}
}
template <class Titem_>
inline int CBinaryHeapT<Titem_>::FindLinear(const Titem_& item) const
{
/** return index of the item that matches (using &item1 == &item2) the given item. */
FORCEINLINE int FindLinear(const Titem_& item) const
{
if (IsEmpty()) return 0;
for (ItemPtr *ppI = m_items + 1, *ppLast = ppI + m_size; ppI <= ppLast; ppI++) {
if (*ppI == &item) {
@ -210,11 +199,15 @@ inline int CBinaryHeapT<Titem_>::FindLinear(const Titem_& item) const
}
}
return 0;
}
}
template <class Titem_>
FORCEINLINE void CBinaryHeapT<Titem_>::CheckConsistency()
{
/** Make the priority queue empty.
* 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 */
#if 0
for (int child = 2; child <= m_size; child++) {
@ -222,7 +215,7 @@ FORCEINLINE void CBinaryHeapT<Titem_>::CheckConsistency()
assert(!(*m_items[child] < *m_items[parent]));
}
#endif
}
}
};
#endif /* BINARYHEAP_HPP */

Loading…
Cancel
Save